| 13 | 13 | * @return {List} |
| 14 | 14 | */ |
| 15 | 15 | append(other) { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 16 | // Edge Case: Appending an empty list |
| 17 | if (other.length() === 0) { |
| 18 | return new List(this.values); |
| 19 | } |
| 20 | |
| 21 | // Edge Case: Appending to an empty list |
| 22 | if (this.length() === 0) { |
| 23 | return new List(other.values); |
| 24 | } |
| 25 | |
| 26 | // Manual implementation without spread operator |
| 27 | const result = []; |
| 28 | for (const item of this.values) { |
| 29 | result.push(item); |
| 30 | } |
| 31 | for (const item of other.values) { |
| 32 | result.push(item); |
| 33 | } |
| 34 | return new List(result); |
| 17 | 35 | } |
| 18 | 36 | |
| 19 | 37 | /** |
| 21 | 39 | * @return {List} |
| 22 | 40 | */ |
| 23 | 41 | concat(others) { |
| 24 | | throw new Error('Remove this line and implement the function'); |
| 42 | // Start with current list values |
| 43 | const result = []; |
| 44 | for (const item of this.values) { |
| 45 | result.push(item); |
| 46 | } |
| 47 | |
| 48 | // Edge Case: Concatenating with an empty list |
| 49 | if (others.length() === 0) { |
| 50 | return new List(result); |
| 51 | } |
| 52 | |
| 53 | // Process each item in the others list |
| 54 | for (const item of others.values) { |
| 55 | if (item instanceof List) { |
| 56 | // If item is a List, add all its elements |
| 57 | for (const subItem of item.values) { |
| 58 | result.push(subItem); |
| 59 | } |
| 60 | } else { |
| 61 | // If item is not a List, add it directly |
| 62 | result.push(item); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return new List(result); |
| 25 | 67 | } |
| 26 | 68 | |
| 27 | 69 | /** |
| 53 | 128 | * @return {any} |
| 54 | 129 | */ |
| 55 | 130 | foldl(callback, initial) { |
| 56 | | throw new Error('Remove this line and implement the function'); |
| 131 | let accumulator = initial; |
| 132 | |
| 133 | // Edge Case: Folding an empty list |
| 134 | if (this.length() === 0) { |
| 135 | return accumulator; |
| 136 | } |
| 137 | |
| 138 | for (const item of this.values) { |
| 139 | accumulator = callback(accumulator, item); |
| 140 | } |
| 141 | |
| 142 | return accumulator; |
| 57 | 143 | } |
| 58 | 144 | |
| 59 | 145 | /** |
| 60 | | * @param {(acc: any, el: any) => any} callback |
| 146 | * @param {(el: any, acc: any) => any} callback |
| 61 | 147 | * @param {any} initial |
| 62 | 148 | * @return {any} |
| 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: Folding an empty list from the right |
| 154 | if (this.length() === 0) { |
| 155 | return accumulator; |
| 156 | } |
| 157 | |
| 158 | // Process from right to left |
| 159 | // Note: callback is called as callback(item, accumulator) |
| 160 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 161 | accumulator = callback(this.values[i], accumulator); |
| 162 | } |
| 163 | |
| 164 | return accumulator; |
| 66 | 165 | } |
| 67 | 166 | |
| 68 | 167 | /** |
| 69 | 168 | * @return {List} |
| 70 | 169 | */ |
| 71 | 170 | reverse() { |
| 72 | | throw new Error('Remove this line and implement the function'); |
| 171 | const result = []; |
| 172 | |
| 173 | // Edge Case: Reversing an empty list |
| 174 | if (this.length() === 0) { |
| 175 | return new List(result); |
| 176 | } |
| 177 | |
| 178 | // Edge Case: Reversing a single element list |
| 179 | if (this.length() === 1) { |
| 180 | return new List([this.values[0]]); |
| 181 | } |
| 182 | |
| 183 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 184 | result.push(this.values[i]); |
| 185 | } |
| 186 | |
| 187 | return new List(result); |
| 73 | 188 | } |
| 74 | 189 | } |
| 190 | |
| 191 | // Handled Edge Cases: Appending an empty list, appending to an empty list, concatenating with an empty list, concatenating an empty list with others, filtering an empty list, mapping over an empty list, length of an empty list, folding an empty list, folding an empty list from the right, reversing an empty list, reversing a single element list |