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