| 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.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 flattened. |
| 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.length === 0) { |
| 64 | return new List([]); |
| 65 | } |
| 66 | |
| 67 | // Edge Case: Handling invalid predicate |
| 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 containing 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.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 list |
| 112 | let count = 0; |
| 113 | for (const _ of this.values) { |
| 114 | count++; |
| 115 | } |
| 116 | return count; |
| 48 | 117 | } |
| 49 | 118 | |
| 50 | 119 | /** |
| 51 | | * @param {(acc: any, el: any) => any} callback |
| 52 | | * @param {any} initial |
| 53 | | * @return {any} |
| 120 | * Folds (reduces) the list from the left using a callback function. |
| 121 | * @param {(acc: any, el: any) => any} callback - Function to apply to each item. |
| 122 | * @param {any} initial - Initial accumulator value. |
| 123 | * @return {any} The final accumulator value. |
| 54 | 124 | */ |
| 55 | 125 | foldl(callback, initial) { |
| 56 | | throw new Error('Remove this line and implement the function'); |
| 126 | // Edge Case: Folding an empty list returns the initial value |
| 127 | if (this.values.length === 0) { |
| 128 | return initial; |
| 129 | } |
| 130 | |
| 131 | // Edge Case: Handling invalid callback |
| 132 | if (typeof callback !== 'function') { |
| 133 | throw new Error('Callback must be a function'); |
| 134 | } |
| 135 | |
| 136 | let accumulator = initial; |
| 137 | for (const item of this.values) { |
| 138 | accumulator = callback(accumulator, item); |
| 139 | } |
| 140 | |
| 141 | return accumulator; |
| 57 | 142 | } |
| 58 | 143 | |
| 59 | 144 | /** |
| 60 | | * @param {(acc: any, el: any) => any} callback |
| 61 | | * @param {any} initial |
| 62 | | * @return {any} |
| 145 | * Folds (reduces) the list from the right using a callback function. |
| 146 | * @param {(acc: any, el: any) => any} callback - Function to apply to each item. |
| 147 | * @param {any} initial - Initial accumulator value. |
| 148 | * @return {any} The final accumulator value. |
| 63 | 149 | */ |
| 64 | 150 | foldr(callback, initial) { |
| 65 | | throw new Error('Remove this line and implement the function'); |
| 151 | // Edge Case: Folding an empty list returns the initial value |
| 152 | if (this.values.length === 0) { |
| 153 | return initial; |
| 154 | } |
| 155 | |
| 156 | // Edge Case: Handling invalid callback |
| 157 | if (typeof callback !== 'function') { |
| 158 | throw new Error('Callback must be a function'); |
| 159 | } |
| 160 | |
| 161 | let accumulator = initial; |
| 162 | // Process from right to left |
| 163 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 164 | accumulator = callback(accumulator, this.values[i]); |
| 165 | } |
| 166 | |
| 167 | return accumulator; |
| 66 | 168 | } |
| 67 | 169 | |
| 68 | 170 | /** |
| 69 | | * @return {List} |
| 171 | * Returns a new list with all items in reversed order. |
| 172 | * @return {List} A new list with items in reversed order. |
| 70 | 173 | */ |
| 71 | 174 | reverse() { |
| 72 | | throw new Error('Remove this line and implement the function'); |
| 175 | // Edge Case: Reversing an empty list |
| 176 | if (this.values.length === 0) { |
| 177 | return new List([]); |
| 178 | } |
| 179 | |
| 180 | const result = []; |
| 181 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 182 | result.push(this.values[i]); |
| 183 | } |
| 184 | |
| 185 | return new List(result); |
| 73 | 186 | } |
| 74 | 187 | } |