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