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