| 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 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 | * |
| 20 | * Handled Edge Cases: |
| 21 | * - When other list is empty |
| 22 | * - When this list is empty |
| 23 | * - When both lists are empty |
| 14 | 24 | */ |
| 15 | 25 | append(other) { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 26 | // Edge Case: When both lists are empty |
| 27 | if (this.values.length === 0 && other.values.length === 0) { |
| 28 | return new List([]); |
| 29 | } |
| 30 | |
| 31 | // Edge Case: When other list is empty |
| 32 | if (other.values.length === 0) { |
| 33 | return new List(this.values); |
| 34 | } |
| 35 | |
| 36 | // Edge Case: When this list is empty |
| 37 | if (this.values.length === 0) { |
| 38 | return new List(other.values); |
| 39 | } |
| 40 | |
| 41 | return new List([...this.values, ...other.values]); |
| 17 | 42 | } |
| 18 | 43 | |
| 19 | 44 | /** |
| 20 | | * @param {List} others |
| 21 | | * @return {List} |
| 45 | * Concatenates all items from a series of lists into one flattened list. |
| 46 | * @param {List} others - A list containing other lists to concatenate. |
| 47 | * @return {List} A new list with all items from all lists. |
| 48 | * |
| 49 | * Handled Edge Cases: |
| 50 | * - When others list is empty |
| 51 | * - When some contained lists are empty |
| 52 | * - When all contained lists are empty |
| 22 | 53 | */ |
| 23 | 54 | concat(others) { |
| 24 | | throw new Error('Remove this line and implement the function'); |
| 55 | let result = [...this.values]; |
| 56 | |
| 57 | // Edge Case: When others list is empty |
| 58 | if (others.values.length === 0) { |
| 59 | return new List(result); |
| 60 | } |
| 61 | |
| 62 | for (const list of others.values) { |
| 63 | // Edge Case: When some contained lists are empty |
| 64 | if (list.values && list.values.length >= 0) { |
| 65 | result = [...result, ...list.values]; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return new List(result); |
| 25 | 70 | } |
| 26 | 71 | |
| 27 | 72 | /** |
| 28 | | * @param {(any) => boolean} predicate |
| 29 | | * @return {List} |
| 73 | * Filters the list based on a predicate function. |
| 74 | * @param {(any) => boolean} predicate - Function to test each item. |
| 75 | * @return {List} A new list containing only items for which predicate returns true. |
| 76 | * |
| 77 | * Handled Edge Cases: |
| 78 | * - When the list is empty |
| 79 | * - When no items match the predicate |
| 80 | * - When all items match the predicate |
| 30 | 81 | */ |
| 31 | 82 | filter(predicate) { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 83 | // Edge Case: When the list is empty |
| 84 | if (this.values.length === 0) { |
| 85 | return new List([]); |
| 86 | } |
| 87 | |
| 88 | const result = []; |
| 89 | for (const item of this.values) { |
| 90 | if (predicate(item)) { |
| 91 | result.push(item); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // Edge Case: When no items match the predicate |
| 96 | // Edge Case: When all items match the predicate |
| 97 | return new List(result); |
| 33 | 98 | } |
| 34 | 99 | |
| 35 | 100 | /** |
| 36 | | * @param {(any) => any} expression |
| 37 | | * @return {List} |
| 101 | * Maps each item in the list using a provided function. |
| 102 | * @param {(any) => any} expression - Function to apply to each item. |
| 103 | * @return {List} A new list containing the results of applying the function to each item. |
| 104 | * |
| 105 | * Handled Edge Cases: |
| 106 | * - When the list is empty |
| 38 | 107 | */ |
| 39 | 108 | map(expression) { |
| 40 | | throw new Error('Remove this line and implement the function'); |
| 109 | // Edge Case: When the list is empty |
| 110 | if (this.values.length === 0) { |
| 111 | return new List([]); |
| 112 | } |
| 113 | |
| 114 | const result = []; |
| 115 | for (const item of this.values) { |
| 116 | result.push(expression(item)); |
| 117 | } |
| 118 | |
| 119 | return new List(result); |
| 41 | 120 | } |
| 42 | 121 | |
| 43 | 122 | /** |
| 44 | | * @return {number} |
| 123 | * Returns the total number of items in the list. |
| 124 | * @return {number} The length of the list. |
| 125 | * |
| 126 | * Handled Edge Cases: |
| 127 | * - When the list is empty |
| 45 | 128 | */ |
| 46 | 129 | length() { |
| 47 | | throw new Error('Remove this line and implement the function'); |
| 130 | // Edge Case: When the list is empty |
| 131 | let count = 0; |
| 132 | for (const _ of this.values) { |
| 133 | count++; |
| 134 | } |
| 135 | return count; |
| 48 | 136 | } |
| 49 | 137 | |
| 50 | 138 | /** |
| 51 | | * @param {(acc: any, el: any) => any} callback |
| 52 | | * @param {any} initial |
| 53 | | * @return {any} |
| 139 | * Folds (reduces) the list from the left using a callback function and initial accumulator. |
| 140 | * @param {(acc: any, el: any) => any} callback - Function to apply to each item. |
| 141 | * @param {any} initial - Initial value for the accumulator. |
| 142 | * @return {any} The final accumulated value. |
| 143 | * |
| 144 | * Handled Edge Cases: |
| 145 | * - When the list is empty |
| 54 | 146 | */ |
| 55 | 147 | foldl(callback, initial) { |
| 56 | | throw new Error('Remove this line and implement the function'); |
| 148 | // Edge Case: When the list is empty |
| 149 | if (this.values.length === 0) { |
| 150 | return initial; |
| 151 | } |
| 152 | |
| 153 | let accumulator = initial; |
| 154 | for (const item of this.values) { |
| 155 | accumulator = callback(accumulator, item); |
| 156 | } |
| 157 | |
| 158 | return accumulator; |
| 57 | 159 | } |
| 58 | 160 | |
| 59 | 161 | /** |
| 60 | | * @param {(acc: any, el: any) => any} callback |
| 61 | | * @param {any} initial |
| 62 | | * @return {any} |
| 162 | * Folds (reduces) the list from the right using a callback function and initial accumulator. |
| 163 | * @param {(acc: any, el: any) => any} callback - Function to apply to each item. |
| 164 | * @param {any} initial - Initial value for the accumulator. |
| 165 | * @return {any} The final accumulated value. |
| 166 | * |
| 167 | * Handled Edge Cases: |
| 168 | * - When the list is empty |
| 63 | 169 | */ |
| 64 | 170 | foldr(callback, initial) { |
| 65 | | throw new Error('Remove this line and implement the function'); |
| 171 | // Edge Case: When the list is empty |
| 172 | if (this.values.length === 0) { |
| 173 | return initial; |
| 174 | } |
| 175 | |
| 176 | let accumulator = initial; |
| 177 | // Process from right to left |
| 178 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 179 | accumulator = callback(accumulator, this.values[i]); |
| 180 | } |
| 181 | |
| 182 | return accumulator; |
| 66 | 183 | } |
| 67 | 184 | |
| 68 | 185 | /** |
| 69 | | * @return {List} |
| 186 | * Returns a new list with all the original items in reversed order. |
| 187 | * @return {List} A new list with items in reversed order. |
| 188 | * |
| 189 | * Handled Edge Cases: |
| 190 | * - When the list is empty |
| 191 | * - When the list has only one item |
| 70 | 192 | */ |
| 71 | 193 | reverse() { |
| 72 | | throw new Error('Remove this line and implement the function'); |
| 194 | // Edge Case: When the list is empty |
| 195 | if (this.values.length === 0) { |
| 196 | return new List([]); |
| 197 | } |
| 198 | |
| 199 | // Edge Case: When the list has only one item |
| 200 | if (this.values.length === 1) { |
| 201 | return new List([this.values[0]]); |
| 202 | } |
| 203 | |
| 204 | const result = []; |
| 205 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 206 | result.push(this.values[i]); |
| 207 | } |
| 208 | |
| 209 | return new List(result); |
| 73 | 210 | } |
| 74 | 211 | } |