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