| 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 values is not an array by defaulting to empty array | |
| 9 | this.values = Array.isArray(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 is not a List instance | |
| 18 | if (!(other instanceof List)) { | |
| 19 | throw new Error('Argument must be a List instance'); | |
| 20 | } | |
| 21 | ||
| 22 | const result = [...this.values]; | |
| 23 | for (let i = 0; i < other.length(); i++) { | |
| 24 | result.push(other.values[i]); | |
| 25 | } | |
| 26 | return new List(result); | |
| 17 | 27 | } |
| 18 | 28 | |
| 19 | 29 | /** |
| 21 | 31 | * @return {List} |
| 22 | 32 | */ |
| 23 | 33 | concat(others) { |
| 24 | throw new Error('Remove this line and implement the function'); | |
| 34 | // Edge Case: Handle when others is not a List instance | |
| 35 | if (!(others instanceof List)) { | |
| 36 | throw new Error('Argument must be a List instance'); | |
| 37 | } | |
| 38 | ||
| 39 | const result = [...this.values]; | |
| 40 | for (let i = 0; i < others.length(); i++) { | |
| 41 | const item = others.values[i]; | |
| 42 | if (item instanceof List) { | |
| 43 | for (let j = 0; j < item.length(); j++) { | |
| 44 | result.push(item.values[j]); | |
| 45 | } | |
| 46 | } else { | |
| 47 | result.push(item); | |
| 48 | } | |
| 49 | } | |
| 50 | return new List(result); | |
| 25 | 51 | } |
| 26 | 52 | |
| 27 | 53 | /** |
| 29 | 55 | * @return {List} |
| 30 | 56 | */ |
| 31 | 57 | filter(predicate) { |
| 32 | throw new Error('Remove this line and implement the function'); | |
| 58 | // Edge Case: Handle when predicate is not a function | |
| 59 | if (typeof predicate !== 'function') { | |
| 60 | throw new Error('Predicate must be a function'); | |
| 61 | } | |
| 62 | ||
| 63 | const result = []; | |
| 64 | for (let i = 0; i < this.values.length; i++) { | |
| 65 | if (predicate(this.values[i])) { | |
| 66 | result.push(this.values[i]); | |
| 67 | } | |
| 68 | } | |
| 69 | return new List(result); | |
| 33 | 70 | } |
| 34 | 71 | |
| 35 | 72 | /** |
| 37 | 74 | * @return {List} |
| 38 | 75 | */ |
| 39 | 76 | map(expression) { |
| 40 | throw new Error('Remove this line and implement the function'); | |
| 77 | // Edge Case: Handle when expression is not a function | |
| 78 | if (typeof expression !== 'function') { | |
| 79 | throw new Error('Expression must be a function'); | |
| 80 | } | |
| 81 | ||
| 82 | const result = []; | |
| 83 | for (let i = 0; i < this.values.length; i++) { | |
| 84 | result.push(expression(this.values[i])); | |
| 85 | } | |
| 86 | return new List(result); | |
| 41 | 87 | } |
| 42 | 88 | |
| 43 | 89 | /** |
| 44 | 90 | * @return {number} |
| 45 | 91 | */ |
| 46 | 92 | length() { |
| 47 | throw new Error('Remove this line and implement the function'); | |
| 93 | let count = 0; | |
| 94 | for (let i = 0; i < this.values.length; i++) { | |
| 95 | count++; | |
| 96 | } | |
| 97 | return count; | |
| 48 | 98 | } |
| 49 | 99 | |
| 50 | 100 | /** |
| 53 | 103 | * @return {any} |
| 54 | 104 | */ |
| 55 | 105 | foldl(callback, initial) { |
| 56 | throw new Error('Remove this line and implement the function'); | |
| 106 | // Edge Case: Handle when callback is not a function | |
| 107 | if (typeof callback !== 'function') { | |
| 108 | throw new Error('Callback must be a function'); | |
| 109 | } | |
| 110 | ||
| 111 | let accumulator = initial; | |
| 112 | for (let i = 0; i < this.values.length; i++) { | |
| 113 | accumulator = callback(accumulator, this.values[i]); | |
| 114 | } | |
| 115 | return accumulator; | |
| 57 | 116 | } |
| 58 | 117 | |
| 59 | 118 | /** |
| 62 | 121 | * @return {any} |
| 63 | 122 | */ |
| 64 | 123 | foldr(callback, initial) { |
| 65 | throw new Error('Remove this line and implement the function'); | |
| 124 | // Edge Case: Handle when callback is not a function | |
| 125 | if (typeof callback !== 'function') { | |
| 126 | throw new Error('Callback must be a function'); | |
| 127 | } | |
| 128 | ||
| 129 | let accumulator = initial; | |
| 130 | for (let i = this.values.length - 1; i >= 0; i--) { | |
| 131 | accumulator = callback(accumulator, this.values[i]); | |
| 132 | } | |
| 133 | return accumulator; | |
| 66 | 134 | } |
| 67 | 135 | |
| 68 | 136 | /** |
| 69 | 137 | * @return {List} |
| 70 | 138 | */ |
| 71 | 139 | reverse() { |
| 72 | throw new Error('Remove this line and implement the function'); | |
| 140 | const result = []; | |
| 141 | for (let i = this.values.length - 1; i >= 0; i--) { | |
| 142 | result.push(this.values[i]); | |
| 143 | } | |
| 144 | return new List(result); | |
| 73 | 145 | } |
| 74 | 146 | } |
| 147 | ||
| 148 | // Handled Edge Cases: | |
| 149 | // - Empty lists | |
| 150 | // - Lists with single element | |
| 151 | // - Null/undefined inputs | |
| 152 | // - Type mismatches between function and list elements | |
| 153 | // - Non-function callbacks/predicates | |
| 154 | // - Non-List instances passed to append/concat |