| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | export class List { |
| 7 | | constructor() { |
| 8 | | throw new Error('Remove this line and implement the function'); |
| 7 | constructor(values = []) { |
| 8 | this.values = values; |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | | * @param {List} other |
| 13 | | * @return {List} |
| 12 | * Appends another list to this list |
| 13 | * @param {List} other - The list to append |
| 14 | * @return {List} A new list containing elements from both lists |
| 14 | 15 | */ |
| 15 | 16 | append(other) { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 17 | // Edge Case: Appending an empty list should return a copy of the original list |
| 18 | const result = []; |
| 19 | for (let i = 0; i < this.values.length; i++) { |
| 20 | result.push(this.values[i]); |
| 21 | } |
| 22 | for (let i = 0; i < other.values.length; i++) { |
| 23 | result.push(other.values[i]); |
| 24 | } |
| 25 | return new List(result); |
| 26 | // Handled Edge Cases: Appending empty lists |
| 17 | 27 | } |
| 18 | 28 | |
| 19 | 29 | /** |
| 20 | | * @param {List} others |
| 21 | | * @return {List} |
| 30 | * Concatenates multiple lists into one |
| 31 | * @param {List} others - A list of lists to concatenate |
| 32 | * @return {List} A new list with all elements flattened |
| 22 | 33 | */ |
| 23 | 34 | concat(others) { |
| 24 | | throw new Error('Remove this line and implement the function'); |
| 35 | let result = []; |
| 36 | |
| 37 | // Add elements from current list |
| 38 | for (let i = 0; i < this.values.length; i++) { |
| 39 | result.push(this.values[i]); |
| 40 | } |
| 41 | |
| 42 | // Add elements from each list in others |
| 43 | for (let i = 0; i < others.values.length; i++) { |
| 44 | const list = others.values[i]; |
| 45 | for (let j = 0; j < list.values.length; j++) { |
| 46 | result.push(list.values[j]); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return new List(result); |
| 51 | // Handled Edge Cases: Concatenating with empty lists |
| 25 | 52 | } |
| 26 | 53 | |
| 27 | 54 | /** |
| 28 | | * @param {(any) => boolean} predicate |
| 29 | | * @return {List} |
| 55 | * Filters the list based on a predicate function |
| 56 | * @param {(any) => boolean} predicate - Function to test each element |
| 57 | * @return {List} A new list with elements that pass the predicate test |
| 30 | 58 | */ |
| 31 | 59 | filter(predicate) { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 60 | const result = []; |
| 61 | |
| 62 | // Edge Case: Empty list should return empty list |
| 63 | for (let i = 0; i < this.values.length; i++) { |
| 64 | if (predicate(this.values[i])) { |
| 65 | result.push(this.values[i]); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return new List(result); |
| 70 | // Handled Edge Cases: Empty list, all elements match, no elements match |
| 33 | 71 | } |
| 34 | 72 | |
| 35 | 73 | /** |
| 36 | | * @param {(any) => any} expression |
| 37 | | * @return {List} |
| 74 | * Maps each element using a provided function |
| 75 | * @param {(any) => any} expression - Function to transform each element |
| 76 | * @return {List} A new list with transformed elements |
| 38 | 77 | */ |
| 39 | 78 | map(expression) { |
| 40 | | throw new Error('Remove this line and implement the function'); |
| 79 | const result = []; |
| 80 | |
| 81 | // Edge Case: Empty list should return empty list |
| 82 | for (let i = 0; i < this.values.length; i++) { |
| 83 | result.push(expression(this.values[i])); |
| 84 | } |
| 85 | |
| 86 | return new List(result); |
| 87 | // Handled Edge Cases: Empty list |
| 41 | 88 | } |
| 42 | 89 | |
| 43 | 90 | /** |
| 44 | | * @return {number} |
| 91 | * Returns the length of the list |
| 92 | * @return {number} The number of elements in the list |
| 45 | 93 | */ |
| 46 | 94 | length() { |
| 47 | | throw new Error('Remove this line and implement the function'); |
| 95 | // Edge Case: Empty list should return 0 |
| 96 | let count = 0; |
| 97 | for (let i = 0; i < this.values.length; i++) { |
| 98 | count++; |
| 99 | } |
| 100 | return count; |
| 101 | // Handled Edge Cases: Empty list |
| 48 | 102 | } |
| 49 | 103 | |
| 50 | 104 | /** |
| 51 | | * @param {(acc: any, el: any) => any} callback |
| 52 | | * @param {any} initial |
| 53 | | * @return {any} |
| 105 | * Folds the list from the left using a callback function |
| 106 | * @param {(acc: any, el: any) => any} callback - Function to apply to accumulator and element |
| 107 | * @param {any} initial - Initial value for the accumulator |
| 108 | * @return {any} The final accumulated value |
| 54 | 109 | */ |
| 55 | 110 | foldl(callback, initial) { |
| 56 | | throw new Error('Remove this line and implement the function'); |
| 111 | let accumulator = initial; |
| 112 | |
| 113 | // Edge Case: Empty list should return initial value |
| 114 | for (let i = 0; i < this.values.length; i++) { |
| 115 | accumulator = callback(accumulator, this.values[i]); |
| 116 | } |
| 117 | |
| 118 | return accumulator; |
| 119 | // Handled Edge Cases: Empty list |
| 57 | 120 | } |
| 58 | 121 | |
| 59 | 122 | /** |
| 60 | | * @param {(acc: any, el: any) => any} callback |
| 61 | | * @param {any} initial |
| 62 | | * @return {any} |
| 123 | * Folds the list from the right using a callback function |
| 124 | * @param {(acc: any, el: any) => any} callback - Function to apply to accumulator and element |
| 125 | * @param {any} initial - Initial value for the accumulator |
| 126 | * @return {any} The final accumulated value |
| 63 | 127 | */ |
| 64 | 128 | foldr(callback, initial) { |
| 65 | | throw new Error('Remove this line and implement the function'); |
| 129 | let accumulator = initial; |
| 130 | |
| 131 | // Edge Case: Empty list should return initial value |
| 132 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 133 | accumulator = callback(accumulator, this.values[i]); |
| 134 | } |
| 135 | |
| 136 | return accumulator; |
| 137 | // Handled Edge Cases: Empty list |
| 66 | 138 | } |
| 67 | 139 | |
| 68 | 140 | /** |
| 69 | | * @return {List} |
| 141 | * Reverses the list |
| 142 | * @return {List} A new list with elements in reverse order |
| 70 | 143 | */ |
| 71 | 144 | reverse() { |
| 72 | | throw new Error('Remove this line and implement the function'); |
| 145 | const result = []; |
| 146 | |
| 147 | // Edge Case: Empty list should return empty list |
| 148 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 149 | result.push(this.values[i]); |
| 150 | } |
| 151 | |
| 152 | return new List(result); |
| 153 | // Handled Edge Cases: Empty list, single element list |
| 73 | 154 | } |
| 74 | 155 | } |