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