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