| 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 | // Edge Case: Handle when values is not an array 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 |
| 14 | 20 | */ |
| 15 | 21 | append(other) { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 22 | // Edge Case: Handle when other is not a List instance |
| 23 | if (!(other instanceof List)) { |
| 24 | throw new Error('Argument must be a List instance'); |
| 25 | } |
| 26 | |
| 27 | return new List(this.values.concat(other.values)); |
| 17 | 28 | } |
| 18 | 29 | |
| 19 | 30 | /** |
| 20 | | * @param {List} others |
| 21 | | * @return {List} |
| 31 | * Concatenates a series of lists into one flattened list |
| 32 | * @param {List} others - A list containing other lists to concatenate |
| 33 | * @return {List} A new List with all items from all lists flattened |
| 22 | 34 | */ |
| 23 | 35 | concat(others) { |
| 24 | | throw new Error('Remove this line and implement the function'); |
| 36 | // Edge Case: Handle when others is not a List instance |
| 37 | if (!(others instanceof List)) { |
| 38 | throw new Error('Argument must be a List instance'); |
| 39 | } |
| 40 | |
| 41 | let result = this.values.slice(); // Copy current values |
| 42 | |
| 43 | // Edge Case: Handle when others contains non-List elements |
| 44 | for (const item of others.values) { |
| 45 | if (item instanceof List) { |
| 46 | result = result.concat(item.values); |
| 47 | } else { |
| 48 | result.push(item); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return new List(result); |
| 25 | 53 | } |
| 26 | 54 | |
| 27 | 55 | /** |
| 28 | | * @param {(any) => boolean} predicate |
| 29 | | * @return {List} |
| 56 | * Filters the list based on a predicate function |
| 57 | * @param {(any) => boolean} predicate - Function to test each element |
| 58 | * @return {List} A new List containing only elements that satisfy the predicate |
| 30 | 59 | */ |
| 31 | 60 | filter(predicate) { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 61 | // Edge Case: Handle when predicate is not a function |
| 62 | if (typeof predicate !== 'function') { |
| 63 | throw new Error('Predicate must be a function'); |
| 64 | } |
| 65 | |
| 66 | const result = []; |
| 67 | |
| 68 | // Edge Case: Handle empty list |
| 69 | if (this.values.length === 0) { |
| 70 | return new List([]); |
| 71 | } |
| 72 | |
| 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 element in the list using the provided expression function |
| 84 | * @param {(any) => any} expression - Function to transform each element |
| 85 | * @return {List} A new List containing transformed elements |
| 38 | 86 | */ |
| 39 | 87 | map(expression) { |
| 40 | | throw new Error('Remove this line and implement the function'); |
| 88 | // Edge Case: Handle when expression is not a function |
| 89 | if (typeof expression !== 'function') { |
| 90 | throw new Error('Expression must be a function'); |
| 91 | } |
| 92 | |
| 93 | const result = []; |
| 94 | |
| 95 | // Edge Case: Handle empty list |
| 96 | if (this.values.length === 0) { |
| 97 | return new List([]); |
| 98 | } |
| 99 | |
| 100 | for (const item of this.values) { |
| 101 | result.push(expression(item)); |
| 102 | } |
| 103 | |
| 104 | return new List(result); |
| 41 | 105 | } |
| 42 | 106 | |
| 43 | 107 | /** |
| 44 | | * @return {number} |
| 108 | * Returns the length of the list |
| 109 | * @return {number} The number of items in the list |
| 45 | 110 | */ |
| 46 | 111 | length() { |
| 47 | | throw new Error('Remove this line and implement the function'); |
| 112 | // Edge Case: Handle when values is not properly initialized |
| 113 | if (!Array.isArray(this.values)) { |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | let count = 0; |
| 118 | for (const _ of this.values) { |
| 119 | count++; |
| 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 element |
| 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: Handle when callback is not a function |
| 132 | if (typeof callback !== 'function') { |
| 133 | throw new Error('Callback must be a function'); |
| 134 | } |
| 135 | |
| 136 | let accumulator = initial; |
| 137 | |
| 138 | // Edge Case: Handle empty list |
| 139 | if (this.values.length === 0) { |
| 140 | return accumulator; |
| 141 | } |
| 142 | |
| 143 | for (const item of this.values) { |
| 144 | accumulator = callback(accumulator, item); |
| 145 | } |
| 146 | |
| 147 | return accumulator; |
| 57 | 148 | } |
| 58 | 149 | |
| 59 | 150 | /** |
| 60 | | * @param {(acc: any, el: any) => any} callback |
| 61 | | * @param {any} initial |
| 62 | | * @return {any} |
| 151 | * Folds (reduces) the list from the right using a callback function |
| 152 | * @param {(acc: any, el: any) => any} callback - Function to apply to each element |
| 153 | * @param {any} initial - Initial accumulator value |
| 154 | * @return {any} The final accumulator value |
| 63 | 155 | */ |
| 64 | 156 | foldr(callback, initial) { |
| 65 | | throw new Error('Remove this line and implement the function'); |
| 157 | // Edge Case: Handle when callback is not a function |
| 158 | if (typeof callback !== 'function') { |
| 159 | throw new Error('Callback must be a function'); |
| 160 | } |
| 161 | |
| 162 | let accumulator = initial; |
| 163 | |
| 164 | // Edge Case: Handle empty list |
| 165 | if (this.values.length === 0) { |
| 166 | return accumulator; |
| 167 | } |
| 168 | |
| 169 | // Process from right to left |
| 170 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 171 | accumulator = callback(accumulator, this.values[i]); |
| 172 | } |
| 173 | |
| 174 | return accumulator; |
| 66 | 175 | } |
| 67 | 176 | |
| 68 | 177 | /** |
| 69 | | * @return {List} |
| 178 | * Returns a new list with elements in reversed order |
| 179 | * @return {List} A new List with elements in reversed order |
| 70 | 180 | */ |
| 71 | 181 | reverse() { |
| 72 | | throw new Error('Remove this line and implement the function'); |
| 182 | const result = []; |
| 183 | |
| 184 | // Edge Case: Handle empty list |
| 185 | if (this.values.length === 0) { |
| 186 | return new List([]); |
| 187 | } |
| 188 | |
| 189 | // Build reversed array |
| 190 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 191 | result.push(this.values[i]); |
| 192 | } |
| 193 | |
| 194 | return new List(result); |
| 73 | 195 | } |
| 196 | |
| 197 | // Handled Edge Cases: Empty list, non-function callbacks/predicates/expressions, non-List arguments, non-array values |
| 74 | 198 | } |