let it go

Finished
44//
55
66export class List {
7 constructor() {
8 throw new Error('Remove this line and implement the function');
7 /**
8 * Creates a new List
9 * @param {Array} [values=[]] - Initial values for the list
10 */
11 constructor(values = []) {
12 this.values = values;
913 }
1014
1115 /**
12 * @param {List} other
13 * @return {List}
16 * Appends all items from another list to the end of this list
17 * @param {List} other - The list to append
18 * @return {List} A new list containing all items from both lists
19 *
20 * Handled Edge Cases: empty lists, null/undefined other list
1421 */
1522 append(other) {
16 throw new Error('Remove this line and implement the function');
23 // Edge Case: other list is null or undefined
24 if (other === null || other === undefined) {
25 return new List(this.values);
26 }
27
28 // Edge Case: both lists are empty
29 if (this.values.length === 0 && other.values.length === 0) {
30 return new List([]);
31 }
32
33 // Edge Case: this list is empty
34 if (this.values.length === 0) {
35 return new List(other.values);
36 }
37
38 // Edge Case: other list is empty
39 if (other.values.length === 0) {
40 return new List(this.values);
41 }
42
43 return new List([...this.values, ...other.values]);
1744 }
1845
1946 /**
20 * @param {List} others
21 * @return {List}
47 * Concatenates all items from a list of lists into one flattened list
48 * @param {List} others - A list containing other lists
49 * @return {List} A new list with all items from all lists flattened
50 *
51 * Handled Edge Cases: empty lists, nested empty lists
2252 */
2353 concat(others) {
24 throw new Error('Remove this line and implement the function');
54 // Edge Case: others list is null or undefined
55 if (others === null || others === undefined) {
56 return new List(this.values);
57 }
58
59 let result = [...this.values];
60
61 // Edge Case: others list is empty
62 if (others.values.length === 0) {
63 return new List(result);
64 }
65
66 for (const item of others.values) {
67 // Edge Case: item in others is not a List
68 if (item instanceof List) {
69 result = [...result, ...item.values];
70 } else {
71 // If item is not a List, add it directly
72 result.push(item);
73 }
74 }
75
76 return new List(result);
2577 }
2678
2779 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
80 * Filters the list based on a predicate function
81 * @param {(any) => boolean} predicate - Function to test each item
82 * @return {List} A new list containing only items that satisfy the predicate
83 *
84 * Handled Edge Cases: empty list, predicate function that throws
3085 */
3186 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
87 // Edge Case: empty list
88 if (this.values.length === 0) {
89 return new List([]);
90 }
91
92 // Edge Case: predicate is null or undefined
93 if (predicate === null || predicate === undefined) {
94 return new List([]);
95 }
96
97 const result = [];
98
99 for (const item of this.values) {
100 try {
101 if (predicate(item)) {
102 result.push(item);
103 }
104 } catch (error) {
105 // Edge Case: predicate function throws an error - skip the item
106 continue;
107 }
108 }
109
110 return new List(result);
33111 }
34112
35113 /**
36 * @param {(any) => any} expression
37 * @return {List}
114 * Maps each item in the list using a provided function
115 * @param {(any) => any} expression - Function to transform each item
116 * @return {List} A new list with transformed items
117 *
118 * Handled Edge Cases: empty list, expression function that throws
38119 */
39120 map(expression) {
40 throw new Error('Remove this line and implement the function');
121 // Edge Case: empty list
122 if (this.values.length === 0) {
123 return new List([]);
124 }
125
126 // Edge Case: expression is null or undefined
127 if (expression === null || expression === undefined) {
128 return new List([]);
129 }
130
131 const result = [];
132
133 for (const item of this.values) {
134 try {
135 result.push(expression(item));
136 } catch (error) {
137 // Edge Case: expression function throws an error - add undefined
138 result.push(undefined);
139 }
140 }
141
142 return new List(result);
41143 }
42144
43145 /**
44 * @return {number}
146 * Returns the length of the list
147 * @return {number} The number of items in the list
148 *
149 * Handled Edge Cases: empty list
45150 */
46151 length() {
47 throw new Error('Remove this line and implement the function');
152 // Edge Case: empty list
153 return this.values.length;
48154 }
49155
50156 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
157 * Folds (reduces) the list from the left using a callback function
158 * @param {(acc: any, el: any) => any} callback - Function to apply to each item
159 * @param {any} initial - Initial accumulator value
160 * @return {any} The final accumulator value
161 *
162 * Handled Edge Cases: empty list, callback function that throws
54163 */
55164 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
165 // Edge Case: callback is null or undefined
166 if (callback === null || callback === undefined) {
167 return initial;
168 }
169
170 let accumulator = initial;
171
172 // Edge Case: empty list
173 if (this.values.length === 0) {
174 return accumulator;
175 }
176
177 for (const item of this.values) {
178 try {
179 accumulator = callback(accumulator, item);
180 } catch (error) {
181 // Edge Case: callback function throws an error - continue with current accumulator
182 continue;
183 }
184 }
185
186 return accumulator;
57187 }
58188
59189 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
190 * Folds (reduces) the list from the right using a callback function
191 * @param {(acc: any, el: any) => any} callback - Function to apply to each item
192 * @param {any} initial - Initial accumulator value
193 * @return {any} The final accumulator value
194 *
195 * Handled Edge Cases: empty list, callback function that throws
63196 */
64197 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
198 // Edge Case: callback is null or undefined
199 if (callback === null || callback === undefined) {
200 return initial;
201 }
202
203 let accumulator = initial;
204
205 // Edge Case: empty list
206 if (this.values.length === 0) {
207 return accumulator;
208 }
209
210 // Process from right to left
211 for (let i = this.values.length - 1; i >= 0; i--) {
212 try {
213 accumulator = callback(accumulator, this.values[i]);
214 } catch (error) {
215 // Edge Case: callback function throws an error - continue with current accumulator
216 continue;
217 }
218 }
219
220 return accumulator;
66221 }
67222
68223 /**
69 * @return {List}
224 * Returns a new list with items in reverse order
225 * @return {List} A new list with items in reverse order
226 *
227 * Handled Edge Cases: empty list
70228 */
71229 reverse() {
72 throw new Error('Remove this line and implement the function');
230 // Edge Case: empty list
231 if (this.values.length === 0) {
232 return new List([]);
233 }
234
235 const result = [];
236 for (let i = this.values.length - 1; i >= 0; i--) {
237 result.push(this.values[i]);
238 }
239
240 return new List(result);
73241 }
74242}
Test NameStatus
empty lists
Pass
list to empty list
Pass
empty list to list
Pass
non-empty lists
Pass
empty list
Pass
list of lists
Pass
list of nested lists
Pass
empty list
Pass
non empty list
Pass
empty list
Pass
non-empty list
Pass
empty list
Pass
non-empty list
Pass
empty list
Pass
direction independent function applied to non-empty list
Pass
direction dependent function applied to non-empty list
Pass
empty list
Pass
direction independent function applied to non-empty list
Pass
direction dependent function applied to non-empty list
Pass
empty list
Pass
non-empty list
Pass
list of lists is not flattened
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.