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