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