| 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 | this.values = values; |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | | * @param {List} other |
| 13 | | * @return {List} |
| 12 | * Appends another list to this list |
| 13 | * @param {List} other - The list to append |
| 14 | * @return {List} A new list containing elements from both lists |
| 14 | 15 | */ |
| 15 | 16 | append(other) { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 17 | // Edge Case: Appending an empty list should return a copy of the original list |
| 18 | const result = [...this.values]; |
| 19 | for (let i = 0; i < other.values.length; i++) { |
| 20 | result.push(other.values[i]); |
| 21 | } |
| 22 | return new List(result); |
| 23 | // Handled Edge Cases: Appending empty lists |
| 17 | 24 | } |
| 18 | 25 | |
| 19 | 26 | /** |
| 20 | | * @param {List} others |
| 21 | | * @return {List} |
| 27 | * Concatenates multiple lists into one |
| 28 | * @param {List} others - A list of lists to concatenate |
| 29 | * @return {List} A new list with all elements flattened |
| 22 | 30 | */ |
| 23 | 31 | concat(others) { |
| 24 | | throw new Error('Remove this line and implement the function'); |
| 32 | // Edge Case: Concatenating with an empty list of lists |
| 33 | let result = [...this.values]; |
| 34 | |
| 35 | for (let i = 0; i < others.values.length; i++) { |
| 36 | const list = others.values[i]; |
| 37 | for (let j = 0; j < list.values.length; j++) { |
| 38 | result.push(list.values[j]); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return new List(result); |
| 43 | // Handled Edge Cases: Empty lists, nested empty lists |
| 25 | 44 | } |
| 26 | 45 | |
| 27 | 46 | /** |
| 28 | | * @param {(any) => boolean} predicate |
| 29 | | * @return {List} |
| 47 | * Filters the list based on a predicate function |
| 48 | * @param {(any) => boolean} predicate - Function to test each element |
| 49 | * @return {List} A new list with elements that pass the predicate test |
| 30 | 50 | */ |
| 31 | 51 | filter(predicate) { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 52 | // Edge Case: Filtering an empty list should return an empty list |
| 53 | const result = []; |
| 54 | |
| 55 | for (let i = 0; i < this.values.length; i++) { |
| 56 | if (predicate(this.values[i])) { |
| 57 | result.push(this.values[i]); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return new List(result); |
| 62 | // Handled Edge Cases: Empty list, all elements match, no elements match |
| 33 | 63 | } |
| 34 | 64 | |
| 35 | 65 | /** |
| 36 | | * @param {(any) => any} expression |
| 37 | | * @return {List} |
| 66 | * Transforms each element using the provided function |
| 67 | * @param {(any) => any} expression - Function to transform each element |
| 68 | * @return {List} A new list with transformed elements |
| 38 | 69 | */ |
| 39 | 70 | map(expression) { |
| 40 | | throw new Error('Remove this line and implement the function'); |
| 71 | // Edge Case: Mapping over an empty list should return an empty list |
| 72 | const result = []; |
| 73 | |
| 74 | for (let i = 0; i < this.values.length; i++) { |
| 75 | result.push(expression(this.values[i])); |
| 76 | } |
| 77 | |
| 78 | return new List(result); |
| 79 | // Handled Edge Cases: Empty list |
| 41 | 80 | } |
| 42 | 81 | |
| 43 | 82 | /** |
| 44 | | * @return {number} |
| 83 | * Returns the length of the list |
| 84 | * @return {number} The number of elements in the list |
| 45 | 85 | */ |
| 46 | 86 | length() { |
| 47 | | throw new Error('Remove this line and implement the function'); |
| 87 | // Edge Case: Length of an empty list should be 0 |
| 88 | let count = 0; |
| 89 | |
| 90 | for (let i = 0; i < this.values.length; i++) { |
| 91 | count++; |
| 92 | } |
| 93 | |
| 94 | return count; |
| 95 | // Handled Edge Cases: Empty list |
| 48 | 96 | } |
| 49 | 97 | |
| 50 | 98 | /** |
| 51 | | * @param {(acc: any, el: any) => any} callback |
| 52 | | * @param {any} initial |
| 53 | | * @return {any} |
| 99 | * Folds the list from the left using a callback function |
| 100 | * @param {(acc: any, el: any) => any} callback - Function to apply to each element |
| 101 | * @param {any} initial - Initial accumulator value |
| 102 | * @return {any} The final accumulator value |
| 54 | 103 | */ |
| 55 | 104 | foldl(callback, initial) { |
| 56 | | throw new Error('Remove this line and implement the function'); |
| 105 | // Edge Case: Folding an empty list should return the initial value |
| 106 | let accumulator = initial; |
| 107 | |
| 108 | for (let i = 0; i < this.values.length; i++) { |
| 109 | accumulator = callback(accumulator, this.values[i]); |
| 110 | } |
| 111 | |
| 112 | return accumulator; |
| 113 | // Handled Edge Cases: Empty list |
| 57 | 114 | } |
| 58 | 115 | |
| 59 | 116 | /** |
| 60 | | * @param {(acc: any, el: any) => any} callback |
| 61 | | * @param {any} initial |
| 62 | | * @return {any} |
| 117 | * Folds the list from the right using a callback function |
| 118 | * @param {(acc: any, el: any) => any} callback - Function to apply to each element |
| 119 | * @param {any} initial - Initial accumulator value |
| 120 | * @return {any} The final accumulator value |
| 63 | 121 | */ |
| 64 | 122 | foldr(callback, initial) { |
| 65 | | throw new Error('Remove this line and implement the function'); |
| 123 | // Edge Case: Folding an empty list should return the initial value |
| 124 | let accumulator = initial; |
| 125 | |
| 126 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 127 | accumulator = callback(accumulator, this.values[i]); |
| 128 | } |
| 129 | |
| 130 | return accumulator; |
| 131 | // Handled Edge Cases: Empty list |
| 66 | 132 | } |
| 67 | 133 | |
| 68 | 134 | /** |
| 69 | | * @return {List} |
| 135 | * Reverses the order of elements in the list |
| 136 | * @return {List} A new list with elements in reverse order |
| 70 | 137 | */ |
| 71 | 138 | reverse() { |
| 72 | | throw new Error('Remove this line and implement the function'); |
| 139 | // Edge Case: Reversing an empty list should return an empty list |
| 140 | const result = []; |
| 141 | |
| 142 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 143 | result.push(this.values[i]); |
| 144 | } |
| 145 | |
| 146 | return new List(result); |
| 147 | // Handled Edge Cases: Empty list, single element list |
| 73 | 148 | } |
| 74 | 149 | } |