corpus

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: 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.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
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.values.length === 0) {
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 list = others.values[i];
71 // Edge Case: Handle if an element in others is not a List
72 if (list && Array.isArray(list.values)) {
73 for (let j = 0; j < list.values.length; j++) {
74 result.push(list.values[j]);
75 }
76 }
77 }
78
79 return new List(result);
2580 }
2681
2782 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
83 * Filters the list based on a predicate function
84 * @param {(any) => boolean} predicate - Function to test each element
85 * @return {List} A new List containing only elements that satisfy the predicate
86 *
87 * Handled Edge Cases: Empty list, predicate returning all false, predicate returning all true
3088 */
3189 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
90 // Edge Case: Handle empty list
91 if (this.values.length === 0) {
92 return new List([]);
93 }
94
95 const result = [];
96
97 for (let i = 0; i < this.values.length; i++) {
98 // Edge Case: Handle if predicate is not a function
99 if (typeof predicate === 'function' && predicate(this.values[i])) {
100 result.push(this.values[i]);
101 }
102 }
103
104 return new List(result);
33105 }
34106
35107 /**
36 * @param {(any) => any} expression
37 * @return {List}
108 * Transforms each element in the list using a provided function
109 * @param {(any) => any} expression - Function to transform each element
110 * @return {List} A new List containing transformed elements
111 *
112 * Handled Edge Cases: Empty list, expression not a function
38113 */
39114 map(expression) {
40 throw new Error('Remove this line and implement the function');
115 // Edge Case: Handle empty list
116 if (this.values.length === 0) {
117 return new List([]);
118 }
119
120 // Edge Case: Handle if expression is not a function
121 if (typeof expression !== 'function') {
122 return new List([]);
123 }
124
125 const result = [];
126
127 for (let i = 0; i < this.values.length; i++) {
128 result.push(expression(this.values[i]));
129 }
130
131 return new List(result);
41132 }
42133
43134 /**
44 * @return {number}
135 * Returns the number of elements in the list
136 * @return {number} The length of the list
137 *
138 * Handled Edge Cases: Empty list
45139 */
46140 length() {
47 throw new Error('Remove this line and implement the function');
141 // Edge Case: Handle empty list
142 let count = 0;
143 for (let i = 0; i < this.values.length; i++) {
144 count++;
145 }
146 return count;
48147 }
49148
50149 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
150 * Folds (reduces) the list from the left using a callback function
151 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
152 * @param {any} initial - Initial accumulator value
153 * @return {any} The final accumulator value
154 *
155 * Handled Edge Cases: Empty list, callback not a function
54156 */
55157 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
158 // Edge Case: Handle if callback is not a function
159 if (typeof callback !== 'function') {
160 return initial;
161 }
162
163 // Edge Case: Handle empty list
164 if (this.values.length === 0) {
165 return initial;
166 }
167
168 let accumulator = initial;
169
170 for (let i = 0; i < this.values.length; i++) {
171 accumulator = callback(accumulator, this.values[i]);
172 }
173
174 return accumulator;
57175 }
58176
59177 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
178 * Folds (reduces) the list from the right using a callback function
179 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
180 * @param {any} initial - Initial accumulator value
181 * @return {any} The final accumulator value
182 *
183 * Handled Edge Cases: Empty list, callback not a function
63184 */
64185 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
186 // Edge Case: Handle if callback is not a function
187 if (typeof callback !== 'function') {
188 return initial;
189 }
190
191 // Edge Case: Handle empty list
192 if (this.values.length === 0) {
193 return initial;
194 }
195
196 let accumulator = initial;
197
198 // Process from right to left
199 for (let i = this.values.length - 1; i >= 0; i--) {
200 accumulator = callback(accumulator, this.values[i]);
201 }
202
203 return accumulator;
66204 }
67205
68206 /**
69 * @return {List}
207 * Returns a new list with elements in reverse order
208 * @return {List} A new List with elements in reverse order
209 *
210 * Handled Edge Cases: Empty list, single element list
70211 */
71212 reverse() {
72 throw new Error('Remove this line and implement the function');
213 // Edge Case: Handle empty list
214 if (this.values.length === 0) {
215 return new List([]);
216 }
217
218 // Edge Case: Handle single element list
219 if (this.values.length === 1) {
220 return new List([this.values[0]]);
221 }
222
223 const result = [];
224
225 // Build reversed list
226 for (let i = this.values.length - 1; i >= 0; i--) {
227 result.push(this.values[i]);
228 }
229
230 return new List(result);
73231 }
74232}
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.