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