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