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