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