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