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