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