| 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 | /** |
| 13 | 13 | * @return {List} |
| 14 | 14 | */ |
| 15 | 15 | append(other) { |
| 16 | throw new Error('Remove this line and implement the function'); | |
| 16 | // Edge Case: Appending an empty list should return the original list | |
| 17 | if (other.length() === 0) { | |
| 18 | return new List(this.values); | |
| 19 | } | |
| 20 | ||
| 21 | const result = []; | |
| 22 | for (let i = 0; i < this.values.length; i++) { | |
| 23 | result.push(this.values[i]); | |
| 24 | } | |
| 25 | for (let i = 0; i < other.values.length; i++) { | |
| 26 | result.push(other.values[i]); | |
| 27 | } | |
| 28 | return new List(result); | |
| 17 | 29 | } |
| 18 | 30 | |
| 19 | 31 | /** |
| 20 | * @param {List} others | |
| 32 | * @param {List[]} others | |
| 21 | 33 | * @return {List} |
| 22 | 34 | */ |
| 23 | 35 | concat(others) { |
| 24 | throw new Error('Remove this line and implement the function'); | |
| 36 | // Edge Case: Concatenating with no lists should return the original list | |
| 37 | if (!others || others.length === 0) { | |
| 38 | return new List(this.values); | |
| 39 | } | |
| 40 | ||
| 41 | let result = []; | |
| 42 | ||
| 43 | // Add elements from the current list | |
| 44 | for (let i = 0; i < this.values.length; i++) { | |
| 45 | result.push(this.values[i]); | |
| 46 | } | |
| 47 | ||
| 48 | // Add elements from each list in others | |
| 49 | for (let i = 0; i < others.length; i++) { | |
| 50 | const list = others[i]; | |
| 51 | for (let j = 0; j < list.values.length; j++) { | |
| 52 | result.push(list.values[j]); | |
| 53 | } | |
| 54 | } | |
| 55 | ||
| 56 | return new List(result); | |
| 25 | 57 | } |
| 26 | 58 | |
| 27 | 59 | /** |
| 29 | 61 | * @return {List} |
| 30 | 62 | */ |
| 31 | 63 | filter(predicate) { |
| 32 | throw new Error('Remove this line and implement the function'); | |
| 64 | // Edge Case: Filtering an empty list should return an empty list | |
| 65 | if (this.values.length === 0) { | |
| 66 | return new List([]); | |
| 67 | } | |
| 68 | ||
| 69 | const result = []; | |
| 70 | for (let i = 0; i < this.values.length; i++) { | |
| 71 | if (predicate(this.values[i])) { | |
| 72 | result.push(this.values[i]); | |
| 73 | } | |
| 74 | } | |
| 75 | return new List(result); | |
| 33 | 76 | } |
| 34 | 77 | |
| 35 | 78 | /** |
| 37 | 80 | * @return {List} |
| 38 | 81 | */ |
| 39 | 82 | map(expression) { |
| 40 | throw new Error('Remove this line and implement the function'); | |
| 83 | // Edge Case: Mapping over an empty list should return an empty list | |
| 84 | if (this.values.length === 0) { | |
| 85 | return new List([]); | |
| 86 | } | |
| 87 | ||
| 88 | const result = []; | |
| 89 | for (let i = 0; i < this.values.length; i++) { | |
| 90 | result.push(expression(this.values[i])); | |
| 91 | } | |
| 92 | return new List(result); | |
| 41 | 93 | } |
| 42 | 94 | |
| 43 | 95 | /** |
| 44 | 96 | * @return {number} |
| 45 | 97 | */ |
| 46 | 98 | length() { |
| 47 | throw new Error('Remove this line and implement the function'); | |
| 99 | // Edge Case: Length of an empty list should be 0 | |
| 100 | let count = 0; | |
| 101 | for (let i = 0; i < this.values.length; i++) { | |
| 102 | count++; | |
| 103 | } | |
| 104 | return count; | |
| 48 | 105 | } |
| 49 | 106 | |
| 50 | 107 | /** |
| 53 | 110 | * @return {any} |
| 54 | 111 | */ |
| 55 | 112 | foldl(callback, initial) { |
| 56 | throw new Error('Remove this line and implement the function'); | |
| 113 | // Edge Case: Folding an empty list should return the initial value | |
| 114 | if (this.values.length === 0) { | |
| 115 | return initial; | |
| 116 | } | |
| 117 | ||
| 118 | let accumulator = initial; | |
| 119 | for (let i = 0; i < this.values.length; i++) { | |
| 120 | accumulator = callback(accumulator, this.values[i]); | |
| 121 | } | |
| 122 | return accumulator; | |
| 57 | 123 | } |
| 58 | 124 | |
| 59 | 125 | /** |
| 62 | 128 | * @return {any} |
| 63 | 129 | */ |
| 64 | 130 | foldr(callback, initial) { |
| 65 | throw new Error('Remove this line and implement the function'); | |
| 131 | // Edge Case: Folding an empty list should return the initial value | |
| 132 | if (this.values.length === 0) { | |
| 133 | return initial; | |
| 134 | } | |
| 135 | ||
| 136 | let accumulator = initial; | |
| 137 | // Process from right to left | |
| 138 | for (let i = this.values.length - 1; i >= 0; i--) { | |
| 139 | accumulator = callback(accumulator, this.values[i]); | |
| 140 | } | |
| 141 | return accumulator; | |
| 66 | 142 | } |
| 67 | 143 | |
| 68 | 144 | /** |
| 69 | 145 | * @return {List} |
| 70 | 146 | */ |
| 71 | 147 | reverse() { |
| 72 | throw new Error('Remove this line and implement the function'); | |
| 148 | // Edge Case: Reversing an empty list should return an empty list | |
| 149 | if (this.values.length === 0) { | |
| 150 | return new List([]); | |
| 151 | } | |
| 152 | ||
| 153 | const result = []; | |
| 154 | for (let i = this.values.length - 1; i >= 0; i--) { | |
| 155 | result.push(this.values[i]); | |
| 156 | } | |
| 157 | return new List(result); | |
| 73 | 158 | } |
| 74 | 159 | } |
| 160 | ||
| 161 | // Handled Edge Cases: Appending an empty list, concatenating with no lists, filtering an empty list, mapping over an empty list, length of an empty list, folding an empty list, reversing an empty list |