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