| 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); | |
| 29 | // Handled Edge Cases: Appending empty list | |
| 17 | 30 | } |
| 18 | 31 | |
| 19 | 32 | /** |
| 21 | 34 | * @return {List} |
| 22 | 35 | */ |
| 23 | 36 | concat(others) { |
| 24 | throw new Error('Remove this line and implement the function'); | |
| 37 | // Edge Case: Concatenating with an empty list of lists | |
| 38 | let result = new List(this.values); | |
| 39 | ||
| 40 | for (let i = 0; i < others.values.length; i++) { | |
| 41 | result = result.append(others.values[i]); | |
| 42 | } | |
| 43 | ||
| 44 | return result; | |
| 45 | // Handled Edge Cases: Concatenating with empty lists | |
| 25 | 46 | } |
| 26 | 47 | |
| 27 | 48 | /** |
| 29 | 50 | * @return {List} |
| 30 | 51 | */ |
| 31 | 52 | filter(predicate) { |
| 32 | throw new Error('Remove this line and implement the function'); | |
| 53 | // Edge Case: Filtering an empty list | |
| 54 | const result = []; | |
| 55 | ||
| 56 | for (let i = 0; i < this.values.length; i++) { | |
| 57 | if (predicate(this.values[i])) { | |
| 58 | result.push(this.values[i]); | |
| 59 | } | |
| 60 | } | |
| 61 | ||
| 62 | return new List(result); | |
| 63 | // Handled Edge Cases: Filtering empty list | |
| 33 | 64 | } |
| 34 | 65 | |
| 35 | 66 | /** |
| 37 | 68 | * @return {List} |
| 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 | |
| 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: Mapping over empty list | |
| 41 | 80 | } |
| 42 | 81 | |
| 43 | 82 | /** |
| 44 | 83 | * @return {number} |
| 45 | 84 | */ |
| 46 | 85 | length() { |
| 47 | throw new Error('Remove this line and implement the function'); | |
| 86 | // Edge Case: Length of an empty list | |
| 87 | let count = 0; | |
| 88 | for (let i = 0; i < this.values.length; i++) { | |
| 89 | count++; | |
| 90 | } | |
| 91 | return count; | |
| 92 | // Handled Edge Cases: Empty list length | |
| 48 | 93 | } |
| 49 | 94 | |
| 50 | 95 | /** |
| 53 | 98 | * @return {any} |
| 54 | 99 | */ |
| 55 | 100 | foldl(callback, initial) { |
| 56 | throw new Error('Remove this line and implement the function'); | |
| 101 | // Edge Case: Folding an empty list | |
| 102 | let accumulator = initial; | |
| 103 | ||
| 104 | for (let i = 0; i < this.values.length; i++) { | |
| 105 | accumulator = callback(accumulator, this.values[i]); | |
| 106 | } | |
| 107 | ||
| 108 | return accumulator; | |
| 109 | // Handled Edge Cases: Folding empty list | |
| 57 | 110 | } |
| 58 | 111 | |
| 59 | 112 | /** |
| 62 | 115 | * @return {any} |
| 63 | 116 | */ |
| 64 | 117 | foldr(callback, initial) { |
| 65 | throw new Error('Remove this line and implement the function'); | |
| 118 | // Edge Case: Folding an empty list from the right | |
| 119 | let accumulator = initial; | |
| 120 | ||
| 121 | for (let i = this.values.length - 1; i >= 0; i--) { | |
| 122 | accumulator = callback(accumulator, this.values[i]); | |
| 123 | } | |
| 124 | ||
| 125 | return accumulator; | |
| 126 | // Handled Edge Cases: Folding empty list from right | |
| 66 | 127 | } |
| 67 | 128 | |
| 68 | 129 | /** |
| 69 | 130 | * @return {List} |
| 70 | 131 | */ |
| 71 | 132 | reverse() { |
| 72 | throw new Error('Remove this line and implement the function'); | |
| 133 | // Edge Case: Reversing an empty list | |
| 134 | const result = []; | |
| 135 | ||
| 136 | for (let i = this.values.length - 1; i >= 0; i--) { | |
| 137 | result.push(this.values[i]); | |
| 138 | } | |
| 139 | ||
| 140 | return new List(result); | |
| 141 | // Handled Edge Cases: Reversing empty list | |
| 73 | 142 | } |
| 74 | 143 | } |