| 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 should return 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 should return 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 empty list should be 0 | |
| 87 | let count = 0; | |
| 88 | ||
| 89 | for (let i = 0; i < this.values.length; i++) { | |
| 90 | count++; | |
| 91 | } | |
| 92 | ||
| 93 | return count; | |
| 94 | // Handled Edge Cases: Empty list length | |
| 48 | 95 | } |
| 49 | 96 | |
| 50 | 97 | /** |
| 53 | 100 | * @return {any} |
| 54 | 101 | */ |
| 55 | 102 | foldl(callback, initial) { |
| 56 | throw new Error('Remove this line and implement the function'); | |
| 103 | // Edge Case: Folding an empty list should return the initial value | |
| 104 | let accumulator = initial; | |
| 105 | ||
| 106 | for (let i = 0; i < this.values.length; i++) { | |
| 107 | accumulator = callback(accumulator, this.values[i]); | |
| 108 | } | |
| 109 | ||
| 110 | return accumulator; | |
| 111 | // Handled Edge Cases: Folding empty list | |
| 57 | 112 | } |
| 58 | 113 | |
| 59 | 114 | /** |
| 62 | 117 | * @return {any} |
| 63 | 118 | */ |
| 64 | 119 | foldr(callback, initial) { |
| 65 | throw new Error('Remove this line and implement the function'); | |
| 120 | // Edge Case: Folding an empty list should return the initial value | |
| 121 | let accumulator = initial; | |
| 122 | ||
| 123 | for (let i = this.values.length - 1; i >= 0; i--) { | |
| 124 | accumulator = callback(accumulator, this.values[i]); | |
| 125 | } | |
| 126 | ||
| 127 | return accumulator; | |
| 128 | // Handled Edge Cases: Folding empty list | |
| 66 | 129 | } |
| 67 | 130 | |
| 68 | 131 | /** |
| 69 | 132 | * @return {List} |
| 70 | 133 | */ |
| 71 | 134 | reverse() { |
| 72 | throw new Error('Remove this line and implement the function'); | |
| 135 | // Edge Case: Reversing an empty list should return an empty list | |
| 136 | const result = []; | |
| 137 | ||
| 138 | for (let i = this.values.length - 1; i >= 0; i--) { | |
| 139 | result.push(this.values[i]); | |
| 140 | } | |
| 141 | ||
| 142 | return new List(result); | |
| 143 | // Handled Edge Cases: Reversing empty list | |
| 73 | 144 | } |
| 74 | 145 | } |