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