| 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 | // Edge Case: Handle when no values are provided, default to empty array | |
| 9 | this.values = values; | |
| 9 | 10 | } |
| 10 | 11 | |
| 11 | 12 | /** |
| 13 | 14 | * @return {List} |
| 14 | 15 | */ |
| 15 | 16 | append(other) { |
| 16 | throw new Error('Remove this line and implement the function'); | |
| 17 | // Edge Case: Handle when other list is empty | |
| 18 | const result = [...this.values]; | |
| 19 | for (let i = 0; i < other.length(); i++) { | |
| 20 | result.push(other.values[i]); | |
| 21 | } | |
| 22 | return new List(result); | |
| 23 | // Handled Edge Cases: other list being empty | |
| 17 | 24 | } |
| 18 | 25 | |
| 19 | 26 | /** |
| 21 | 28 | * @return {List} |
| 22 | 29 | */ |
| 23 | 30 | concat(others) { |
| 24 | throw new Error('Remove this line and implement the function'); | |
| 31 | // Edge Case: Handle when others list is empty | |
| 32 | let result = [...this.values]; | |
| 33 | for (let i = 0; i < others.length(); i++) { | |
| 34 | const list = others.values[i]; | |
| 35 | if (list instanceof List) { | |
| 36 | result = result.concat(list.values); | |
| 37 | } else { | |
| 38 | result.push(list); | |
| 39 | } | |
| 40 | } | |
| 41 | return new List(result); | |
| 42 | // Handled Edge Cases: others list being empty | |
| 25 | 43 | } |
| 26 | 44 | |
| 27 | 45 | /** |
| 29 | 47 | * @return {List} |
| 30 | 48 | */ |
| 31 | 49 | filter(predicate) { |
| 32 | throw new Error('Remove this line and implement the function'); | |
| 50 | // Edge Case: Handle when predicate is not a function | |
| 51 | if (typeof predicate !== 'function') { | |
| 52 | throw new Error('Predicate must be a function'); | |
| 53 | } | |
| 54 | ||
| 55 | const result = []; | |
| 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 | return new List(result); | |
| 62 | // Handled Edge Cases: predicate not being a function | |
| 33 | 63 | } |
| 34 | 64 | |
| 35 | 65 | /** |
| 37 | 67 | * @return {List} |
| 38 | 68 | */ |
| 39 | 69 | map(expression) { |
| 40 | throw new Error('Remove this line and implement the function'); | |
| 70 | // Edge Case: Handle when expression is not a function | |
| 71 | if (typeof expression !== 'function') { | |
| 72 | throw new Error('Expression must be a function'); | |
| 73 | } | |
| 74 | ||
| 75 | const result = []; | |
| 76 | for (let i = 0; i < this.values.length; i++) { | |
| 77 | result.push(expression(this.values[i])); | |
| 78 | } | |
| 79 | return new List(result); | |
| 80 | // Handled Edge Cases: expression not being a function | |
| 41 | 81 | } |
| 42 | 82 | |
| 43 | 83 | /** |
| 44 | 84 | * @return {number} |
| 45 | 85 | */ |
| 46 | 86 | length() { |
| 47 | throw new Error('Remove this line and implement the function'); | |
| 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 | |
| 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: Handle when callback is not a function | |
| 102 | if (typeof callback !== 'function') { | |
| 103 | throw new Error('Callback must be a function'); | |
| 104 | } | |
| 105 | ||
| 106 | let accumulator = initial; | |
| 107 | for (let i = 0; i < this.values.length; i++) { | |
| 108 | accumulator = callback(accumulator, this.values[i]); | |
| 109 | } | |
| 110 | return accumulator; | |
| 111 | // Handled Edge Cases: callback not being a function | |
| 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: Handle when callback is not a function | |
| 121 | if (typeof callback !== 'function') { | |
| 122 | throw new Error('Callback must be a function'); | |
| 123 | } | |
| 124 | ||
| 125 | let accumulator = initial; | |
| 126 | for (let i = this.values.length - 1; i >= 0; i--) { | |
| 127 | accumulator = callback(accumulator, this.values[i]); | |
| 128 | } | |
| 129 | return accumulator; | |
| 130 | // Handled Edge Cases: callback not being a function | |
| 66 | 131 | } |
| 67 | 132 | |
| 68 | 133 | /** |
| 69 | 134 | * @return {List} |
| 70 | 135 | */ |
| 71 | 136 | reverse() { |
| 72 | throw new Error('Remove this line and implement the function'); | |
| 137 | const result = []; | |
| 138 | for (let i = this.values.length - 1; i >= 0; i--) { | |
| 139 | result.push(this.values[i]); | |
| 140 | } | |
| 141 | return new List(result); | |
| 142 | // Handled Edge Cases: empty list | |
| 73 | 143 | } |
| 74 | 144 | } |