fishlium

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 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: Handle null or undefined other list
24 if (!other) {
25 return new List(this.values);
26 }
27
28 // Edge Case: Handle empty lists
29 if (this.length() === 0 && other.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 a list of lists into a single flattened list
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
2254 */
2355 concat(others) {
24 throw new Error('Remove this line and implement the function');
56 // Edge Case: Handle empty others list
57 if (!others || others.length() === 0) {
58 return new List(this.values);
59 }
60
61 const 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 list = others.values[i];
71 // Edge Case: Handle if an element in others is not a List
72 if (list instanceof List) {
73 for (let j = 0; j < list.values.length; j++) {
74 result.push(list.values[j]);
75 }
76 } else {
77 // If it's not a List, just add it as is
78 result.push(list);
79 }
80 }
81
82 return new List(result);
2583 }
2684
2785 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
86 * Filters the list based on a predicate function
87 * @param {(any) => boolean} predicate - Function to test each item
88 * @return {List} A new List containing only items that satisfy the predicate
89 *
90 * Handled Edge Cases: Empty list, predicate function throwing errors
3091 */
3192 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
93 // Edge Case: Handle empty list
94 if (this.length() === 0) {
95 return new List([]);
96 }
97
98 const result = [];
99
100 for (let i = 0; i < this.values.length; i++) {
101 try {
102 // Edge Case: Handle predicate function throwing errors
103 if (predicate(this.values[i])) {
104 result.push(this.values[i]);
105 }
106 } catch (error) {
107 // Silently skip items that cause predicate to throw
108 continue;
109 }
110 }
111
112 return new List(result);
33113 }
34114
35115 /**
36 * @param {(any) => any} expression
37 * @return {List}
116 * Maps each item in the list using a provided function
117 * @param {(any) => any} expression - Function to transform each item
118 * @return {List} A new List containing transformed items
119 *
120 * Handled Edge Cases: Empty list, expression function throwing errors
38121 */
39122 map(expression) {
40 throw new Error('Remove this line and implement the function');
123 // Edge Case: Handle empty list
124 if (this.length() === 0) {
125 return new List([]);
126 }
127
128 const result = [];
129
130 for (let i = 0; i < this.values.length; i++) {
131 try {
132 // Edge Case: Handle expression function throwing errors
133 result.push(expression(this.values[i]));
134 } catch (error) {
135 // Add undefined for items that cause expression to throw
136 result.push(undefined);
137 }
138 }
139
140 return new List(result);
41141 }
42142
43143 /**
44 * @return {number}
144 * Returns the number of items in the list
145 * @return {number} The length of the list
146 *
147 * Handled Edge Cases: Empty list, very large lists
45148 */
46149 length() {
47 throw new Error('Remove this line and implement the function');
150 // Edge Case: Handle empty list
151 if (!this.values) {
152 return 0;
153 }
154
155 let count = 0;
156 // Count items using a loop instead of built-in length
157 for (let i = 0; i < this.values.length; i++) {
158 count++;
159 }
160
161 return count;
48162 }
49163
50164 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
165 * Folds (reduces) the list from the left using a callback function
166 * @param {(acc: any, el: any) => any} callback - Function to apply to each item
167 * @param {any} initial - Initial accumulator value
168 * @return {any} The final accumulator value
169 *
170 * Handled Edge Cases: Empty list, callback function throwing errors
54171 */
55172 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
173 let accumulator = initial;
174
175 // Edge Case: Handle empty list
176 if (this.length() === 0) {
177 return accumulator;
178 }
179
180 for (let i = 0; i < this.values.length; i++) {
181 try {
182 // Edge Case: Handle callback function throwing errors
183 accumulator = callback(accumulator, this.values[i]);
184 } catch (error) {
185 // Continue with the current accumulator value if callback throws
186 continue;
187 }
188 }
189
190 return accumulator;
57191 }
58192
59193 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
194 * Folds (reduces) the list from the right using a callback function
195 * @param {(acc: any, el: any) => any} callback - Function to apply to each item
196 * @param {any} initial - Initial accumulator value
197 * @return {any} The final accumulator value
198 *
199 * Handled Edge Cases: Empty list, callback function throwing errors
63200 */
64201 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
202 let accumulator = initial;
203
204 // Edge Case: Handle empty list
205 if (this.length() === 0) {
206 return accumulator;
207 }
208
209 // Process from right to left
210 for (let i = this.values.length - 1; i >= 0; i--) {
211 try {
212 // Edge Case: Handle callback function throwing errors
213 accumulator = callback(accumulator, this.values[i]);
214 } catch (error) {
215 // Continue with the current accumulator value if callback throws
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: Handle empty list
231 if (this.length() === 0) {
232 return new List([]);
233 }
234
235 const result = [];
236
237 // Build reversed list
238 for (let i = this.values.length - 1; i >= 0; i--) {
239 result.push(this.values[i]);
240 }
241
242 return new List(result);
73243 }
74244}
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.