| 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 | * - When other list is empty |
| 22 | * - When this list is empty |
| 14 | 23 | */ |
| 15 | 24 | append(other) { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 25 | // Edge Case: When both lists are empty |
| 26 | if (this.length() === 0 && other.length() === 0) { |
| 27 | return new List([]); |
| 28 | } |
| 29 | |
| 30 | // Edge Case: When this list is empty |
| 31 | if (this.length() === 0) { |
| 32 | return new List([...other.values]); |
| 33 | } |
| 34 | |
| 35 | // Edge Case: When other list is empty |
| 36 | if (other.length() === 0) { |
| 37 | return new List([...this.values]); |
| 38 | } |
| 39 | |
| 40 | return new List([...this.values, ...other.values]); |
| 17 | 41 | } |
| 18 | 42 | |
| 19 | 43 | /** |
| 20 | | * @param {List} others |
| 21 | | * @return {List} |
| 44 | * Concatenates a series of lists into one flattened list. |
| 45 | * @param {List} others - A list containing other lists to concatenate. |
| 46 | * @return {List} A new List with all items from all lists. |
| 47 | * |
| 48 | * Handled Edge Cases: |
| 49 | * - When others list is empty |
| 50 | * - When some contained lists are empty |
| 22 | 51 | */ |
| 23 | 52 | concat(others) { |
| 24 | | throw new Error('Remove this line and implement the function'); |
| 53 | let result = [...this.values]; |
| 54 | |
| 55 | // Edge Case: When others list is empty |
| 56 | if (others.length() === 0) { |
| 57 | return new List(result); |
| 58 | } |
| 59 | |
| 60 | // Process each list in others |
| 61 | for (let i = 0; i < others.length(); i++) { |
| 62 | const list = others.values[i]; |
| 63 | // Edge Case: When a contained list is null or undefined |
| 64 | if (list && typeof list.values !== 'undefined') { |
| 65 | result = [...result, ...list.values]; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return new List(result); |
| 25 | 70 | } |
| 26 | 71 | |
| 27 | 72 | /** |
| 28 | | * @param {(any) => boolean} predicate |
| 29 | | * @return {List} |
| 73 | * Filters the list based on a predicate function. |
| 74 | * @param {(any) => boolean} predicate - Function to test each item. |
| 75 | * @return {List} A new List containing only items for which predicate returns true. |
| 76 | * |
| 77 | * Handled Edge Cases: |
| 78 | * - When this list is empty |
| 79 | * - When no items match the predicate |
| 80 | * - When all items match the predicate |
| 30 | 81 | */ |
| 31 | 82 | filter(predicate) { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 83 | // Edge Case: When this list is empty |
| 84 | if (this.length() === 0) { |
| 85 | return new List([]); |
| 86 | } |
| 87 | |
| 88 | const result = []; |
| 89 | for (let i = 0; i < this.values.length; i++) { |
| 90 | if (predicate(this.values[i])) { |
| 91 | result.push(this.values[i]); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // Edge Case: When no items match (result will be empty array) |
| 96 | // Edge Case: When all items match (result will be copy of original) |
| 97 | return new List(result); |
| 33 | 98 | } |
| 34 | 99 | |
| 35 | 100 | /** |
| 36 | | * @param {(any) => any} expression |
| 37 | | * @return {List} |
| 101 | * Maps each item in the list using a provided function. |
| 102 | * @param {(any) => any} expression - Function to apply to each item. |
| 103 | * @return {List} A new List containing the results of applying expression to each item. |
| 104 | * |
| 105 | * Handled Edge Cases: |
| 106 | * - When this list is empty |
| 38 | 107 | */ |
| 39 | 108 | map(expression) { |
| 40 | | throw new Error('Remove this line and implement the function'); |
| 109 | // Edge Case: When this list is empty |
| 110 | if (this.length() === 0) { |
| 111 | return new List([]); |
| 112 | } |
| 113 | |
| 114 | const result = []; |
| 115 | for (let i = 0; i < this.values.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 count of items in the list. |
| 125 | * |
| 126 | * Handled Edge Cases: |
| 127 | * - When list is empty |
| 45 | 128 | */ |
| 46 | 129 | length() { |
| 47 | | throw new Error('Remove this line and implement the function'); |
| 130 | // Edge Case: When list is empty |
| 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 with accumulator. |
| 146 | * @param {any} initial - Initial value for the accumulator. |
| 147 | * @return {any} The final accumulated value. |
| 148 | * |
| 149 | * Handled Edge Cases: |
| 150 | * - When this list is empty (returns initial value) |
| 54 | 151 | */ |
| 55 | 152 | foldl(callback, initial) { |
| 56 | | throw new Error('Remove this line and implement the function'); |
| 153 | // Edge Case: When this list is empty |
| 154 | if (this.length() === 0) { |
| 155 | return initial; |
| 156 | } |
| 157 | |
| 158 | let accumulator = initial; |
| 159 | for (let i = 0; i < this.values.length; i++) { |
| 160 | accumulator = callback(accumulator, this.values[i]); |
| 161 | } |
| 162 | |
| 163 | return accumulator; |
| 57 | 164 | } |
| 58 | 165 | |
| 59 | 166 | /** |
| 60 | | * @param {(acc: any, el: any) => any} callback |
| 61 | | * @param {any} initial |
| 62 | | * @return {any} |
| 167 | * Folds (reduces) the list from the right using a callback function. |
| 168 | * @param {(acc: any, el: any) => any} callback - Function to apply to each item with accumulator. |
| 169 | * @param {any} initial - Initial value for the accumulator. |
| 170 | * @return {any} The final accumulated value. |
| 171 | * |
| 172 | * Handled Edge Cases: |
| 173 | * - When this list is empty (returns initial value) |
| 63 | 174 | */ |
| 64 | 175 | foldr(callback, initial) { |
| 65 | | throw new Error('Remove this line and implement the function'); |
| 176 | // Edge Case: When this list is empty |
| 177 | if (this.length() === 0) { |
| 178 | return initial; |
| 179 | } |
| 180 | |
| 181 | let accumulator = initial; |
| 182 | // Process from right to left |
| 183 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 184 | accumulator = callback(accumulator, this.values[i]); |
| 185 | } |
| 186 | |
| 187 | return accumulator; |
| 66 | 188 | } |
| 67 | 189 | |
| 68 | 190 | /** |
| 69 | | * @return {List} |
| 191 | * Returns a new list with all items in reverse order. |
| 192 | * @return {List} A new List with items in reversed order. |
| 193 | * |
| 194 | * Handled Edge Cases: |
| 195 | * - When this list is empty |
| 196 | * - When this list has only one item |
| 70 | 197 | */ |
| 71 | 198 | reverse() { |
| 72 | | throw new Error('Remove this line and implement the function'); |
| 199 | // Edge Case: When this list is empty |
| 200 | if (this.length() === 0) { |
| 201 | return new List([]); |
| 202 | } |
| 203 | |
| 204 | // Edge Case: When this list has only one item |
| 205 | if (this.length() === 1) { |
| 206 | return new List([this.values[0]]); |
| 207 | } |
| 208 | |
| 209 | const result = []; |
| 210 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 211 | result.push(this.values[i]); |
| 212 | } |
| 213 | |
| 214 | return new List(result); |
| 73 | 215 | } |
| 74 | 216 | } |