let it go

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 not a List instance
24 if (!(other instanceof List)) {
25 throw new Error('Argument must be a List');
26 }
27
28 // Edge Case: Handle empty lists
29 if (this.values.length === 0 && other.values.length === 0) {
30 return new List([]);
31 }
32
33 return new List([...this.values, ...other.values]);
1734 }
1835
1936 /**
20 * @param {List} others
21 * @return {List}
37 * Concatenates multiple lists into one.
38 * @param {List} others - A list of lists to concatenate.
39 * @return {List} A new list with all elements from all lists.
40 *
41 * Handled Edge Cases: Empty lists, nested empty lists
2242 */
2343 concat(others) {
24 throw new Error('Remove this line and implement the function');
44 // Edge Case: Handle when others is not a List instance
45 if (!(others instanceof List)) {
46 throw new Error('Argument must be a List');
47 }
48
49 let result = [...this.values];
50
51 // Edge Case: Handle empty others list
52 if (others.values.length === 0) {
53 return new List(result);
54 }
55
56 for (const list of others.values) {
57 // Edge Case: Handle when an element in others is not a List
58 if (!(list instanceof List)) {
59 throw new Error('All elements in others must be Lists');
60 }
61 result = [...result, ...list.values];
62 }
63
64 return new List(result);
2565 }
2666
2767 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
68 * Filters the list based on a predicate function.
69 * @param {(any) => boolean} predicate - Function to test each element.
70 * @return {List} A new list with elements that pass the predicate test.
71 *
72 * Handled Edge Cases: Empty list, predicate function throwing errors
3073 */
3174 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
75 // Edge Case: Handle when predicate is not a function
76 if (typeof predicate !== 'function') {
77 throw new Error('Predicate must be a function');
78 }
79
80 const result = [];
81
82 // Edge Case: Handle empty list
83 if (this.values.length === 0) {
84 return new List([]);
85 }
86
87 for (const value of this.values) {
88 try {
89 if (predicate(value)) {
90 result.push(value);
91 }
92 } catch (error) {
93 // Edge Case: Handle errors in predicate function
94 throw new Error(`Predicate function error: ${error.message}`);
95 }
96 }
97
98 return new List(result);
3399 }
34100
35101 /**
36 * @param {(any) => any} expression
37 * @return {List}
102 * Maps each element using a provided function.
103 * @param {(any) => any} expression - Function to transform each element.
104 * @return {List} A new list with transformed elements.
105 *
106 * Handled Edge Cases: Empty list, expression function throwing errors
38107 */
39108 map(expression) {
40 throw new Error('Remove this line and implement the function');
109 // Edge Case: Handle when expression is not a function
110 if (typeof expression !== 'function') {
111 throw new Error('Expression must be a function');
112 }
113
114 const result = [];
115
116 // Edge Case: Handle empty list
117 if (this.values.length === 0) {
118 return new List([]);
119 }
120
121 for (const value of this.values) {
122 try {
123 result.push(expression(value));
124 } catch (error) {
125 // Edge Case: Handle errors in expression function
126 throw new Error(`Expression function error: ${error.message}`);
127 }
128 }
129
130 return new List(result);
41131 }
42132
43133 /**
44 * @return {number}
134 * Returns the length of the list.
135 * @return {number} The number of elements in the list.
136 *
137 * Handled Edge Cases: None (this operation is always safe)
45138 */
46139 length() {
47 throw new Error('Remove this line and implement the function');
140 return this.values.length;
48141 }
49142
50143 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
144 * Folds the list from the left using a callback function.
145 * @param {(acc: any, el: any) => any} callback - Function to apply to each element.
146 * @param {any} initial - Initial accumulator value.
147 * @return {any} The final accumulator value.
148 *
149 * Handled Edge Cases: Empty list, callback function throwing errors
54150 */
55151 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
152 // Edge Case: Handle when callback is not a function
153 if (typeof callback !== 'function') {
154 throw new Error('Callback must be a function');
155 }
156
157 let accumulator = initial;
158
159 // Edge Case: Handle empty list
160 if (this.values.length === 0) {
161 return accumulator;
162 }
163
164 for (const value of this.values) {
165 try {
166 accumulator = callback(accumulator, value);
167 } catch (error) {
168 // Edge Case: Handle errors in callback function
169 throw new Error(`Callback function error: ${error.message}`);
170 }
171 }
172
173 return accumulator;
57174 }
58175
59176 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
177 * Folds the list from the right using a callback function.
178 * @param {(acc: any, el: any) => any} callback - Function to apply to each element.
179 * @param {any} initial - Initial accumulator value.
180 * @return {any} The final accumulator value.
181 *
182 * Handled Edge Cases: Empty list, callback function throwing errors
63183 */
64184 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
185 // Edge Case: Handle when callback is not a function
186 if (typeof callback !== 'function') {
187 throw new Error('Callback must be a function');
188 }
189
190 let accumulator = initial;
191
192 // Edge Case: Handle empty list
193 if (this.values.length === 0) {
194 return accumulator;
195 }
196
197 // Process from right to left
198 for (let i = this.values.length - 1; i >= 0; i--) {
199 try {
200 accumulator = callback(accumulator, this.values[i]);
201 } catch (error) {
202 // Edge Case: Handle errors in callback function
203 throw new Error(`Callback function error: ${error.message}`);
204 }
205 }
206
207 return accumulator;
66208 }
67209
68210 /**
69 * @return {List}
211 * Reverses the list.
212 * @return {List} A new list with elements in reverse order.
213 *
214 * Handled Edge Cases: Empty list
70215 */
71216 reverse() {
72 throw new Error('Remove this line and implement the function');
217 // Edge Case: Handle empty list
218 if (this.values.length === 0) {
219 return new List([]);
220 }
221
222 const result = [];
223 for (let i = this.values.length - 1; i >= 0; i--) {
224 result.push(this.values[i]);
225 }
226
227 return new List(result);
73228 }
74229}
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.