| 1 | | // |
| 2 | | // This is only a SKELETON file for the 'List Ops' exercise. It's been provided as a |
| 3 | | // convenience to get you started writing code faster. |
| 4 | | // |
| 5 | | |
| 6 | 1 | export class List { |
| 7 | | constructor() { |
| 8 | | throw new Error('Remove this line and implement the function'); |
| 2 | /** |
| 3 | * Creates a new List instance. |
| 4 | * @param {Array} [values=[]] - Initial values for the list. |
| 5 | */ |
| 6 | constructor(values = []) { |
| 7 | this.values = values; |
| 9 | 8 | } |
| 10 | 9 | |
| 11 | 10 | /** |
| 12 | | * @param {List} other |
| 13 | | * @return {List} |
| 11 | * Appends all items from another list to the end of this list. |
| 12 | * @param {List} other - The list to append. |
| 13 | * @return {List} A new list containing all items from both lists. |
| 14 | * Handled Edge Cases: other list is empty, this list is empty |
| 14 | 15 | */ |
| 15 | 16 | append(other) { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 17 | // Edge Case: Handle when other list is empty |
| 18 | if (other.length() === 0) { |
| 19 | return new List(this.values); |
| 20 | } |
| 21 | |
| 22 | // Edge Case: Handle when this list is empty |
| 23 | if (this.length() === 0) { |
| 24 | return new List(other.values); |
| 25 | } |
| 26 | |
| 27 | const result = []; |
| 28 | for (let i = 0; i < this.values.length; i++) { |
| 29 | result.push(this.values[i]); |
| 30 | } |
| 31 | for (let i = 0; i < other.values.length; i++) { |
| 32 | result.push(other.values[i]); |
| 33 | } |
| 34 | return new List(result); |
| 17 | 35 | } |
| 18 | 36 | |
| 19 | 37 | /** |
| 20 | | * @param {List} others |
| 21 | | * @return {List} |
| 38 | * Concatenates a series of lists into one flattened list. |
| 39 | * @param {List} others - A list containing other lists to concatenate. |
| 40 | * @return {List} A new list with all items from all lists. |
| 41 | * Handled Edge Cases: others list is empty, contains empty lists |
| 22 | 42 | */ |
| 23 | 43 | concat(others) { |
| 24 | | throw new Error('Remove this line and implement the function'); |
| 44 | // Edge Case: Handle when others list is empty |
| 45 | if (others.length() === 0) { |
| 46 | return new List(this.values); |
| 47 | } |
| 48 | |
| 49 | let result = []; |
| 50 | |
| 51 | // Add items from the current list |
| 52 | for (let i = 0; i < this.values.length; i++) { |
| 53 | result.push(this.values[i]); |
| 54 | } |
| 55 | |
| 56 | // Add items from each list in others |
| 57 | for (let i = 0; i < others.values.length; i++) { |
| 58 | const list = others.values[i]; |
| 59 | // Edge Case: Handle when a list in others is empty |
| 60 | if (list instanceof List) { |
| 61 | for (let j = 0; j < list.values.length; j++) { |
| 62 | result.push(list.values[j]); |
| 63 | } |
| 64 | } else { |
| 65 | result.push(list); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return new List(result); |
| 25 | 70 | } |
| 26 | 71 | |
| 27 | 72 | /** |
| 28 | | * @param {(any) => boolean} predicate |
| 29 | | * @return {List} |
| 73 | * Filters the list based on a predicate function. |
| 74 | * @param {Function} predicate - Function that returns true for items to keep. |
| 75 | * @return {List} A new list containing only items for which predicate is true. |
| 76 | * Handled Edge Cases: empty list, no items match predicate, all items match predicate |
| 30 | 77 | */ |
| 31 | 78 | filter(predicate) { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 79 | // Edge Case: Handle when this list is empty |
| 80 | if (this.length() === 0) { |
| 81 | return new List([]); |
| 82 | } |
| 83 | |
| 84 | const result = []; |
| 85 | for (let i = 0; i < this.values.length; i++) { |
| 86 | if (predicate(this.values[i])) { |
| 87 | result.push(this.values[i]); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Edge Case: Handle when no items match predicate |
| 92 | // Edge Case: Handle when all items match predicate |
| 93 | return new List(result); |
| 33 | 94 | } |
| 34 | 95 | |
| 35 | 96 | /** |
| 36 | | * @param {(any) => any} expression |
| 37 | | * @return {List} |
| 97 | * Maps each item in the list using a provided function. |
| 98 | * @param {Function} expression - Function to apply to each item. |
| 99 | * @return {List} A new list with the results of applying the function. |
| 100 | * Handled Edge Cases: empty list |
| 38 | 101 | */ |
| 39 | 102 | map(expression) { |
| 40 | | throw new Error('Remove this line and implement the function'); |
| 103 | // Edge Case: Handle when this list is empty |
| 104 | if (this.length() === 0) { |
| 105 | return new List([]); |
| 106 | } |
| 107 | |
| 108 | const result = []; |
| 109 | for (let i = 0; i < this.values.length; i++) { |
| 110 | result.push(expression(this.values[i])); |
| 111 | } |
| 112 | |
| 113 | return new List(result); |
| 41 | 114 | } |
| 42 | 115 | |
| 43 | 116 | /** |
| 44 | | * @return {number} |
| 117 | * Returns the number of items in the list. |
| 118 | * @return {number} The length of the list. |
| 119 | * Handled Edge Cases: empty list |
| 45 | 120 | */ |
| 46 | 121 | length() { |
| 47 | | throw new Error('Remove this line and implement the function'); |
| 122 | let count = 0; |
| 123 | for (let i = 0; i < this.values.length; i++) { |
| 124 | count++; |
| 125 | } |
| 126 | |
| 127 | // Edge Case: Handle when list is empty |
| 128 | return count; |
| 48 | 129 | } |
| 49 | 130 | |
| 50 | 131 | /** |
| 51 | | * @param {(acc: any, el: any) => any} callback |
| 52 | | * @param {any} initial |
| 53 | | * @return {any} |
| 132 | * Folds (reduces) the list from the left using a callback function. |
| 133 | * @param {Function} callback - Function to apply to accumulator and each item. |
| 134 | * @param {any} initial - Initial value for the accumulator. |
| 135 | * @return {any} The final accumulator value. |
| 136 | * Handled Edge Cases: empty list |
| 54 | 137 | */ |
| 55 | 138 | foldl(callback, initial) { |
| 56 | | throw new Error('Remove this line and implement the function'); |
| 139 | // Edge Case: Handle when this list is empty |
| 140 | if (this.length() === 0) { |
| 141 | return initial; |
| 142 | } |
| 143 | |
| 144 | let accumulator = initial; |
| 145 | for (let i = 0; i < this.values.length; i++) { |
| 146 | accumulator = callback(accumulator, this.values[i]); |
| 147 | } |
| 148 | |
| 149 | return accumulator; |
| 57 | 150 | } |
| 58 | 151 | |
| 59 | 152 | /** |
| 60 | | * @param {(acc: any, el: any) => any} callback |
| 61 | | * @param {any} initial |
| 62 | | * @return {any} |
| 153 | * Folds (reduces) the list from the right using a callback function. |
| 154 | * @param {Function} callback - Function to apply to accumulator and each item. |
| 155 | * @param {any} initial - Initial value for the accumulator. |
| 156 | * @return {any} The final accumulator value. |
| 157 | * Handled Edge Cases: empty list |
| 63 | 158 | */ |
| 64 | 159 | foldr(callback, initial) { |
| 65 | | throw new Error('Remove this line and implement the function'); |
| 160 | // Edge Case: Handle when this list is empty |
| 161 | if (this.length() === 0) { |
| 162 | return initial; |
| 163 | } |
| 164 | |
| 165 | let accumulator = initial; |
| 166 | // Process from right to left |
| 167 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 168 | accumulator = callback(accumulator, this.values[i]); |
| 169 | } |
| 170 | |
| 171 | return accumulator; |
| 66 | 172 | } |
| 67 | 173 | |
| 68 | 174 | /** |
| 69 | | * @return {List} |
| 175 | * Reverses the order of items in the list. |
| 176 | * @return {List} A new list with items in reversed order. |
| 177 | * Handled Edge Cases: empty list |
| 70 | 178 | */ |
| 71 | 179 | reverse() { |
| 72 | | throw new Error('Remove this line and implement the function'); |
| 180 | // Edge Case: Handle when this list is empty |
| 181 | if (this.length() === 0) { |
| 182 | return new List([]); |
| 183 | } |
| 184 | |
| 185 | const result = []; |
| 186 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 187 | result.push(this.values[i]); |
| 188 | } |
| 189 | |
| 190 | return new List(result); |
| 73 | 191 | } |
| 74 | 192 | } |