| 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 | if (this.length() === 0) { | |
| 55 | return new List([]); | |
| 56 | } | |
| 57 | ||
| 58 | const result = []; | |
| 59 | for (let i = 0; i < this.values.length; i++) { | |
| 60 | if (predicate(this.values[i])) { | |
| 61 | result.push(this.values[i]); | |
| 62 | } | |
| 63 | } | |
| 64 | return new List(result); | |
| 65 | // Handled Edge Cases: Filtering empty list, predicate matching none, predicate matching all | |
| 33 | 66 | } |
| 34 | 67 | |
| 35 | 68 | /** |
| 37 | 70 | * @return {List} |
| 38 | 71 | */ |
| 39 | 72 | map(expression) { |
| 40 | throw new Error('Remove this line and implement the function'); | |
| 73 | // Edge Case: Mapping over an empty list | |
| 74 | if (this.length() === 0) { | |
| 75 | return new List([]); | |
| 76 | } | |
| 77 | ||
| 78 | const result = []; | |
| 79 | for (let i = 0; i < this.values.length; i++) { | |
| 80 | result.push(expression(this.values[i])); | |
| 81 | } | |
| 82 | return new List(result); | |
| 83 | // Handled Edge Cases: Mapping over empty list | |
| 41 | 84 | } |
| 42 | 85 | |
| 43 | 86 | /** |
| 44 | 87 | * @return {number} |
| 45 | 88 | */ |
| 46 | 89 | length() { |
| 47 | throw new Error('Remove this line and implement the function'); | |
| 90 | // Edge Case: Length of empty list | |
| 91 | let count = 0; | |
| 92 | for (let i = 0; i < this.values.length; i++) { | |
| 93 | count++; | |
| 94 | } | |
| 95 | return count; | |
| 96 | // Handled Edge Cases: Empty list | |
| 48 | 97 | } |
| 49 | 98 | |
| 50 | 99 | /** |
| 53 | 102 | * @return {any} |
| 54 | 103 | */ |
| 55 | 104 | foldl(callback, initial) { |
| 56 | throw new Error('Remove this line and implement the function'); | |
| 105 | // Edge Case: Folding an empty list | |
| 106 | if (this.length() === 0) { | |
| 107 | return initial; | |
| 108 | } | |
| 109 | ||
| 110 | let accumulator = initial; | |
| 111 | for (let i = 0; i < this.values.length; i++) { | |
| 112 | accumulator = callback(accumulator, this.values[i]); | |
| 113 | } | |
| 114 | return accumulator; | |
| 115 | // Handled Edge Cases: Folding empty list | |
| 57 | 116 | } |
| 58 | 117 | |
| 59 | 118 | /** |
| 62 | 121 | * @return {any} |
| 63 | 122 | */ |
| 64 | 123 | foldr(callback, initial) { |
| 65 | throw new Error('Remove this line and implement the function'); | |
| 124 | // Edge Case: Folding an empty list | |
| 125 | if (this.length() === 0) { | |
| 126 | return initial; | |
| 127 | } | |
| 128 | ||
| 129 | let accumulator = initial; | |
| 130 | for (let i = this.values.length - 1; i >= 0; i--) { | |
| 131 | accumulator = callback(accumulator, this.values[i]); | |
| 132 | } | |
| 133 | return accumulator; | |
| 134 | // Handled Edge Cases: Folding empty list | |
| 66 | 135 | } |
| 67 | 136 | |
| 68 | 137 | /** |
| 69 | 138 | * @return {List} |
| 70 | 139 | */ |
| 71 | 140 | reverse() { |
| 72 | throw new Error('Remove this line and implement the function'); | |
| 141 | // Edge Case: Reversing an empty list | |
| 142 | if (this.length() === 0) { | |
| 143 | return new List([]); | |
| 144 | } | |
| 145 | ||
| 146 | const result = []; | |
| 147 | for (let i = this.values.length - 1; i >= 0; i--) { | |
| 148 | result.push(this.values[i]); | |
| 149 | } | |
| 150 | return new List(result); | |
| 151 | // Handled Edge Cases: Reversing empty list | |
| 73 | 152 | } |
| 74 | 153 | } |