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