future

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, null/undefined others, empty nested lists
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.length() === 0) {
63 return new List(result);
64 }
65
66 for (let i = 0; i < others.values.length; i++) {
67 const list = others.values[i];
68 // Edge Case: nested list is null or undefined
69 if (list !== null && list !== undefined) {
70 if (Array.isArray(list)) {
71 // If it's a plain array, spread its elements
72 result = [...result, ...list];
73 } else if (Array.isArray(list.values)) {
74 // If it's a List object, use its values
75 result = [...result, ...list.values];
76 }
77 }
78 }
79
80 return new List(result);
2581 }
2682
2783 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
84 * Filters the list based on a predicate function
85 * @param {(any) => boolean} predicate - Function to test each element
86 * @return {List} A new list containing only elements that pass the predicate
87 *
88 * Handled Edge Cases: empty list, null/undefined predicate, predicate throws error
3089 */
3190 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
91 // Edge Case: empty list
92 if (this.values.length === 0) {
93 return new List([]);
94 }
95
96 // Edge Case: predicate is null or undefined
97 if (predicate === null || predicate === undefined) {
98 return new List([]);
99 }
100
101 const result = [];
102 for (let i = 0; i < this.values.length; i++) {
103 try {
104 if (predicate(this.values[i])) {
105 result.push(this.values[i]);
106 }
107 } catch (error) {
108 // Edge Case: predicate throws an error - skip the element
109 continue;
110 }
111 }
112
113 return new List(result);
33114 }
34115
35116 /**
36 * @param {(any) => any} expression
37 * @return {List}
117 * Maps each element using a provided function
118 * @param {(any) => any} expression - Function to transform each element
119 * @return {List} A new list with transformed elements
120 *
121 * Handled Edge Cases: empty list, null/undefined expression, expression throws error
38122 */
39123 map(expression) {
40 throw new Error('Remove this line and implement the function');
124 // Edge Case: empty list
125 if (this.values.length === 0) {
126 return new List([]);
127 }
128
129 // Edge Case: expression is null or undefined
130 if (expression === null || expression === undefined) {
131 return new List([]);
132 }
133
134 const result = [];
135 for (let i = 0; i < this.values.length; i++) {
136 try {
137 result.push(expression(this.values[i]));
138 } catch (error) {
139 // Edge Case: expression throws an error - push undefined
140 result.push(undefined);
141 }
142 }
143
144 return new List(result);
41145 }
42146
43147 /**
44 * @return {number}
148 * Returns the length of the list
149 * @return {number} The number of elements in the list
150 *
151 * Handled Edge Cases: empty list, very large lists
45152 */
46153 length() {
47 throw new Error('Remove this line and implement the function');
154 // Edge Case: empty list
155 if (!this.values) {
156 return 0;
157 }
158
159 return this.values.length;
48160 }
49161
50162 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
163 * Folds the list from the left using a callback function
164 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
165 * @param {any} initial - Initial accumulator value
166 * @return {any} The final accumulator value
167 *
168 * Handled Edge Cases: empty list, null/undefined callback, callback throws error
54169 */
55170 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
171 // Edge Case: callback is null or undefined
172 if (callback === null || callback === undefined) {
173 return initial;
174 }
175
176 // Edge Case: empty list
177 if (this.values.length === 0) {
178 return initial;
179 }
180
181 let accumulator = initial;
182 for (let i = 0; i < this.values.length; i++) {
183 try {
184 accumulator = callback(accumulator, this.values[i]);
185 } catch (error) {
186 // Edge Case: callback throws an error - return current accumulator
187 return accumulator;
188 }
189 }
190
191 return accumulator;
57192 }
58193
59194 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
195 * Folds the list from the right using a callback function
196 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
197 * @param {any} initial - Initial accumulator value
198 * @return {any} The final accumulator value
199 *
200 * Handled Edge Cases: empty list, null/undefined callback, callback throws error
63201 */
64202 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
203 // Edge Case: callback is null or undefined
204 if (callback === null || callback === undefined) {
205 return initial;
206 }
207
208 // Edge Case: empty list
209 if (this.values.length === 0) {
210 return initial;
211 }
212
213 let accumulator = initial;
214 for (let i = this.values.length - 1; i >= 0; i--) {
215 try {
216 accumulator = callback(accumulator, this.values[i]);
217 } catch (error) {
218 // Edge Case: callback throws an error - return current accumulator
219 return accumulator;
220 }
221 }
222
223 return accumulator;
66224 }
67225
68226 /**
69 * @return {List}
227 * Reverses the list
228 * @return {List} A new list with elements in reverse order
229 *
230 * Handled Edge Cases: empty list, single element list
70231 */
71232 reverse() {
72 throw new Error('Remove this line and implement the function');
233 // Edge Case: empty list
234 if (this.values.length === 0) {
235 return new List([]);
236 }
237
238 // Edge Case: single element list
239 if (this.values.length === 1) {
240 return new List([this.values[0]]);
241 }
242
243 const result = [];
244 for (let i = this.values.length - 1; i >= 0; i--) {
245 result.push(this.values[i]);
246 }
247
248 return new List(result);
73249 }
74250}
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.