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