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