| 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 another list to this list. |
| 17 | * @param {List} other - The list to append. |
| 18 | * @return {List} A new list containing 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 when other is null or undefined |
| 24 | if (!other) { |
| 25 | return new List(this.values); |
| 26 | } |
| 27 | |
| 28 | // Edge Case: Handle when this list or other list is empty |
| 29 | if (this.values.length === 0 && other.values.length === 0) { |
| 30 | return new List([]); |
| 31 | } else if (this.values.length === 0) { |
| 32 | return new List(other.values); |
| 33 | } else if (other.values.length === 0) { |
| 34 | return new List(this.values); |
| 35 | } |
| 36 | |
| 37 | return new List([...this.values, ...other.values]); |
| 17 | 38 | } |
| 18 | 39 | |
| 19 | 40 | /** |
| 20 | | * @param {List} others |
| 21 | | * @return {List} |
| 41 | * Concatenates multiple lists into one. |
| 42 | * @param {List} others - A list of lists to concatenate. |
| 43 | * @return {List} A new list with all elements from all lists. |
| 44 | * |
| 45 | * Handled Edge Cases: empty lists, nested empty lists |
| 22 | 46 | */ |
| 23 | 47 | concat(others) { |
| 24 | | throw new Error('Remove this line and implement the function'); |
| 48 | let result = [...this.values]; |
| 49 | |
| 50 | // Edge Case: Handle when others is null or undefined |
| 51 | if (!others || !others.values) { |
| 52 | return new List(result); |
| 53 | } |
| 54 | |
| 55 | // Edge Case: Handle when others list is empty |
| 56 | if (others.values.length === 0) { |
| 57 | return new List(result); |
| 58 | } |
| 59 | |
| 60 | for (const list of others.values) { |
| 61 | // Edge Case: Handle when an element in others is not a List |
| 62 | if (list instanceof List) { |
| 63 | result = [...result, ...list.values]; |
| 64 | } else { |
| 65 | // If it's not a List, we treat it as a single element |
| 66 | result = [...result, list]; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return new List(result); |
| 25 | 71 | } |
| 26 | 72 | |
| 27 | 73 | /** |
| 28 | | * @param {(any) => boolean} predicate |
| 29 | | * @return {List} |
| 74 | * Filters the list based on a predicate function. |
| 75 | * @param {(any) => boolean} predicate - Function to test each element. |
| 76 | * @return {List} A new list with elements that pass the predicate test. |
| 77 | * |
| 78 | * Handled Edge Cases: empty list, predicate function throws error |
| 30 | 79 | */ |
| 31 | 80 | filter(predicate) { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 81 | // Edge Case: Handle when this list is empty |
| 82 | if (this.values.length === 0) { |
| 83 | return new List([]); |
| 84 | } |
| 85 | |
| 86 | // Edge Case: Handle when predicate is null or undefined |
| 87 | if (typeof predicate !== 'function') { |
| 88 | return new List([]); |
| 89 | } |
| 90 | |
| 91 | const result = []; |
| 92 | for (const item of this.values) { |
| 93 | try { |
| 94 | if (predicate(item)) { |
| 95 | result.push(item); |
| 96 | } |
| 97 | } catch (error) { |
| 98 | // Edge Case: If predicate throws an error, we skip that element |
| 99 | continue; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return new List(result); |
| 33 | 104 | } |
| 34 | 105 | |
| 35 | 106 | /** |
| 36 | | * @param {(any) => any} expression |
| 37 | | * @return {List} |
| 107 | * Maps each element using a provided function. |
| 108 | * @param {(any) => any} expression - Function to transform each element. |
| 109 | * @return {List} A new list with transformed elements. |
| 110 | * |
| 111 | * Handled Edge Cases: empty list, expression function throws error |
| 38 | 112 | */ |
| 39 | 113 | map(expression) { |
| 40 | | throw new Error('Remove this line and implement the function'); |
| 114 | // Edge Case: Handle when this list is empty |
| 115 | if (this.values.length === 0) { |
| 116 | return new List([]); |
| 117 | } |
| 118 | |
| 119 | // Edge Case: Handle when expression is null or undefined |
| 120 | if (typeof expression !== 'function') { |
| 121 | return new List([]); |
| 122 | } |
| 123 | |
| 124 | const result = []; |
| 125 | for (const item of this.values) { |
| 126 | try { |
| 127 | result.push(expression(item)); |
| 128 | } catch (error) { |
| 129 | // Edge Case: If expression throws an error, we push undefined |
| 130 | result.push(undefined); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return new List(result); |
| 41 | 135 | } |
| 42 | 136 | |
| 43 | 137 | /** |
| 44 | | * @return {number} |
| 138 | * Returns the length of the list. |
| 139 | * @return {number} The number of elements in the list. |
| 140 | * |
| 141 | * Handled Edge Cases: empty list |
| 45 | 142 | */ |
| 46 | 143 | length() { |
| 47 | | throw new Error('Remove this line and implement the function'); |
| 144 | // Edge Case: Handle when this list is empty |
| 145 | return this.values.length; |
| 48 | 146 | } |
| 49 | 147 | |
| 50 | 148 | /** |
| 51 | | * @param {(acc: any, el: any) => any} callback |
| 52 | | * @param {any} initial |
| 53 | | * @return {any} |
| 149 | * Folds the list from the left using a callback function. |
| 150 | * @param {(acc: any, el: any) => any} callback - Function to apply to each element. |
| 151 | * @param {any} initial - Initial accumulator value. |
| 152 | * @return {any} The final accumulator value. |
| 153 | * |
| 154 | * Handled Edge Cases: empty list, callback function throws error |
| 54 | 155 | */ |
| 55 | 156 | foldl(callback, initial) { |
| 56 | | throw new Error('Remove this line and implement the function'); |
| 157 | // Edge Case: Handle when this list is empty |
| 158 | if (this.values.length === 0) { |
| 159 | return initial; |
| 160 | } |
| 161 | |
| 162 | // Edge Case: Handle when callback is null or undefined |
| 163 | if (typeof callback !== 'function') { |
| 164 | return initial; |
| 165 | } |
| 166 | |
| 167 | let accumulator = initial; |
| 168 | for (const item of this.values) { |
| 169 | try { |
| 170 | accumulator = callback(accumulator, item); |
| 171 | } catch (error) { |
| 172 | // Edge Case: If callback throws an error, we continue with current accumulator |
| 173 | continue; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return accumulator; |
| 57 | 178 | } |
| 58 | 179 | |
| 59 | 180 | /** |
| 60 | | * @param {(acc: any, el: any) => any} callback |
| 61 | | * @param {any} initial |
| 62 | | * @return {any} |
| 181 | * Folds the list from the right using a callback function. |
| 182 | * @param {(acc: any, el: any) => any} callback - Function to apply to each element. |
| 183 | * @param {any} initial - Initial accumulator value. |
| 184 | * @return {any} The final accumulator value. |
| 185 | * |
| 186 | * Handled Edge Cases: empty list, callback function throws error |
| 63 | 187 | */ |
| 64 | 188 | foldr(callback, initial) { |
| 65 | | throw new Error('Remove this line and implement the function'); |
| 189 | // Edge Case: Handle when this list is empty |
| 190 | if (this.values.length === 0) { |
| 191 | return initial; |
| 192 | } |
| 193 | |
| 194 | // Edge Case: Handle when callback is null or undefined |
| 195 | if (typeof callback !== 'function') { |
| 196 | return initial; |
| 197 | } |
| 198 | |
| 199 | let accumulator = initial; |
| 200 | // Process from right to left |
| 201 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 202 | try { |
| 203 | accumulator = callback(accumulator, this.values[i]); |
| 204 | } catch (error) { |
| 205 | // Edge Case: If callback throws an error, we continue with current accumulator |
| 206 | continue; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return accumulator; |
| 66 | 211 | } |
| 67 | 212 | |
| 68 | 213 | /** |
| 69 | | * @return {List} |
| 214 | * Reverses the list. |
| 215 | * @return {List} A new list with elements in reverse order. |
| 216 | * |
| 217 | * Handled Edge Cases: empty list |
| 70 | 218 | */ |
| 71 | 219 | reverse() { |
| 72 | | throw new Error('Remove this line and implement the function'); |
| 220 | // Edge Case: Handle when this list is empty |
| 221 | if (this.values.length === 0) { |
| 222 | return new List([]); |
| 223 | } |
| 224 | |
| 225 | return new List([...this.values].reverse()); |
| 73 | 226 | } |
| 74 | 227 | } |