| 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 | * Handled Edge Cases: Appending empty lists, appending to empty lists |
| 14 | 20 | */ |
| 15 | 21 | append(other) { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 22 | // Edge Case: Appending empty lists |
| 23 | if (!other || !Array.isArray(other.values)) { |
| 24 | return new List(this.values); |
| 25 | } |
| 26 | |
| 27 | const result = []; |
| 28 | for (let i = 0; i < this.values.length; i++) { |
| 29 | result.push(this.values[i]); |
| 30 | } |
| 31 | for (let i = 0; i < other.values.length; i++) { |
| 32 | result.push(other.values[i]); |
| 33 | } |
| 34 | |
| 35 | return new List(result); |
| 17 | 36 | } |
| 18 | 37 | |
| 19 | 38 | /** |
| 20 | | * @param {List} others |
| 21 | | * @return {List} |
| 39 | * Concatenates a series of lists into one flattened list. |
| 40 | * @param {List} others - A list containing other lists to concatenate. |
| 41 | * @return {List} A new list with all items from all lists. |
| 42 | * Handled Edge Cases: Concatenating empty lists, concatenating with empty lists |
| 22 | 43 | */ |
| 23 | 44 | concat(others) { |
| 24 | | throw new Error('Remove this line and implement the function'); |
| 45 | // Edge Case: Concatenating empty lists |
| 46 | if (!others || !Array.isArray(others.values)) { |
| 47 | return new List(this.values); |
| 48 | } |
| 49 | |
| 50 | let result = []; |
| 51 | |
| 52 | // Add items from the current list |
| 53 | for (let i = 0; i < this.values.length; i++) { |
| 54 | result.push(this.values[i]); |
| 55 | } |
| 56 | |
| 57 | // Add items from each list in others |
| 58 | for (let i = 0; i < others.values.length; i++) { |
| 59 | const item = others.values[i]; |
| 60 | if (item instanceof List) { |
| 61 | for (let j = 0; j < item.values.length; j++) { |
| 62 | result.push(item.values[j]); |
| 63 | } |
| 64 | } else { |
| 65 | result.push(item); |
| 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 that satisfy the predicate. |
| 76 | * Handled Edge Cases: Empty list, predicate function that always returns false |
| 30 | 77 | */ |
| 31 | 78 | filter(predicate) { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 79 | // Edge Case: Empty list |
| 80 | if (this.values.length === 0) { |
| 81 | return new List([]); |
| 82 | } |
| 83 | |
| 84 | const result = []; |
| 85 | for (let i = 0; i < this.values.length; i++) { |
| 86 | if (predicate(this.values[i])) { |
| 87 | result.push(this.values[i]); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return new List(result); |
| 33 | 92 | } |
| 34 | 93 | |
| 35 | 94 | /** |
| 36 | | * @param {(any) => any} expression |
| 37 | | * @return {List} |
| 95 | * Transforms each item in the list using a provided function. |
| 96 | * @param {(any) => any} expression - Function to transform each item. |
| 97 | * @return {List} A new list with transformed items. |
| 98 | * Handled Edge Cases: Empty list, expression that returns undefined |
| 38 | 99 | */ |
| 39 | 100 | map(expression) { |
| 40 | | throw new Error('Remove this line and implement the function'); |
| 101 | // Edge Case: Empty list |
| 102 | if (this.values.length === 0) { |
| 103 | return new List([]); |
| 104 | } |
| 105 | |
| 106 | const result = []; |
| 107 | for (let i = 0; i < this.values.length; i++) { |
| 108 | result.push(expression(this.values[i])); |
| 109 | } |
| 110 | |
| 111 | return new List(result); |
| 41 | 112 | } |
| 42 | 113 | |
| 43 | 114 | /** |
| 44 | | * @return {number} |
| 115 | * Returns the number of items in the list. |
| 116 | * @return {number} The length of the list. |
| 117 | * Handled Edge Cases: Empty list |
| 45 | 118 | */ |
| 46 | 119 | length() { |
| 47 | | throw new Error('Remove this line and implement the function'); |
| 120 | // Edge Case: Empty list |
| 121 | let count = 0; |
| 122 | for (let i = 0; i < this.values.length; i++) { |
| 123 | count++; |
| 124 | } |
| 125 | return count; |
| 48 | 126 | } |
| 49 | 127 | |
| 50 | 128 | /** |
| 51 | | * @param {(acc: any, el: any) => any} callback |
| 52 | | * @param {any} initial |
| 53 | | * @return {any} |
| 129 | * Folds (reduces) the list from left to right using a callback function. |
| 130 | * @param {(acc: any, el: any) => any} callback - Function to apply to each item. |
| 131 | * @param {any} initial - Initial accumulator value. |
| 132 | * @return {any} The final accumulator value. |
| 133 | * Handled Edge Cases: Empty list |
| 54 | 134 | */ |
| 55 | 135 | foldl(callback, initial) { |
| 56 | | throw new Error('Remove this line and implement the function'); |
| 136 | // Edge Case: Empty list |
| 137 | let accumulator = initial; |
| 138 | for (let i = 0; i < this.values.length; i++) { |
| 139 | accumulator = callback(accumulator, this.values[i]); |
| 140 | } |
| 141 | return accumulator; |
| 57 | 142 | } |
| 58 | 143 | |
| 59 | 144 | /** |
| 60 | | * @param {(acc: any, el: any) => any} callback |
| 61 | | * @param {any} initial |
| 62 | | * @return {any} |
| 145 | * Folds (reduces) the list from right to left using a callback function. |
| 146 | * @param {(acc: any, el: any) => any} callback - Function to apply to each item. |
| 147 | * @param {any} initial - Initial accumulator value. |
| 148 | * @return {any} The final accumulator value. |
| 149 | * Handled Edge Cases: Empty list |
| 63 | 150 | */ |
| 64 | 151 | foldr(callback, initial) { |
| 65 | | throw new Error('Remove this line and implement the function'); |
| 152 | // Edge Case: Empty list |
| 153 | let accumulator = initial; |
| 154 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 155 | accumulator = callback(accumulator, this.values[i]); |
| 156 | } |
| 157 | return accumulator; |
| 66 | 158 | } |
| 67 | 159 | |
| 68 | 160 | /** |
| 69 | | * @return {List} |
| 161 | * Returns a new list with items in reversed order. |
| 162 | * @return {List} A new list with reversed items. |
| 163 | * Handled Edge Cases: Empty list, single item list |
| 70 | 164 | */ |
| 71 | 165 | reverse() { |
| 72 | | throw new Error('Remove this line and implement the function'); |
| 166 | // Edge Case: Empty list |
| 167 | if (this.values.length === 0) { |
| 168 | return new List([]); |
| 169 | } |
| 170 | |
| 171 | // Edge Case: Single item list |
| 172 | if (this.values.length === 1) { |
| 173 | return new List([this.values[0]]); |
| 174 | } |
| 175 | |
| 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 | } |