| 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 | const result = []; | |
| 55 | for (let i = 0; i < this.values.length; i++) { | |
| 56 | if (predicate(this.values[i])) { | |
| 57 | result.push(this.values[i]); | |
| 58 | } | |
| 59 | } | |
| 60 | return new List(result); | |
| 61 | // Handled Edge Cases: predicate not being a function | |
| 33 | 62 | } |
| 34 | 63 | |
| 35 | 64 | /** |
| 37 | 66 | * @return {List} |
| 38 | 67 | */ |
| 39 | 68 | map(expression) { |
| 40 | throw new Error('Remove this line and implement the function'); | |
| 69 | // Edge Case: Handle when expression is not a function | |
| 70 | if (typeof expression !== 'function') { | |
| 71 | throw new Error('Expression must be a function'); | |
| 72 | } | |
| 73 | const result = []; | |
| 74 | for (let i = 0; i < this.values.length; i++) { | |
| 75 | result.push(expression(this.values[i])); | |
| 76 | } | |
| 77 | return new List(result); | |
| 78 | // Handled Edge Cases: expression not being a function | |
| 41 | 79 | } |
| 42 | 80 | |
| 43 | 81 | /** |
| 44 | 82 | * @return {number} |
| 45 | 83 | */ |
| 46 | 84 | length() { |
| 47 | throw new Error('Remove this line and implement the function'); | |
| 85 | let count = 0; | |
| 86 | for (let i = 0; i < this.values.length; i++) { | |
| 87 | count++; | |
| 88 | } | |
| 89 | return count; | |
| 90 | // Handled Edge Cases: empty list | |
| 48 | 91 | } |
| 49 | 92 | |
| 50 | 93 | /** |
| 53 | 96 | * @return {any} |
| 54 | 97 | */ |
| 55 | 98 | foldl(callback, initial) { |
| 56 | throw new Error('Remove this line and implement the function'); | |
| 99 | // Edge Case: Handle when callback is not a function | |
| 100 | if (typeof callback !== 'function') { | |
| 101 | throw new Error('Callback must be a function'); | |
| 102 | } | |
| 103 | let accumulator = initial; | |
| 104 | for (let i = 0; i < this.values.length; i++) { | |
| 105 | accumulator = callback(accumulator, this.values[i]); | |
| 106 | } | |
| 107 | return accumulator; | |
| 108 | // Handled Edge Cases: callback not being a function | |
| 57 | 109 | } |
| 58 | 110 | |
| 59 | 111 | /** |
| 62 | 114 | * @return {any} |
| 63 | 115 | */ |
| 64 | 116 | foldr(callback, initial) { |
| 65 | throw new Error('Remove this line and implement the function'); | |
| 117 | // Edge Case: Handle when callback is not a function | |
| 118 | if (typeof callback !== 'function') { | |
| 119 | throw new Error('Callback must be a function'); | |
| 120 | } | |
| 121 | let accumulator = initial; | |
| 122 | for (let i = this.values.length - 1; i >= 0; i--) { | |
| 123 | accumulator = callback(accumulator, this.values[i]); | |
| 124 | } | |
| 125 | return accumulator; | |
| 126 | // Handled Edge Cases: callback not being a function | |
| 66 | 127 | } |
| 67 | 128 | |
| 68 | 129 | /** |
| 69 | 130 | * @return {List} |
| 70 | 131 | */ |
| 71 | 132 | reverse() { |
| 72 | throw new Error('Remove this line and implement the function'); | |
| 133 | const result = []; | |
| 134 | for (let i = this.values.length - 1; i >= 0; i--) { | |
| 135 | result.push(this.values[i]); | |
| 136 | } | |
| 137 | return new List(result); | |
| 138 | // Handled Edge Cases: empty list | |
| 73 | 139 | } |
| 74 | 140 | } |