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