| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | export class List { |
| 7 | | constructor() { |
| 8 | | throw new Error('Remove this line and implement the function'); |
| 7 | constructor(values = []) { |
| 8 | // Edge Case: Handle when no values are provided by initializing with an empty array |
| 9 | this.values = values; |
| 9 | 10 | } |
| 10 | 11 | |
| 11 | 12 | /** |
| 12 | | * @param {List} other |
| 13 | | * @return {List} |
| 13 | * Appends all items from another list to the end of this list |
| 14 | * @param {List} other - The list to append |
| 15 | * @return {List} A new list containing all items from both lists |
| 14 | 16 | */ |
| 15 | 17 | append(other) { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 18 | // Edge Case: Handle when other list is empty |
| 19 | if (!other || !other.values) { |
| 20 | return new List(this.values); |
| 21 | } |
| 22 | |
| 23 | const result = []; |
| 24 | for (let i = 0; i < this.values.length; i++) { |
| 25 | result.push(this.values[i]); |
| 26 | } |
| 27 | for (let i = 0; i < other.values.length; i++) { |
| 28 | result.push(other.values[i]); |
| 29 | } |
| 30 | return new List(result); |
| 31 | // Handled Edge Cases: Empty other list |
| 17 | 32 | } |
| 18 | 33 | |
| 19 | 34 | /** |
| 20 | | * @param {List} others |
| 21 | | * @return {List} |
| 35 | * Concatenates a series of lists into one flattened list |
| 36 | * @param {List} others - A list containing other lists to concatenate |
| 37 | * @return {List} A new list with all items from all lists |
| 22 | 38 | */ |
| 23 | 39 | concat(others) { |
| 24 | | throw new Error('Remove this line and implement the function'); |
| 40 | // Edge Case: Handle when others is null or undefined |
| 41 | if (!others) { |
| 42 | return new List(this.values); |
| 43 | } |
| 44 | |
| 45 | let result = []; |
| 46 | |
| 47 | // Add items from the current list |
| 48 | for (let i = 0; i < this.values.length; i++) { |
| 49 | result.push(this.values[i]); |
| 50 | } |
| 51 | |
| 52 | // Add items from each list in others |
| 53 | for (let i = 0; i < others.values.length; i++) { |
| 54 | const item = others.values[i]; |
| 55 | if (item instanceof List) { |
| 56 | // If item is a List, add its values |
| 57 | for (let j = 0; j < item.values.length; j++) { |
| 58 | result.push(item.values[j]); |
| 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); |
| 67 | // Handled Edge Cases: Null/undefined others, non-List items in others |
| 25 | 68 | } |
| 26 | 69 | |
| 27 | 70 | /** |
| 28 | | * @param {(any) => boolean} predicate |
| 29 | | * @return {List} |
| 71 | * Filters the list based on a predicate function |
| 72 | * @param {(any) => boolean} predicate - Function to test each item |
| 73 | * @return {List} A new list containing only items that satisfy the predicate |
| 30 | 74 | */ |
| 31 | 75 | filter(predicate) { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 76 | // Edge Case: Handle when predicate is not a function |
| 77 | if (typeof predicate !== 'function') { |
| 78 | throw new Error('Predicate must be a function'); |
| 79 | } |
| 80 | |
| 81 | const result = []; |
| 82 | for (let i = 0; i < this.values.length; i++) { |
| 83 | if (predicate(this.values[i])) { |
| 84 | result.push(this.values[i]); |
| 85 | } |
| 86 | } |
| 87 | return new List(result); |
| 88 | // Handled Edge Cases: Non-function predicate |
| 33 | 89 | } |
| 34 | 90 | |
| 35 | 91 | /** |
| 36 | | * @param {(any) => any} expression |
| 37 | | * @return {List} |
| 92 | * Maps each item in the list using a provided function |
| 93 | * @param {(any) => any} expression - Function to transform each item |
| 94 | * @return {List} A new list with transformed items |
| 38 | 95 | */ |
| 39 | 96 | map(expression) { |
| 40 | | throw new Error('Remove this line and implement the function'); |
| 97 | // Edge Case: Handle when expression is not a function |
| 98 | if (typeof expression !== 'function') { |
| 99 | throw new Error('Expression must be a function'); |
| 100 | } |
| 101 | |
| 102 | const result = []; |
| 103 | for (let i = 0; i < this.values.length; i++) { |
| 104 | result.push(expression(this.values[i])); |
| 105 | } |
| 106 | return new List(result); |
| 107 | // Handled Edge Cases: Non-function expression |
| 41 | 108 | } |
| 42 | 109 | |
| 43 | 110 | /** |
| 44 | | * @return {number} |
| 111 | * Returns the number of items in the list |
| 112 | * @return {number} The length of the list |
| 45 | 113 | */ |
| 46 | 114 | length() { |
| 47 | | throw new Error('Remove this line and implement the function'); |
| 115 | let count = 0; |
| 116 | for (let i = 0; i < this.values.length; i++) { |
| 117 | count++; |
| 118 | } |
| 119 | return count; |
| 120 | // Handled Edge Cases: None (empty list is naturally handled) |
| 48 | 121 | } |
| 49 | 122 | |
| 50 | 123 | /** |
| 51 | | * @param {(acc: any, el: any) => any} callback |
| 52 | | * @param {any} initial |
| 53 | | * @return {any} |
| 124 | * Folds the list from the left using a callback function and initial accumulator |
| 125 | * @param {(acc: any, el: any) => any} callback - Function to apply to each item |
| 126 | * @param {any} initial - Initial accumulator value |
| 127 | * @return {any} The final accumulator value |
| 54 | 128 | */ |
| 55 | 129 | foldl(callback, initial) { |
| 56 | | throw new Error('Remove this line and implement the function'); |
| 130 | // Edge Case: Handle when callback is not a function |
| 131 | if (typeof callback !== 'function') { |
| 132 | throw new Error('Callback must be a function'); |
| 133 | } |
| 134 | |
| 135 | let accumulator = initial; |
| 136 | for (let i = 0; i < this.values.length; i++) { |
| 137 | accumulator = callback(accumulator, this.values[i]); |
| 138 | } |
| 139 | return accumulator; |
| 140 | // Handled Edge Cases: Non-function callback |
| 57 | 141 | } |
| 58 | 142 | |
| 59 | 143 | /** |
| 60 | | * @param {(acc: any, el: any) => any} callback |
| 61 | | * @param {any} initial |
| 62 | | * @return {any} |
| 144 | * Folds 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 |
| 63 | 148 | */ |
| 64 | 149 | foldr(callback, initial) { |
| 65 | | throw new Error('Remove this line and implement the function'); |
| 150 | // Edge Case: Handle when callback is not a function |
| 151 | if (typeof callback !== 'function') { |
| 152 | throw new Error('Callback must be a function'); |
| 153 | } |
| 154 | |
| 155 | let accumulator = initial; |
| 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 | return accumulator; |
| 161 | // Handled Edge Cases: Non-function callback, empty list |
| 66 | 162 | } |
| 67 | 163 | |
| 68 | 164 | /** |
| 69 | | * @return {List} |
| 165 | * Returns a new list with items in reverse order |
| 166 | * @return {List} A new list with reversed items |
| 70 | 167 | */ |
| 71 | 168 | reverse() { |
| 72 | | throw new Error('Remove this line and implement the function'); |
| 169 | const result = []; |
| 170 | // Build result array in reverse order |
| 171 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 172 | result.push(this.values[i]); |
| 173 | } |
| 174 | return new List(result); |
| 175 | // Handled Edge Cases: Empty list |
| 73 | 176 | } |
| 74 | 177 | } |