couple

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: If other is not a List instance, we treat it as an empty list
24 if (!(other instanceof List)) {
25 other = new 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 if (this.values.length === 0) {
34 return new List(other.values);
35 }
36
37 if (other.values.length === 0) {
38 return new List(this.values);
39 }
40
41 return new List([...this.values, ...other.values]);
1742 }
1843
1944 /**
20 * @param {List} others
21 * @return {List}
45 * Concatenates multiple lists into one.
46 * @param {List} others - A list of lists to concatenate.
47 * @return {List} A new list with all elements flattened.
48 *
49 * Handled Edge Cases: Empty lists, nested empty lists
2250 */
2351 concat(others) {
24 throw new Error('Remove this line and implement the function');
52 // Edge Case: If others is not a List instance, treat as empty list
53 if (!(others instanceof List)) {
54 return new List(this.values);
55 }
56
57 let result = [...this.values];
58
59 // Edge Case: Handle empty others list
60 if (others.values.length === 0) {
61 return new List(result);
62 }
63
64 for (const item of others.values) {
65 if (item instanceof List) {
66 result = [...result, ...item.values];
67 } else {
68 // Edge Case: If item is not a List, add it directly
69 result.push(item);
70 }
71 }
72
73 return new List(result);
2574 }
2675
2776 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
77 * Filters the list based on a predicate function.
78 * @param {(any) => boolean} predicate - Function to test each element.
79 * @return {List} A new list with elements that pass the predicate test.
80 *
81 * Handled Edge Cases: Empty list, null/undefined predicate
3082 */
3183 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
84 // Edge Case: If predicate is not a function, return empty list
85 if (typeof predicate !== 'function') {
86 return new List();
87 }
88
89 // Edge Case: Handle empty list
90 if (this.values.length === 0) {
91 return new List();
92 }
93
94 const result = [];
95 for (const item of this.values) {
96 if (predicate(item)) {
97 result.push(item);
98 }
99 }
100
101 return new List(result);
33102 }
34103
35104 /**
36 * @param {(any) => any} expression
37 * @return {List}
105 * Maps each element using a provided function.
106 * @param {(any) => any} expression - Function to transform each element.
107 * @return {List} A new list with transformed elements.
108 *
109 * Handled Edge Cases: Empty list, null/undefined expression
38110 */
39111 map(expression) {
40 throw new Error('Remove this line and implement the function');
112 // Edge Case: If expression is not a function, return empty list
113 if (typeof expression !== 'function') {
114 return new List();
115 }
116
117 // Edge Case: Handle empty list
118 if (this.values.length === 0) {
119 return new List();
120 }
121
122 const result = [];
123 for (const item of this.values) {
124 result.push(expression(item));
125 }
126
127 return new List(result);
41128 }
42129
43130 /**
44 * @return {number}
131 * Returns the length of the list.
132 * @return {number} The number of elements in the list.
133 *
134 * Handled Edge Cases: Empty list
45135 */
46136 length() {
47 throw new Error('Remove this line and implement the function');
137 // Edge Case: Handle empty list
138 let count = 0;
139 for (const _ of this.values) {
140 count++;
141 }
142 return count;
48143 }
49144
50145 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
146 * Folds the list from the left using a callback function.
147 * @param {(acc: any, el: any) => any} callback - Function to apply to each element.
148 * @param {any} initial - Initial accumulator value.
149 * @return {any} The final accumulator value.
150 *
151 * Handled Edge Cases: Empty list, null/undefined callback
54152 */
55153 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
154 // Edge Case: If callback is not a function, return initial value
155 if (typeof callback !== 'function') {
156 return initial;
157 }
158
159 // Edge Case: Handle empty list
160 if (this.values.length === 0) {
161 return initial;
162 }
163
164 let accumulator = initial;
165 for (const item of this.values) {
166 accumulator = callback(accumulator, item);
167 }
168
169 return accumulator;
57170 }
58171
59172 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
173 * Folds the list from the right using a callback function.
174 * @param {(acc: any, el: any) => any} callback - Function to apply to each element.
175 * @param {any} initial - Initial accumulator value.
176 * @return {any} The final accumulator value.
177 *
178 * Handled Edge Cases: Empty list, null/undefined callback
63179 */
64180 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
181 // Edge Case: If callback is not a function, return initial value
182 if (typeof callback !== 'function') {
183 return initial;
184 }
185
186 // Edge Case: Handle empty list
187 if (this.values.length === 0) {
188 return initial;
189 }
190
191 let accumulator = initial;
192 // Process from right to left
193 for (let i = this.values.length - 1; i >= 0; i--) {
194 accumulator = callback(accumulator, this.values[i]);
195 }
196
197 return accumulator;
66198 }
67199
68200 /**
69 * @return {List}
201 * Reverses the list.
202 * @return {List} A new list with elements in reverse order.
203 *
204 * Handled Edge Cases: Empty list, single element list
70205 */
71206 reverse() {
72 throw new Error('Remove this line and implement the function');
207 // Edge Case: Handle empty list
208 if (this.values.length === 0) {
209 return new List();
210 }
211
212 // Edge Case: Handle single element list
213 if (this.values.length === 1) {
214 return new List([this.values[0]]);
215 }
216
217 const result = [];
218 for (let i = this.values.length - 1; i >= 0; i--) {
219 result.push(this.values[i]);
220 }
221
222 return new List(result);
73223 }
74224}
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.