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 // Edge Case: Handle undefined/null input by defaulting to empty array
13 this.values = Array.isArray(values) ? values : [];
914 }
1015
1116 /**
12 * @param {List} other
13 * @return {List}
17 * Appends all items from another list to the end of this list
18 * @param {List} other - The list to append
19 * @return {List} A new list containing all items from both lists
20 *
21 * Handled Edge Cases: empty lists, null/undefined other list, non-List objects
1422 */
1523 append(other) {
16 throw new Error('Remove this line and implement the function');
24 // Edge Case: Handle undefined or null other list
25 if (!other || !(other instanceof List)) {
26 return new List(this.values);
27 }
28
29 // Edge Case: Handle empty lists
30 if (this.values.length === 0 && other.values.length === 0) {
31 return new List([]);
32 }
33
34 // Edge Case: Handle empty this list
35 if (this.values.length === 0) {
36 return new List(other.values);
37 }
38
39 // Edge Case: Handle empty other list
40 if (other.values.length === 0) {
41 return new List(this.values);
42 }
43
44 return new List([...this.values, ...other.values]);
1745 }
1846
1947 /**
20 * @param {List} others
21 * @return {List}
48 * Concatenates all items from a list of lists into a single flattened list
49 * @param {List} others - A list containing other lists to concatenate
50 * @return {List} A new list with all items from all lists flattened
51 *
52 * Handled Edge Cases: empty lists, nested empty lists, null/undefined values, non-List objects
2253 */
2354 concat(others) {
24 throw new Error('Remove this line and implement the function');
55 // Edge Case: Handle undefined or null others
56 if (!others || !(others instanceof List)) {
57 return new List(this.values);
58 }
59
60 let result = [...this.values];
61
62 // Edge Case: Handle empty others list
63 if (others.values.length === 0) {
64 return new List(result);
65 }
66
67 for (const item of others.values) {
68 // Edge Case: Handle if item is a List object
69 if (item instanceof List) {
70 result = [...result, ...item.values];
71 }
72 // Edge Case: Handle if item is an array
73 else if (Array.isArray(item)) {
74 result = [...result, ...item];
75 }
76 // Edge Case: Handle primitive values
77 else {
78 result.push(item);
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, null/undefined predicate
3091 */
3192 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
93 // Edge Case: Handle undefined or null predicate
94 if (typeof predicate !== 'function') {
95 return new List([]);
96 }
97
98 // Edge Case: Handle empty list
99 if (this.values.length === 0) {
100 return new List([]);
101 }
102
103 const result = [];
104
105 for (const item of this.values) {
106 try {
107 // Edge Case: Handle predicate function throwing errors
108 // Edge Case: Predicate is a pure function that doesn't modify original items
109 if (predicate(item)) {
110 // Create a copy to ensure original items aren't modified
111 result.push(typeof item === 'object' && item !== null ? {...item} : item);
112 }
113 } catch (error) {
114 // Silently skip items that cause predicate to throw
115 continue;
116 }
117 }
118
119 return new List(result);
33120 }
34121
35122 /**
36 * @param {(any) => any} expression
37 * @return {List}
123 * Maps each item in the list using a transformation function
124 * @param {(any) => any} expression - Function to transform each item
125 * @return {List} A new list containing transformed items
126 *
127 * Handled Edge Cases: empty list, expression function throwing errors, null/undefined expression
38128 */
39129 map(expression) {
40 throw new Error('Remove this line and implement the function');
130 // Edge Case: Handle undefined or null expression
131 if (typeof expression !== 'function') {
132 return new List([]);
133 }
134
135 // Edge Case: Handle empty list
136 if (this.values.length === 0) {
137 return new List([]);
138 }
139
140 const result = [];
141
142 for (const item of this.values) {
143 try {
144 // Edge Case: Handle expression function throwing errors
145 // Edge Case: Expression is a pure function that doesn't modify original items
146 const transformed = expression(item);
147 // Create a copy to ensure original items aren't modified
148 result.push(typeof transformed === 'object' && transformed !== null ? {...transformed} : transformed);
149 } catch (error) {
150 // Add undefined for items that cause expression to throw
151 result.push(undefined);
152 }
153 }
154
155 return new List(result);
41156 }
42157
43158 /**
44 * @return {number}
159 * Returns the length of the list
160 * @return {number} The number of items in the list
161 *
162 * Handled Edge Cases: empty list
45163 */
46164 length() {
47 throw new Error('Remove this line and implement the function');
165 // Edge Case: Handle empty list
166 return this.values.length;
48167 }
49168
50169 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
170 * Folds (reduces) the list from the left using a callback function
171 * @param {(acc: any, el: any) => any} callback - Function to apply to each item
172 * @param {any} initial - Initial accumulator value
173 * @return {any} The final accumulator value
174 *
175 * Handled Edge Cases: empty list, callback function throwing errors, null/undefined callback
54176 */
55177 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
178 // Edge Case: Handle undefined or null callback
179 if (typeof callback !== 'function') {
180 return initial;
181 }
182
183 // Edge Case: Handle empty list
184 if (this.values.length === 0) {
185 return initial;
186 }
187
188 let accumulator = initial;
189
190 for (const item of this.values) {
191 try {
192 // Edge Case: Handle callback function throwing errors
193 // Following foldl signature: foldl(func, acc, item)
194 accumulator = callback(accumulator, item);
195 } catch (error) {
196 // Continue with current accumulator if callback throws
197 continue;
198 }
199 }
200
201 return accumulator;
57202 }
58203
59204 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
205 * Folds (reduces) the list from the right using a callback function
206 * @param {(el: any, acc: any) => any} callback - Function to apply to each item
207 * @param {any} initial - Initial accumulator value
208 * @return {any} The final accumulator value
209 *
210 * Handled Edge Cases: empty list, callback function throwing errors, null/undefined callback
63211 */
64212 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
213 // Edge Case: Handle undefined or null callback
214 if (typeof callback !== 'function') {
215 return initial;
216 }
217
218 // Edge Case: Handle empty list
219 if (this.values.length === 0) {
220 return initial;
221 }
222
223 let accumulator = initial;
224
225 // Process from right to left
226 for (let i = this.values.length - 1; i >= 0; i--) {
227 try {
228 // Edge Case: Handle callback function throwing errors
229 // Following foldr signature: foldr(func, item, acc)
230 accumulator = callback(this.values[i], accumulator);
231 } catch (error) {
232 // Continue with current accumulator if callback throws
233 continue;
234 }
235 }
236
237 return accumulator;
66238 }
67239
68240 /**
69 * @return {List}
241 * Returns a new list with items in reverse order
242 * @return {List} A new list with items in reverse order
243 *
244 * Handled Edge Cases: empty list
70245 */
71246 reverse() {
72 throw new Error('Remove this line and implement the function');
247 // Edge Case: Handle empty list
248 return new List([...this.values].reverse());
73249 }
74250}
Test NameStatus
direction dependent function applied to non-empty list
Fail
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
empty list
Pass
non-empty list
Pass
list of lists is not flattened
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.