| 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 all items from another list to the end of this list. |
| 17 | * @param {List} other - The list to append. |
| 18 | * @return {List} A new list containing all items from both lists. |
| 19 | * Handled Edge Cases: Appending empty lists, appending to empty lists |
| 14 | 20 | */ |
| 15 | 21 | append(other) { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 22 | // Edge Case: Appending empty lists |
| 23 | if (!other || !other.values) { |
| 24 | return new List(this.values); |
| 25 | } |
| 26 | |
| 27 | const result = []; |
| 28 | for (let i = 0; i < this.values.length; i++) { |
| 29 | result.push(this.values[i]); |
| 30 | } |
| 31 | for (let i = 0; i < other.values.length; i++) { |
| 32 | result.push(other.values[i]); |
| 33 | } |
| 34 | return new List(result); |
| 17 | 35 | } |
| 18 | 36 | |
| 19 | 37 | /** |
| 20 | | * @param {List} others |
| 21 | | * @return {List} |
| 38 | * Concatenates a series of lists into one flattened list. |
| 39 | * @param {List} others - A list containing other lists to concatenate. |
| 40 | * @return {List} A new list with all items from all lists. |
| 41 | * Handled Edge Cases: Concatenating empty lists, concatenating lists with empty sublists |
| 22 | 42 | */ |
| 23 | 43 | concat(others) { |
| 24 | | throw new Error('Remove this line and implement the function'); |
| 44 | let result = []; |
| 45 | |
| 46 | // Add items from the current list |
| 47 | for (let i = 0; i < this.values.length; i++) { |
| 48 | result.push(this.values[i]); |
| 49 | } |
| 50 | |
| 51 | // Edge Case: Concatenating empty lists |
| 52 | if (!others || !others.values) { |
| 53 | return new List(result); |
| 54 | } |
| 55 | |
| 56 | // Add items from each list in others |
| 57 | for (let i = 0; i < others.values.length; i++) { |
| 58 | const sublist = others.values[i]; |
| 59 | // Edge Case: Handling null or undefined sublists |
| 60 | if (sublist && sublist.values) { |
| 61 | for (let j = 0; j < sublist.values.length; j++) { |
| 62 | result.push(sublist.values[j]); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return new List(result); |
| 25 | 68 | } |
| 26 | 69 | |
| 27 | 70 | /** |
| 28 | | * @param {(any) => boolean} predicate |
| 29 | | * @return {List} |
| 71 | * Filters the list based on a predicate function. |
| 72 | * @param {Function} predicate - Function that returns true for items to keep. |
| 73 | * @return {List} A new list containing only items for which predicate is true. |
| 74 | * Handled Edge Cases: Empty list, predicate that matches no items, predicate that matches all items |
| 30 | 75 | */ |
| 31 | 76 | filter(predicate) { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 77 | // Edge Case: Empty list |
| 78 | if (this.values.length === 0) { |
| 79 | return new List([]); |
| 80 | } |
| 81 | |
| 82 | const result = []; |
| 83 | for (let i = 0; i < this.values.length; i++) { |
| 84 | // Edge Case: Predicate function throws an error |
| 85 | try { |
| 86 | if (predicate(this.values[i])) { |
| 87 | result.push(this.values[i]); |
| 88 | } |
| 89 | } catch (e) { |
| 90 | // Silently ignore items that cause predicate to throw |
| 91 | continue; |
| 92 | } |
| 93 | } |
| 94 | return new List(result); |
| 33 | 95 | } |
| 34 | 96 | |
| 35 | 97 | /** |
| 36 | | * @param {(any) => any} expression |
| 37 | | * @return {List} |
| 98 | * Maps each item in the list using a provided function. |
| 99 | * @param {Function} expression - Function to apply to each item. |
| 100 | * @return {List} A new list with the results of applying expression to each item. |
| 101 | * Handled Edge Cases: Empty list, expression that throws for some items |
| 38 | 102 | */ |
| 39 | 103 | map(expression) { |
| 40 | | throw new Error('Remove this line and implement the function'); |
| 104 | // Edge Case: Empty list |
| 105 | if (this.values.length === 0) { |
| 106 | return new List([]); |
| 107 | } |
| 108 | |
| 109 | const result = []; |
| 110 | for (let i = 0; i < this.values.length; i++) { |
| 111 | // Edge Case: Expression function throws an error |
| 112 | try { |
| 113 | result.push(expression(this.values[i])); |
| 114 | } catch (e) { |
| 115 | // Push undefined for items that cause expression to throw |
| 116 | result.push(undefined); |
| 117 | } |
| 118 | } |
| 119 | return new List(result); |
| 41 | 120 | } |
| 42 | 121 | |
| 43 | 122 | /** |
| 44 | | * @return {number} |
| 123 | * Returns the number of items in the list. |
| 124 | * @return {number} The length of the list. |
| 125 | * Handled Edge Cases: Empty list |
| 45 | 126 | */ |
| 46 | 127 | length() { |
| 47 | | throw new Error('Remove this line and implement the function'); |
| 128 | // Edge Case: Empty list |
| 129 | let count = 0; |
| 130 | for (let i = 0; i < this.values.length; i++) { |
| 131 | count++; |
| 132 | } |
| 133 | return count; |
| 48 | 134 | } |
| 49 | 135 | |
| 50 | 136 | /** |
| 51 | | * @param {(acc: any, el: any) => any} callback |
| 52 | | * @param {any} initial |
| 53 | | * @return {any} |
| 137 | * Folds (reduces) the list from the left using a callback function. |
| 138 | * @param {Function} callback - Function to apply to each item with accumulator. |
| 139 | * @param {any} initial - Initial value for the accumulator. |
| 140 | * @return {any} The final accumulated value. |
| 141 | * Handled Edge Cases: Empty list, callback that throws |
| 54 | 142 | */ |
| 55 | 143 | foldl(callback, initial) { |
| 56 | | throw new Error('Remove this line and implement the function'); |
| 144 | let accumulator = initial; |
| 145 | |
| 146 | // Edge Case: Empty list |
| 147 | if (this.values.length === 0) { |
| 148 | return accumulator; |
| 149 | } |
| 150 | |
| 151 | for (let i = 0; i < this.values.length; i++) { |
| 152 | // Edge Case: Callback function throws an error |
| 153 | try { |
| 154 | accumulator = callback(accumulator, this.values[i]); |
| 155 | } catch (e) { |
| 156 | // Return current accumulator if callback throws |
| 157 | return accumulator; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return accumulator; |
| 57 | 162 | } |
| 58 | 163 | |
| 59 | 164 | /** |
| 60 | | * @param {(acc: any, el: any) => any} callback |
| 61 | | * @param {any} initial |
| 62 | | * @return {any} |
| 165 | * Folds (reduces) the list from the right using a callback function. |
| 166 | * @param {Function} callback - Function to apply to each item with accumulator. |
| 167 | * @param {any} initial - Initial value for the accumulator. |
| 168 | * @return {any} The final accumulated value. |
| 169 | * Handled Edge Cases: Empty list, callback that throws |
| 63 | 170 | */ |
| 64 | 171 | foldr(callback, initial) { |
| 65 | | throw new Error('Remove this line and implement the function'); |
| 172 | let accumulator = initial; |
| 173 | |
| 174 | // Edge Case: Empty list |
| 175 | if (this.values.length === 0) { |
| 176 | return accumulator; |
| 177 | } |
| 178 | |
| 179 | // Process from right to left |
| 180 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 181 | // Edge Case: Callback function throws an error |
| 182 | try { |
| 183 | accumulator = callback(accumulator, this.values[i]); |
| 184 | } catch (e) { |
| 185 | // Return current accumulator if callback throws |
| 186 | return accumulator; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return accumulator; |
| 66 | 191 | } |
| 67 | 192 | |
| 68 | 193 | /** |
| 69 | | * @return {List} |
| 194 | * Returns a new list with all items in reverse order. |
| 195 | * @return {List} A new list with items in reverse order. |
| 196 | * Handled Edge Cases: Empty list, single item list |
| 70 | 197 | */ |
| 71 | 198 | reverse() { |
| 72 | | throw new Error('Remove this line and implement the function'); |
| 199 | const result = []; |
| 200 | |
| 201 | // Edge Case: Empty list |
| 202 | if (this.values.length === 0) { |
| 203 | return new List(result); |
| 204 | } |
| 205 | |
| 206 | // Edge Case: Single item list |
| 207 | if (this.values.length === 1) { |
| 208 | return new List([this.values[0]]); |
| 209 | } |
| 210 | |
| 211 | // Build reversed list |
| 212 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 213 | result.push(this.values[i]); |
| 214 | } |
| 215 | |
| 216 | return new List(result); |
| 73 | 217 | } |
| 74 | 218 | } |