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