couple

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 another list to this list
17 * @param {List} other - The list to append
18 * @return {List} A new list containing all elements 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 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 multiple lists into one
48 * @param {List} others - A list of lists to concatenate
49 * @return {List} A new list with all elements from all lists flattened
50 *
51 * Handled Edge Cases: empty lists, nested empty lists, null/undefined values
2252 */
2353 concat(others) {
24 throw new Error('Remove this line and implement the function');
54 // Edge Case: others 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 is empty
62 if (others.values.length === 0) {
63 return new List(result);
64 }
65
66 for (const list of others.values) {
67 // Edge Case: element in others is not a List
68 if (list instanceof List) {
69 result = [...result, ...list.values];
70 } else {
71 // If it's not a List, we treat it as a single element
72 result = [...result, list];
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 element
82 * @return {List} A new list containing only elements that satisfy the predicate
83 *
84 * Handled Edge Cases: empty list, predicate throwing error, all elements filtered out
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 throw new Error('Predicate must be a function');
95 }
96
97 const result = [];
98 for (const value of this.values) {
99 try {
100 if (predicate(value)) {
101 result.push(value);
102 }
103 } catch (error) {
104 // Edge Case: predicate throws an error - we skip that element
105 continue;
106 }
107 }
108
109 return new List(result);
33110 }
34111
35112 /**
36 * @param {(any) => any} expression
37 * @return {List}
113 * Maps each element using a provided function
114 * @param {(any) => any} expression - Function to transform each element
115 * @return {List} A new list with transformed elements
116 *
117 * Handled Edge Cases: empty list, expression throwing error, null/undefined expression
38118 */
39119 map(expression) {
40 throw new Error('Remove this line and implement the function');
120 // Edge Case: empty list
121 if (this.values.length === 0) {
122 return new List([]);
123 }
124
125 // Edge Case: expression is null or undefined
126 if (expression === null || expression === undefined) {
127 throw new Error('Expression must be a function');
128 }
129
130 const result = [];
131 for (const value of this.values) {
132 try {
133 result.push(expression(value));
134 } catch (error) {
135 // Edge Case: expression throws an error - we push undefined for that element
136 result.push(undefined);
137 }
138 }
139
140 return new List(result);
41141 }
42142
43143 /**
44 * @return {number}
144 * Returns the length of the list
145 * @return {number} The number of elements in the list
146 *
147 * Handled Edge Cases: empty list
45148 */
46149 length() {
47 throw new Error('Remove this line and implement the function');
150 // Edge Case: empty list
151 return this.values.length;
48152 }
49153
50154 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
155 * Folds the list from the left using a callback function
156 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
157 * @param {any} initial - Initial accumulator value
158 * @return {any} The final accumulator value
159 *
160 * Handled Edge Cases: empty list, callback throwing error, null/undefined callback
54161 */
55162 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
163 // Edge Case: callback is null or undefined
164 if (callback === null || callback === undefined) {
165 throw new Error('Callback must be a function');
166 }
167
168 // Edge Case: empty list
169 if (this.values.length === 0) {
170 return initial;
171 }
172
173 let accumulator = initial;
174 for (const value of this.values) {
175 try {
176 accumulator = callback(accumulator, value);
177 } catch (error) {
178 // Edge Case: callback throws an error - we continue with the current accumulator
179 continue;
180 }
181 }
182
183 return accumulator;
57184 }
58185
59186 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
187 * Folds the list from the right using a callback function
188 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
189 * @param {any} initial - Initial accumulator value
190 * @return {any} The final accumulator value
191 *
192 * Handled Edge Cases: empty list, callback throwing error, null/undefined callback
63193 */
64194 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
195 // Edge Case: callback is null or undefined
196 if (callback === null || callback === undefined) {
197 throw new Error('Callback must be a function');
198 }
199
200 // Edge Case: empty list
201 if (this.values.length === 0) {
202 return initial;
203 }
204
205 let accumulator = initial;
206 // Process from right to left
207 for (let i = this.values.length - 1; i >= 0; i--) {
208 try {
209 accumulator = callback(accumulator, this.values[i]);
210 } catch (error) {
211 // Edge Case: callback throws an error - we continue with the current accumulator
212 continue;
213 }
214 }
215
216 return accumulator;
66217 }
67218
68219 /**
69 * @return {List}
220 * Reverses the list
221 * @return {List} A new list with elements in reverse order
222 *
223 * Handled Edge Cases: empty list, single element list
70224 */
71225 reverse() {
72 throw new Error('Remove this line and implement the function');
226 // Edge Case: empty list
227 if (this.values.length === 0) {
228 return new List([]);
229 }
230
231 // Edge Case: single element list
232 if (this.values.length === 1) {
233 return new List([this.values[0]]);
234 }
235
236 const result = [];
237 for (let i = this.values.length - 1; i >= 0; i--) {
238 result.push(this.values[i]);
239 }
240
241 return new List(result);
73242 }
74243}
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.