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