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