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