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