| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | export class List { |
| 7 | | constructor() { |
| 8 | | throw new Error('Remove this line and implement the function'); |
| 7 | /** |
| 8 | * Creates a new List instance. |
| 9 | * @param {Array} [values=[]] - Initial values for the list. |
| 10 | */ |
| 11 | constructor(values = []) { |
| 12 | this.values = values; |
| 9 | 13 | } |
| 10 | 14 | |
| 11 | 15 | /** |
| 12 | | * @param {List} other |
| 13 | | * @return {List} |
| 16 | * Appends another list to this list. |
| 17 | * @param {List} other - The list to append. |
| 18 | * @return {List} A new list containing elements from both lists. |
| 19 | * |
| 20 | * Handled Edge Cases: Appending an empty list, appending to an empty list |
| 14 | 21 | */ |
| 15 | 22 | append(other) { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 23 | // Edge Case: Appending an empty list |
| 24 | if (other.length() === 0) { |
| 25 | return new List(this.values); |
| 26 | } |
| 27 | |
| 28 | // Edge Case: Appending to an empty list |
| 29 | if (this.length() === 0) { |
| 30 | return new List(other.values); |
| 31 | } |
| 32 | |
| 33 | const combined = []; |
| 34 | for (let i = 0; i < this.length(); i++) { |
| 35 | combined.push(this.values[i]); |
| 36 | } |
| 37 | for (let i = 0; i < other.length(); i++) { |
| 38 | combined.push(other.values[i]); |
| 39 | } |
| 40 | return new List(combined); |
| 41 | |
| 42 | // Handled Edge Cases: Appending an empty list, appending to an empty list |
| 17 | 43 | } |
| 18 | 44 | |
| 19 | 45 | /** |
| 20 | | * @param {List} others |
| 21 | | * @return {List} |
| 46 | * Concatenates multiple lists into one. |
| 47 | * @param {List} others - A list of lists to concatenate. |
| 48 | * @return {List} A new list with all elements from all lists. |
| 49 | * |
| 50 | * Handled Edge Cases: Concatenating with an empty list of lists, concatenating empty lists |
| 22 | 51 | */ |
| 23 | 52 | concat(others) { |
| 24 | | throw new Error('Remove this line and implement the function'); |
| 53 | // Edge Case: Concatenating with an empty list of lists |
| 54 | if (others.length() === 0) { |
| 55 | return new List(this.values); |
| 56 | } |
| 57 | |
| 58 | let result = new List(this.values); |
| 59 | for (let i = 0; i < others.length(); i++) { |
| 60 | result = result.append(others.values[i]); |
| 61 | } |
| 62 | return result; |
| 63 | |
| 64 | // Handled Edge Cases: Concatenating with an empty list of lists, concatenating empty lists |
| 25 | 65 | } |
| 26 | 66 | |
| 27 | 67 | /** |
| 28 | | * @param {(any) => boolean} predicate |
| 29 | | * @return {List} |
| 68 | * Filters the list based on a predicate function. |
| 69 | * @param {(any) => boolean} predicate - Function to test each element. |
| 70 | * @return {List} A new list with elements that pass the predicate test. |
| 71 | * |
| 72 | * Handled Edge Cases: Empty list, no elements match the predicate, all elements match the predicate |
| 30 | 73 | */ |
| 31 | 74 | filter(predicate) { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 75 | // Edge Case: Empty list |
| 76 | if (this.length() === 0) { |
| 77 | return new List([]); |
| 78 | } |
| 79 | |
| 80 | const filtered = []; |
| 81 | for (let i = 0; i < this.length(); i++) { |
| 82 | if (predicate(this.values[i])) { |
| 83 | filtered.push(this.values[i]); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Edge Case: No elements match the predicate |
| 88 | // Edge Case: All elements match the predicate |
| 89 | return new List(filtered); |
| 90 | |
| 91 | // Handled Edge Cases: Empty list, no elements match the predicate, all elements match the predicate |
| 33 | 92 | } |
| 34 | 93 | |
| 35 | 94 | /** |
| 36 | | * @param {(any) => any} expression |
| 37 | | * @return {List} |
| 95 | * Transforms each element in the list using a mapping function. |
| 96 | * @param {(any) => any} expression - Function to transform each element. |
| 97 | * @return {List} A new list with transformed elements. |
| 98 | * |
| 99 | * Handled Edge Cases: Empty list, mapping function returns undefined or null |
| 38 | 100 | */ |
| 39 | 101 | map(expression) { |
| 40 | | throw new Error('Remove this line and implement the function'); |
| 102 | // Edge Case: Empty list |
| 103 | if (this.length() === 0) { |
| 104 | return new List([]); |
| 105 | } |
| 106 | |
| 107 | const mapped = []; |
| 108 | for (let i = 0; i < this.length(); i++) { |
| 109 | mapped.push(expression(this.values[i])); |
| 110 | } |
| 111 | |
| 112 | // Edge Case: Mapping function returns undefined or null |
| 113 | return new List(mapped); |
| 114 | |
| 115 | // Handled Edge Cases: Empty list, mapping function returns undefined or null |
| 41 | 116 | } |
| 42 | 117 | |
| 43 | 118 | /** |
| 44 | | * @return {number} |
| 119 | * Returns the number of elements in the list. |
| 120 | * @return {number} The length of the list. |
| 121 | * |
| 122 | * Handled Edge Cases: Empty list |
| 45 | 123 | */ |
| 46 | 124 | length() { |
| 47 | | throw new Error('Remove this line and implement the function'); |
| 125 | // Edge Case: Empty list |
| 126 | let count = 0; |
| 127 | for (let i = 0; i < this.values.length; i++) { |
| 128 | count++; |
| 129 | } |
| 130 | return count; |
| 131 | |
| 132 | // Handled Edge Cases: Empty list |
| 48 | 133 | } |
| 49 | 134 | |
| 50 | 135 | /** |
| 51 | | * @param {(acc: any, el: any) => any} callback |
| 52 | | * @param {any} initial |
| 53 | | * @return {any} |
| 136 | * Folds the list from the left using a callback function. |
| 137 | * @param {(acc: any, el: any) => any} callback - Function to apply to each element. |
| 138 | * @param {any} initial - Initial accumulator value. |
| 139 | * @return {any} The final accumulator value. |
| 140 | * |
| 141 | * Handled Edge Cases: Empty list |
| 54 | 142 | */ |
| 55 | 143 | foldl(callback, initial) { |
| 56 | | throw new Error('Remove this line and implement the function'); |
| 144 | // Edge Case: Empty list |
| 145 | if (this.length() === 0) { |
| 146 | return initial; |
| 147 | } |
| 148 | |
| 149 | let accumulator = initial; |
| 150 | for (let i = 0; i < this.length(); i++) { |
| 151 | accumulator = callback(accumulator, this.values[i]); |
| 152 | } |
| 153 | return accumulator; |
| 154 | |
| 155 | // Handled Edge Cases: Empty list |
| 57 | 156 | } |
| 58 | 157 | |
| 59 | 158 | /** |
| 60 | | * @param {(acc: any, el: any) => any} callback |
| 61 | | * @param {any} initial |
| 62 | | * @return {any} |
| 159 | * Folds the list from the right using a callback function. |
| 160 | * @param {(acc: any, el: any) => any} callback - Function to apply to each element. |
| 161 | * @param {any} initial - Initial accumulator value. |
| 162 | * @return {any} The final accumulator value. |
| 163 | * |
| 164 | * Handled Edge Cases: Empty list |
| 63 | 165 | */ |
| 64 | 166 | foldr(callback, initial) { |
| 65 | | throw new Error('Remove this line and implement the function'); |
| 167 | // Edge Case: Empty list |
| 168 | if (this.length() === 0) { |
| 169 | return initial; |
| 170 | } |
| 171 | |
| 172 | let accumulator = initial; |
| 173 | for (let i = this.length() - 1; i >= 0; i--) { |
| 174 | accumulator = callback(accumulator, this.values[i]); |
| 175 | } |
| 176 | return accumulator; |
| 177 | |
| 178 | // Handled Edge Cases: Empty list |
| 66 | 179 | } |
| 67 | 180 | |
| 68 | 181 | /** |
| 69 | | * @return {List} |
| 182 | * Reverses the order of elements in the list. |
| 183 | * @return {List} A new list with elements in reverse order. |
| 184 | * |
| 185 | * Handled Edge Cases: Empty list, single element list |
| 70 | 186 | */ |
| 71 | 187 | reverse() { |
| 72 | | throw new Error('Remove this line and implement the function'); |
| 188 | // Edge Case: Empty list |
| 189 | if (this.length() === 0) { |
| 190 | return new List([]); |
| 191 | } |
| 192 | |
| 193 | // Edge Case: Single element list |
| 194 | if (this.length() === 1) { |
| 195 | return new List([this.values[0]]); |
| 196 | } |
| 197 | |
| 198 | const reversed = []; |
| 199 | for (let i = this.length() - 1; i >= 0; i--) { |
| 200 | reversed.push(this.values[i]); |
| 201 | } |
| 202 | return new List(reversed); |
| 203 | |
| 204 | // Handled Edge Cases: Empty list, single element list |
| 73 | 205 | } |
| 74 | 206 | } |