Immune

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.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 all items from multiple lists into one 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
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 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 && Array.isArray(list.values)) {
73 for (let j = 0; j < list.values.length; j++) {
74 result.push(list.values[j]);
75 }
76 } else if (Array.isArray(list)) {
77 // If it's a plain array, add its elements
78 for (let j = 0; j < list.length; j++) {
79 result.push(list[j]);
80 }
81 }
82 }
83
84 return new List(result);
2585 }
2686
2787 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
88 * Filters the list based on a predicate function
89 * @param {(any) => boolean} predicate - Function to test each item
90 * @return {List} A new List containing only items that satisfy the predicate
91 *
92 * Handled Edge Cases: Empty list, all items filtered out, all items pass
3093 */
3194 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
95 // Edge Case: Handle empty list
96 if (this.values.length === 0) {
97 return new List([]);
98 }
99
100 const result = [];
101
102 for (let i = 0; i < this.values.length; i++) {
103 // Edge Case: Handle if predicate is not a function
104 if (typeof predicate === 'function' && predicate(this.values[i])) {
105 result.push(this.values[i]);
106 }
107 }
108
109 return new List(result);
33110 }
34111
35112 /**
36 * @param {(any) => any} expression
37 * @return {List}
113 * Maps each item in the list using a provided function
114 * @param {(any) => any} expression - Function to transform each item
115 * @return {List} A new List with transformed items
116 *
117 * Handled Edge Cases: Empty list, expression is not a function
38118 */
39119 map(expression) {
40 throw new Error('Remove this line and implement the function');
120 // Edge Case: Handle empty list
121 if (this.values.length === 0) {
122 return new List([]);
123 }
124
125 // Edge Case: Handle if expression is not a function
126 if (typeof expression !== 'function') {
127 return new List(this.values);
128 }
129
130 const result = [];
131
132 for (let i = 0; i < this.values.length; i++) {
133 result.push(expression(this.values[i]));
134 }
135
136 return new List(result);
41137 }
42138
43139 /**
44 * @return {number}
140 * Returns the number of items in the list
141 * @return {number} The length of the list
142 *
143 * Handled Edge Cases: Empty list
45144 */
46145 length() {
47 throw new Error('Remove this line and implement the function');
146 // Edge Case: Handle empty list
147 if (!this.values) {
148 return 0;
149 }
150
151 let count = 0;
152 for (let i = 0; i < this.values.length; i++) {
153 count++;
154 }
155
156 return count;
48157 }
49158
50159 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
160 * Folds (reduces) the list from left to right using a callback function
161 * @param {(acc: any, el: any) => any} callback - Function to apply to each item
162 * @param {any} initial - Initial accumulator value
163 * @return {any} The final accumulator value
164 *
165 * Handled Edge Cases: Empty list, callback is not a function
54166 */
55167 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
168 // Edge Case: Handle if callback is not a function
169 if (typeof callback !== 'function') {
170 return initial;
171 }
172
173 // Edge Case: Handle empty list
174 if (this.values.length === 0) {
175 return initial;
176 }
177
178 let accumulator = initial;
179
180 for (let i = 0; i < this.values.length; i++) {
181 accumulator = callback(accumulator, this.values[i]);
182 }
183
184 return accumulator;
57185 }
58186
59187 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
188 * Folds (reduces) the list from right to left using a callback function
189 * @param {(acc: any, el: any) => any} callback - Function to apply to each item
190 * @param {any} initial - Initial accumulator value
191 * @return {any} The final accumulator value
192 *
193 * Handled Edge Cases: Empty list, callback is not a function
63194 */
64195 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
196 // Edge Case: Handle if callback is not a function
197 if (typeof callback !== 'function') {
198 return initial;
199 }
200
201 // Edge Case: Handle empty list
202 if (this.values.length === 0) {
203 return initial;
204 }
205
206 let accumulator = initial;
207
208 // Process from right to left
209 for (let i = this.values.length - 1; i >= 0; i--) {
210 accumulator = callback(accumulator, this.values[i]);
211 }
212
213 return accumulator;
66214 }
67215
68216 /**
69 * @return {List}
217 * Returns a new list with items in reverse order
218 * @return {List} A new List with reversed items
219 *
220 * Handled Edge Cases: Empty list, single item list
70221 */
71222 reverse() {
72 throw new Error('Remove this line and implement the function');
223 // Edge Case: Handle empty list
224 if (this.values.length === 0) {
225 return new List([]);
226 }
227
228 // Edge Case: Handle single item list
229 if (this.values.length === 1) {
230 return new List([this.values[0]]);
231 }
232
233 const result = [];
234
235 // Add items in reverse order
236 for (let i = this.values.length - 1; i >= 0; i--) {
237 result.push(this.values[i]);
238 }
239
240 return new List(result);
73241 }
74242}
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.