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