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 instance.
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 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: Handle when other is null or undefined
24 if (!other) {
25 return new List(this.values);
26 }
27
28 // Edge Case: Handle when this list or other list is empty
29 if (this.values.length === 0 && other.values.length === 0) {
30 return new List([]);
31 } else if (this.values.length === 0) {
32 return new List(other.values);
33 } else if (other.values.length === 0) {
34 return new List(this.values);
35 }
36
37 return new List([...this.values, ...other.values]);
1738 }
1839
1940 /**
20 * @param {List} others
21 * @return {List}
41 * Concatenates multiple lists into one.
42 * @param {List} others - A list of lists to concatenate.
43 * @return {List} A new list with all elements from all lists.
44 *
45 * Handled Edge Cases: empty lists, nested empty lists
2246 */
2347 concat(others) {
24 throw new Error('Remove this line and implement the function');
48 let result = [...this.values];
49
50 // Edge Case: Handle when others is null or undefined
51 if (!others || !others.values) {
52 return new List(result);
53 }
54
55 // Edge Case: Handle when others list is empty
56 if (others.values.length === 0) {
57 return new List(result);
58 }
59
60 for (const list of others.values) {
61 // Edge Case: Handle when an element in others is not a List
62 if (list instanceof List) {
63 result = [...result, ...list.values];
64 } else {
65 // If it's not a List, we treat it as a single element
66 result = [...result, list];
67 }
68 }
69
70 return new List(result);
2571 }
2672
2773 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
74 * Filters the list based on a predicate function.
75 * @param {(any) => boolean} predicate - Function to test each element.
76 * @return {List} A new list with elements that pass the predicate test.
77 *
78 * Handled Edge Cases: empty list, predicate function throws error
3079 */
3180 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
81 // Edge Case: Handle when this list is empty
82 if (this.values.length === 0) {
83 return new List([]);
84 }
85
86 // Edge Case: Handle when predicate is null or undefined
87 if (typeof predicate !== 'function') {
88 return new List([]);
89 }
90
91 const result = [];
92 for (const item of this.values) {
93 try {
94 if (predicate(item)) {
95 result.push(item);
96 }
97 } catch (error) {
98 // Edge Case: If predicate throws an error, we skip that element
99 continue;
100 }
101 }
102
103 return new List(result);
33104 }
34105
35106 /**
36 * @param {(any) => any} expression
37 * @return {List}
107 * Maps each element using a provided function.
108 * @param {(any) => any} expression - Function to transform each element.
109 * @return {List} A new list with transformed elements.
110 *
111 * Handled Edge Cases: empty list, expression function throws error
38112 */
39113 map(expression) {
40 throw new Error('Remove this line and implement the function');
114 // Edge Case: Handle when this list is empty
115 if (this.values.length === 0) {
116 return new List([]);
117 }
118
119 // Edge Case: Handle when expression is null or undefined
120 if (typeof expression !== 'function') {
121 return new List([]);
122 }
123
124 const result = [];
125 for (const item of this.values) {
126 try {
127 result.push(expression(item));
128 } catch (error) {
129 // Edge Case: If expression throws an error, we push undefined
130 result.push(undefined);
131 }
132 }
133
134 return new List(result);
41135 }
42136
43137 /**
44 * @return {number}
138 * Returns the length of the list.
139 * @return {number} The number of elements in the list.
140 *
141 * Handled Edge Cases: empty list
45142 */
46143 length() {
47 throw new Error('Remove this line and implement the function');
144 // Edge Case: Handle when this list is empty
145 return this.values.length;
48146 }
49147
50148 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
149 * Folds the list from the left using a callback function.
150 * @param {(acc: any, el: any) => any} callback - Function to apply to each element.
151 * @param {any} initial - Initial accumulator value.
152 * @return {any} The final accumulator value.
153 *
154 * Handled Edge Cases: empty list, callback function throws error
54155 */
55156 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
157 // Edge Case: Handle when this list is empty
158 if (this.values.length === 0) {
159 return initial;
160 }
161
162 // Edge Case: Handle when callback is null or undefined
163 if (typeof callback !== 'function') {
164 return initial;
165 }
166
167 let accumulator = initial;
168 for (const item of this.values) {
169 try {
170 accumulator = callback(accumulator, item);
171 } catch (error) {
172 // Edge Case: If callback throws an error, we continue with current accumulator
173 continue;
174 }
175 }
176
177 return accumulator;
57178 }
58179
59180 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
181 * Folds the list from the right using a callback function.
182 * @param {(acc: any, el: any) => any} callback - Function to apply to each element.
183 * @param {any} initial - Initial accumulator value.
184 * @return {any} The final accumulator value.
185 *
186 * Handled Edge Cases: empty list, callback function throws error
63187 */
64188 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
189 // Edge Case: Handle when this list is empty
190 if (this.values.length === 0) {
191 return initial;
192 }
193
194 // Edge Case: Handle when callback is null or undefined
195 if (typeof callback !== 'function') {
196 return initial;
197 }
198
199 let accumulator = initial;
200 // Process from right to left
201 for (let i = this.values.length - 1; i >= 0; i--) {
202 try {
203 accumulator = callback(accumulator, this.values[i]);
204 } catch (error) {
205 // Edge Case: If callback throws an error, we continue with current accumulator
206 continue;
207 }
208 }
209
210 return accumulator;
66211 }
67212
68213 /**
69 * @return {List}
214 * Reverses the list.
215 * @return {List} A new list with elements in reverse order.
216 *
217 * Handled Edge Cases: empty list
70218 */
71219 reverse() {
72 throw new Error('Remove this line and implement the function');
220 // Edge Case: Handle when this list is empty
221 if (this.values.length === 0) {
222 return new List([]);
223 }
224
225 return new List([...this.values].reverse());
73226 }
74227}
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.