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