| 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 | // Edge Case: Handle nested lists by flattening them |
| 44 | for (let j = 0; j < item.length(); j++) { |
| 45 | result.push(item.values[j]); |
| 46 | } |
| 47 | } else { |
| 48 | result.push(item); |
| 49 | } |
| 50 | } |
| 51 | return new List(result); |
| 25 | 52 | } |
| 26 | 53 | |
| 27 | 54 | /** |
| 37 | 75 | * @return {List} |
| 38 | 76 | */ |
| 39 | 77 | map(expression) { |
| 40 | | throw new Error('Remove this line and implement the function'); |
| 78 | // Edge Case: Handle when expression is not a function |
| 79 | if (typeof expression !== 'function') { |
| 80 | throw new Error('Expression must be a function'); |
| 81 | } |
| 82 | |
| 83 | const result = []; |
| 84 | for (let i = 0; i < this.values.length; i++) { |
| 85 | result.push(expression(this.values[i])); |
| 86 | } |
| 87 | return new List(result); |
| 41 | 88 | } |
| 42 | 89 | |
| 43 | 90 | /** |
| 44 | 91 | * @return {number} |
| 45 | 92 | */ |
| 46 | 93 | length() { |
| 47 | | throw new Error('Remove this line and implement the function'); |
| 94 | let count = 0; |
| 95 | // Edge Case: Handle empty list |
| 96 | for (let i = 0; i < this.values.length; i++) { |
| 97 | count++; |
| 98 | } |
| 99 | return count; |
| 48 | 100 | } |
| 49 | 101 | |
| 50 | 102 | /** |
| 53 | 105 | * @return {any} |
| 54 | 106 | */ |
| 55 | 107 | foldl(callback, initial) { |
| 56 | | throw new Error('Remove this line and implement the function'); |
| 108 | // Edge Case: Handle when callback is not a function |
| 109 | if (typeof callback !== 'function') { |
| 110 | throw new Error('Callback must be a function'); |
| 111 | } |
| 112 | |
| 113 | let accumulator = initial; |
| 114 | // Edge Case: Handle empty list |
| 115 | for (let i = 0; i < this.values.length; i++) { |
| 116 | accumulator = callback(accumulator, this.values[i]); |
| 117 | } |
| 118 | return accumulator; |
| 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 | if (typeof callback !== 'function') { |
| 129 | throw new Error('Callback must be a function'); |
| 130 | } |
| 131 | |
| 132 | let accumulator = initial; |
| 133 | // Edge Case: Handle empty list |
| 134 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 135 | accumulator = callback(accumulator, this.values[i]); |
| 136 | } |
| 137 | return accumulator; |
| 66 | 138 | } |
| 67 | 139 | |
| 68 | 140 | /** |
| 69 | 141 | * @return {List} |
| 70 | 142 | */ |
| 71 | 143 | reverse() { |
| 72 | | throw new Error('Remove this line and implement the function'); |
| 144 | const result = []; |
| 145 | // Edge Case: Handle empty list |
| 146 | for (let i = this.values.length - 1; i >= 0; i--) { |
| 147 | result.push(this.values[i]); |
| 148 | } |
| 149 | return new List(result); |
| 73 | 150 | } |
| 74 | 151 | } |
| 152 | |
| 153 | // Handled Edge Cases: |
| 154 | // - Handle when values is not an array by defaulting to empty array |
| 155 | // - Handle when other is not a List instance |
| 156 | // - Handle when others is not a List instance |
| 157 | // - Handle nested lists by flattening them |
| 158 | // - Handle when predicate is not a function |
| 159 | // - Handle empty list |
| 160 | // - Handle when expression is not a function |
| 161 | // - Handle when callback is not a function |