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