xas

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 array of 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 const result = [];
34
35 // Add all items from this list
36 for (let i = 0; i < this.values.length; i++) {
37 result.push(this.values[i]);
38 }
39
40 // Add all items from the other list
41 for (let i = 0; i < other.values.length; i++) {
42 result.push(other.values[i]);
43 }
44
45 return new List(result);
1746 }
1847
1948 /**
20 * @param {List} others
21 * @return {List}
49 * Concatenates this list with a list of lists
50 * @param {List} others - A list containing other lists to concatenate
51 * @return {List} A new List with all items from all lists flattened
52 *
53 * Handled Edge Cases: empty lists, nested empty lists, null/undefined values
2254 */
2355 concat(others) {
24 throw new Error('Remove this line and implement the function');
56 // Edge Case: others is null or undefined
57 if (others === null || others === undefined) {
58 return new List(this.values);
59 }
60
61 let result = [];
62
63 // Add all items from this list
64 for (let i = 0; i < this.values.length; i++) {
65 result.push(this.values[i]);
66 }
67
68 // Add all items from each list in others
69 for (let i = 0; i < others.values.length; i++) {
70 const item = others.values[i];
71
72 // Edge Case: item is a List object
73 if (item instanceof List) {
74 for (let j = 0; j < item.values.length; j++) {
75 result.push(item.values[j]);
76 }
77 } else {
78 // Edge Case: item is a regular value
79 result.push(item);
80 }
81 }
82
83 return new List(result);
2584 }
2685
2786 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
87 * Filters the list based on a predicate function
88 * @param {Function} predicate - Function that returns true for items to keep
89 * @return {List} A new List containing only items where predicate returns true
90 *
91 * Handled Edge Cases: empty list, predicate function throwing errors, all items filtered out
3092 */
3193 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
94 // Edge Case: empty list
95 if (this.values.length === 0) {
96 return new List([]);
97 }
98
99 const result = [];
100
101 for (let i = 0; i < this.values.length; i++) {
102 const item = this.values[i];
103
104 // Edge Case: predicate function throws an error
105 try {
106 if (predicate(item)) {
107 result.push(item);
108 }
109 } catch (error) {
110 // Silently skip items that cause predicate to throw
111 continue;
112 }
113 }
114
115 return new List(result);
33116 }
34117
35118 /**
36 * @param {(any) => any} expression
37 * @return {List}
119 * Maps each item in the list using the provided expression function
120 * @param {Function} expression - Function to apply to each item
121 * @return {List} A new List containing the results of applying expression to each item
122 *
123 * Handled Edge Cases: empty list, expression function throwing errors
38124 */
39125 map(expression) {
40 throw new Error('Remove this line and implement the function');
126 // Edge Case: empty list
127 if (this.values.length === 0) {
128 return new List([]);
129 }
130
131 const result = [];
132
133 for (let i = 0; i < this.values.length; i++) {
134 const item = this.values[i];
135
136 // Edge Case: expression function throws an error
137 try {
138 result.push(expression(item));
139 } catch (error) {
140 // Push undefined if expression throws
141 result.push(undefined);
142 }
143 }
144
145 return new List(result);
41146 }
42147
43148 /**
44 * @return {number}
149 * Returns the length of the list
150 * @return {number} The number of items in the list
151 *
152 * Handled Edge Cases: empty list
45153 */
46154 length() {
47 throw new Error('Remove this line and implement the function');
155 // Edge Case: empty list
156 let count = 0;
157 for (let i = 0; i < this.values.length; i++) {
158 count++;
159 }
160 return count;
48161 }
49162
50163 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
164 * Folds the list from the left using a callback function and initial accumulator
165 * @param {Function} callback - Function that takes accumulator and current element
166 * @param {any} initial - Initial value for the accumulator
167 * @return {any} The final accumulated value
168 *
169 * Handled Edge Cases: empty list, callback function throwing errors
54170 */
55171 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
172 let accumulator = initial;
173
174 // Edge Case: empty list
175 if (this.values.length === 0) {
176 return accumulator;
177 }
178
179 for (let i = 0; i < this.values.length; i++) {
180 const item = this.values[i];
181
182 // Edge Case: callback function throws an error
183 try {
184 accumulator = callback(accumulator, item);
185 } catch (error) {
186 // Continue with current accumulator if callback throws
187 continue;
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 and initial accumulator
196 * @param {Function} callback - Function that takes accumulator and current element
197 * @param {any} initial - Initial value for the accumulator
198 * @return {any} The final accumulated value
199 *
200 * Handled Edge Cases: empty list, callback function throwing errors
63201 */
64202 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
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 const item = this.values[i];
213
214 // Edge Case: callback function throws an error
215 try {
216 accumulator = callback(accumulator, item);
217 } catch (error) {
218 // Continue with current accumulator if callback throws
219 continue;
220 }
221 }
222
223 return accumulator;
66224 }
67225
68226 /**
69 * @return {List}
227 * Returns a new list with all items in reverse order
228 * @return {List} A new List with items in reverse order
229 *
230 * Handled Edge Cases: empty list, single item 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 item list
239 if (this.values.length === 1) {
240 return new List([this.values[0]]);
241 }
242
243 const result = [];
244
245 // Build reversed list
246 for (let i = this.values.length - 1; i >= 0; i--) {
247 result.push(this.values[i]);
248 }
249
250 return new List(result);
73251 }
74252}
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.