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