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