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