| 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 another list to this list |
| 18 | * @param {List} other - The list to append |
| 19 | * @return {List} A new list containing all elements 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 multiple lists into one |
| 33 | * @param {List} others - A list of lists to concatenate |
| 34 | * @return {List} A new list with all elements flattened |
| 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 elements in others are not List instances |
| 45 | for (const list of others.values) { |
| 46 | if (!(list instanceof List)) { |
| 47 | throw new Error('All elements in others must be List instances'); |
| 48 | } |
| 49 | result = result.concat(list.values); |
| 50 | } |
| 51 | |
| 52 | return new List(result); |
| 53 | // Handled Edge Cases: others not being a List instance, elements in others not being List instances |
| 25 | 54 | } |
| 26 | 55 | |
| 27 | 56 | /** |
| 28 | | * @param {(any) => boolean} predicate |
| 29 | | * @return {List} |
| 57 | * Filters the list based on a predicate function |
| 58 | * @param {(any) => boolean} predicate - Function to test each element |
| 59 | * @return {List} A new list with elements that pass the predicate test |
| 30 | 60 | */ |
| 31 | 61 | filter(predicate) { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 62 | // Edge Case: Handle when predicate is not a function |
| 63 | if (typeof predicate !== 'function') { |
| 64 | throw new Error('Predicate must be a function'); |
| 65 | } |
| 66 | |
| 67 | const result = []; |
| 68 | for (const value of this.values) { |
| 69 | if (predicate(value)) { |
| 70 | result.push(value); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return new List(result); |
| 75 | // Handled Edge Cases: predicate not being a function |
| 33 | 76 | } |
| 34 | 77 | |
| 35 | 78 | /** |
| 36 | | * @param {(any) => any} expression |
| 37 | | * @return {List} |
| 79 | * Maps each element using a provided function |
| 80 | * @param {(any) => any} expression - Function to transform each element |
| 81 | * @return {List} A new list with transformed elements |
| 38 | 82 | */ |
| 39 | 83 | map(expression) { |
| 40 | | throw new Error('Remove this line and implement the function'); |
| 84 | // Edge Case: Handle when expression is not a function |
| 85 | if (typeof expression !== 'function') { |
| 86 | throw new Error('Expression must be a function'); |
| 87 | } |
| 88 | |
| 89 | const result = []; |
| 90 | for (const value of this.values) { |
| 91 | result.push(expression(value)); |
| 92 | } |
| 93 | |
| 94 | return new List(result); |
| 95 | // Handled Edge Cases: expression not being a function |
| 41 | 96 | } |
| 42 | 97 | |
| 43 | 98 | /** |
| 44 | | * @return {number} |
| 99 | * Returns the length of the list |
| 100 | * @return {number} The number of elements in the list |
| 45 | 101 | */ |
| 46 | 102 | length() { |
| 47 | | throw new Error('Remove this line and implement the function'); |
| 103 | let count = 0; |
| 104 | for (const _ of this.values) { |
| 105 | count++; |
| 106 | } |
| 107 | return count; |
| 108 | // Handled Edge Cases: None (this is a simple counting operation) |
| 48 | 109 | } |
| 49 | 110 | |
| 50 | 111 | /** |
| 51 | | * @param {(acc: any, el: any) => any} callback |
| 52 | | * @param {any} initial |
| 53 | | * @return {any} |
| 112 | * Folds the list from the left using a callback function |
| 113 | * @param {(acc: any, el: any) => any} callback - Function to apply to each element |
| 114 | * @param {any} initial - Initial accumulator value |
| 115 | * @return {any} The final accumulator value |
| 54 | 116 | */ |
| 55 | 117 | foldl(callback, initial) { |
| 56 | | throw new Error('Remove this line and implement the function'); |
| 118 | // Edge Case: Handle when callback is not a function |
| 119 | if (typeof callback !== 'function') { |
| 120 | throw new Error('Callback must be a function'); |
| 121 | } |
| 122 | |
| 123 | let accumulator = initial; |
| 124 | for (const value of this.values) { |
| 125 | accumulator = callback(accumulator, value); |
| 126 | } |
| 127 | |
| 128 | return accumulator; |
| 129 | // Handled Edge Cases: callback not being a function |
| 57 | 130 | } |
| 58 | 131 | |
| 59 | 132 | /** |
| 60 | | * @param {(acc: any, el: any) => any} callback |
| 61 | | * @param {any} initial |
| 62 | | * @return {any} |
| 133 | * Folds the list from the right using a callback function |
| 134 | * @param {(acc: any, el: any) => any} callback - Function to apply to each element |
| 135 | * @param {any} initial - Initial accumulator value |
| 136 | * @return {any} The final accumulator value |
| 63 | 137 | */ |
| 64 | 138 | foldr(callback, initial) { |
| 65 | | throw new Error('Remove this line and implement the function'); |
| 139 | // Edge Case: Handle when callback is not a function |
| 140 | if (typeof callback !== 'function') { |
| 141 | throw new Error('Callback must be a function'); |
| 142 | } |
| 143 | |
| 144 | let accumulator = initial; |
| 145 | // Process elements from right to left |
| 146 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 147 | accumulator = callback(accumulator, this.values[i]); |
| 148 | } |
| 149 | |
| 150 | return accumulator; |
| 151 | // Handled Edge Cases: callback not being a function |
| 66 | 152 | } |
| 67 | 153 | |
| 68 | 154 | /** |
| 69 | | * @return {List} |
| 155 | * Reverses the list |
| 156 | * @return {List} A new list with elements in reverse order |
| 70 | 157 | */ |
| 71 | 158 | reverse() { |
| 72 | | throw new Error('Remove this line and implement the function'); |
| 159 | const result = []; |
| 160 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 161 | result.push(this.values[i]); |
| 162 | } |
| 163 | |
| 164 | return new List(result); |
| 165 | // Handled Edge Cases: None (works with empty lists naturally) |
| 73 | 166 | } |
| 74 | 167 | } |