| 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 |
| 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 all elements from both lists |
| 19 | * |
| 20 | * Handled Edge Cases: empty lists, null/undefined other list |
| 14 | 21 | */ |
| 15 | 22 | append(other) { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 23 | // Edge Case: other is null or undefined |
| 24 | if (other === null || other === undefined) { |
| 25 | return new List(this.values); |
| 26 | } |
| 27 | |
| 28 | // Edge Case: both lists are empty |
| 29 | if (this.values.length === 0 && other.values.length === 0) { |
| 30 | return new List([]); |
| 31 | } |
| 32 | |
| 33 | // Edge Case: this list is empty |
| 34 | if (this.values.length === 0) { |
| 35 | return new List(other.values); |
| 36 | } |
| 37 | |
| 38 | // Edge Case: other list is empty |
| 39 | if (other.values.length === 0) { |
| 40 | return new List(this.values); |
| 41 | } |
| 42 | |
| 43 | // Manual concatenation without using spread operator |
| 44 | const result = []; |
| 45 | for (let i = 0; i < this.values.length; i++) { |
| 46 | result[i] = this.values[i]; |
| 47 | } |
| 48 | for (let j = 0; j < other.values.length; j++) { |
| 49 | result[this.values.length + j] = other.values[j]; |
| 50 | } |
| 51 | return new List(result); |
| 17 | 52 | } |
| 18 | 53 | |
| 19 | 54 | /** |
| 20 | | * @param {List} others |
| 21 | | * @return {List} |
| 55 | * Concatenates multiple lists into one |
| 56 | * @param {List} others - A list of lists to concatenate |
| 57 | * @return {List} A new list with all elements from all lists flattened |
| 58 | * |
| 59 | * Handled Edge Cases: empty lists, null/undefined others, empty nested lists |
| 22 | 60 | */ |
| 23 | 61 | concat(others) { |
| 24 | | throw new Error('Remove this line and implement the function'); |
| 62 | // Edge Case: others is null or undefined |
| 63 | if (others === null || others === undefined) { |
| 64 | return new List(this.values); |
| 65 | } |
| 66 | |
| 67 | // Manual copy of this.values |
| 68 | const result = []; |
| 69 | for (let i = 0; i < this.values.length; i++) { |
| 70 | result[i] = this.values[i]; |
| 71 | } |
| 72 | |
| 73 | // Edge Case: others is empty |
| 74 | if (others.values.length === 0) { |
| 75 | return new List(result); |
| 76 | } |
| 77 | |
| 78 | for (const list of others.values) { |
| 79 | // Edge Case: nested list is null or undefined |
| 80 | if (list !== null && list !== undefined && Array.isArray(list.values)) { |
| 81 | // Manual concatenation of each list |
| 82 | const currentLength = result.length; |
| 83 | for (let j = 0; j < list.values.length; j++) { |
| 84 | result[currentLength + j] = list.values[j]; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return new List(result); |
| 25 | 90 | } |
| 26 | 91 | |
| 27 | 92 | /** |
| 28 | | * @param {(any) => boolean} predicate |
| 29 | | * @return {List} |
| 93 | * Filters the list based on a predicate function |
| 94 | * @param {(any) => boolean} predicate - Function to test each element |
| 95 | * @return {List} A new list containing only elements that pass the predicate |
| 96 | * |
| 97 | * Handled Edge Cases: empty list, null/undefined predicate, predicate throws error |
| 30 | 98 | */ |
| 31 | 99 | filter(predicate) { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 100 | // Edge Case: empty list |
| 101 | if (this.values.length === 0) { |
| 102 | return new List([]); |
| 103 | } |
| 104 | |
| 105 | // Edge Case: predicate is null or undefined |
| 106 | if (predicate === null || predicate === undefined) { |
| 107 | return new List([]); |
| 108 | } |
| 109 | |
| 110 | const result = []; |
| 111 | let resultIndex = 0; |
| 112 | for (const item of this.values) { |
| 113 | try { |
| 114 | if (predicate(item)) { |
| 115 | result[resultIndex] = item; |
| 116 | resultIndex++; |
| 117 | } |
| 118 | } catch (error) { |
| 119 | // Edge Case: predicate throws an error - skip the item |
| 120 | continue; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return new List(result); |
| 33 | 125 | } |
| 34 | 126 | |
| 35 | 127 | /** |
| 36 | | * @param {(any) => any} expression |
| 37 | | * @return {List} |
| 128 | * Maps each element using a provided function |
| 129 | * @param {(any) => any} expression - Function to transform each element |
| 130 | * @return {List} A new list with transformed elements |
| 131 | * |
| 132 | * Handled Edge Cases: empty list, null/undefined expression, expression throws error |
| 38 | 133 | */ |
| 39 | 134 | map(expression) { |
| 40 | | throw new Error('Remove this line and implement the function'); |
| 135 | // Edge Case: empty list |
| 136 | if (this.values.length === 0) { |
| 137 | return new List([]); |
| 138 | } |
| 139 | |
| 140 | // Edge Case: expression is null or undefined |
| 141 | if (expression === null || expression === undefined) { |
| 142 | return new List([]); |
| 143 | } |
| 144 | |
| 145 | const result = []; |
| 146 | for (let i = 0; i < this.values.length; i++) { |
| 147 | try { |
| 148 | result[i] = expression(this.values[i]); |
| 149 | } catch (error) { |
| 150 | // Edge Case: expression throws an error - push undefined or handle as needed |
| 151 | result[i] = undefined; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return new List(result); |
| 41 | 156 | } |
| 42 | 157 | |
| 43 | 158 | /** |
| 44 | | * @return {number} |
| 159 | * Returns the length of the list |
| 160 | * @return {number} The number of elements in the list |
| 161 | * |
| 162 | * Handled Edge Cases: empty list |
| 45 | 163 | */ |
| 46 | 164 | length() { |
| 47 | | throw new Error('Remove this line and implement the function'); |
| 165 | // Edge Case: empty list |
| 166 | return this.values.length; |
| 48 | 167 | } |
| 49 | 168 | |
| 50 | 169 | /** |
| 51 | | * @param {(acc: any, el: any) => any} callback |
| 52 | | * @param {any} initial |
| 53 | | * @return {any} |
| 170 | * Folds the list from the left using a callback function |
| 171 | * @param {(acc: any, el: any) => any} callback - Function to apply to each element |
| 172 | * @param {any} initial - Initial accumulator value |
| 173 | * @return {any} The final accumulator value |
| 174 | * |
| 175 | * Handled Edge Cases: empty list, null/undefined callback, callback throws error |
| 54 | 176 | */ |
| 55 | 177 | foldl(callback, initial) { |
| 56 | | throw new Error('Remove this line and implement the function'); |
| 178 | // Edge Case: callback is null or undefined |
| 179 | if (callback === null || callback === undefined) { |
| 180 | return initial; |
| 181 | } |
| 182 | |
| 183 | let accumulator = initial; |
| 184 | |
| 185 | // Edge Case: empty list |
| 186 | if (this.values.length === 0) { |
| 187 | return accumulator; |
| 188 | } |
| 189 | |
| 190 | for (const item of this.values) { |
| 191 | try { |
| 192 | accumulator = callback(accumulator, item); |
| 193 | } catch (error) { |
| 194 | // Edge Case: callback throws an error - continue with current accumulator |
| 195 | continue; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return accumulator; |
| 57 | 200 | } |
| 58 | 201 | |
| 59 | 202 | /** |
| 60 | | * @param {(acc: any, el: any) => any} callback |
| 61 | | * @param {any} initial |
| 62 | | * @return {any} |
| 203 | * Folds the list from the right using a callback function |
| 204 | * @param {(acc: any, el: any) => any} callback - Function to apply to each element |
| 205 | * @param {any} initial - Initial accumulator value |
| 206 | * @return {any} The final accumulator value |
| 207 | * |
| 208 | * Handled Edge Cases: empty list, null/undefined callback, callback throws error |
| 63 | 209 | */ |
| 64 | 210 | foldr(callback, initial) { |
| 65 | | throw new Error('Remove this line and implement the function'); |
| 211 | // Edge Case: callback is null or undefined |
| 212 | if (callback === null || callback === undefined) { |
| 213 | return initial; |
| 214 | } |
| 215 | |
| 216 | let accumulator = initial; |
| 217 | |
| 218 | // Edge Case: empty list |
| 219 | if (this.values.length === 0) { |
| 220 | return accumulator; |
| 221 | } |
| 222 | |
| 223 | // Process from right to left |
| 224 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 225 | try { |
| 226 | accumulator = callback(accumulator, this.values[i]); |
| 227 | } catch (error) { |
| 228 | // Edge Case: callback throws an error - continue with current accumulator |
| 229 | continue; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | return accumulator; |
| 66 | 234 | } |
| 67 | 235 | |
| 68 | 236 | /** |
| 69 | | * @return {List} |
| 237 | * Reverses the list |
| 238 | * @return {List} A new list with elements in reverse order |
| 239 | * |
| 240 | * Handled Edge Cases: empty list, single element list |
| 70 | 241 | */ |
| 71 | 242 | reverse() { |
| 72 | | throw new Error('Remove this line and implement the function'); |
| 243 | // Edge Case: empty list |
| 244 | if (this.values.length === 0) { |
| 245 | return new List([]); |
| 246 | } |
| 247 | |
| 248 | // Edge Case: single element list |
| 249 | if (this.values.length === 1) { |
| 250 | return new List([this.values[0]]); |
| 251 | } |
| 252 | |
| 253 | const result = []; |
| 254 | for (let i = 0; i < this.values.length; i++) { |
| 255 | result[i] = this.values[this.values.length - 1 - i]; |
| 256 | } |
| 257 | |
| 258 | return new List(result); |
| 73 | 259 | } |
| 74 | 260 | } |