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:
21 * - Appending an empty list to another list
22 * - Appending a list to an empty list
1423 */
1524 append(other) {
16 throw new Error('Remove this line and implement the function');
25 // Edge Case: Appending an empty list to another list
26 if (other.length() === 0) {
27 return new List(this.values);
28 }
29
30 // Edge Case: Appending a list to an empty list
31 if (this.length() === 0) {
32 return new List(other.values);
33 }
34
35 const combined = [];
36 for (let i = 0; i < this.length(); i++) {
37 combined.push(this.values[i]);
38 }
39 for (let i = 0; i < other.length(); i++) {
40 combined.push(other.values[i]);
41 }
42 return new List(combined);
1743 }
1844
1945 /**
20 * @param {List} others
21 * @return {List}
46 * Concatenates a series of lists into one flattened list.
47 * @param {List} others - A list containing other lists to concatenate.
48 * @return {List} A new list with all items from all lists.
49 *
50 * Handled Edge Cases:
51 * - Concatenating with an empty list of lists
52 * - Concatenating when this list is empty
2253 */
2354 concat(others) {
24 throw new Error('Remove this line and implement the function');
55 // Edge Case: Concatenating with an empty list of lists
56 if (others.length() === 0) {
57 return new List(this.values);
58 }
59
60 let result = new List(this.values);
61 for (let i = 0; i < others.length(); i++) {
62 const list = others.values[i];
63 // Edge Case: Handling nested empty lists
64 if (list instanceof List) {
65 result = result.append(list);
66 } else {
67 result = result.append(new List([list]));
68 }
69 }
70 return result;
2571 }
2672
2773 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
74 * Filters the list based on a predicate function.
75 * @param {(any) => boolean} predicate - Function to test each item.
76 * @return {List} A new list containing only items that satisfy the predicate.
77 *
78 * Handled Edge Cases:
79 * - Filtering an empty list
80 * - Predicate that matches no items
81 * - Predicate that matches all items
3082 */
3183 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
84 // Edge Case: Filtering an empty list
85 if (this.length() === 0) {
86 return new List([]);
87 }
88
89 const filtered = [];
90 for (let i = 0; i < this.length(); i++) {
91 const item = this.values[i];
92 if (predicate(item)) {
93 filtered.push(item);
94 }
95 }
96
97 return new List(filtered);
3398 }
3499
35100 /**
36 * @param {(any) => any} expression
37 * @return {List}
101 * Transforms each item in the list using a provided function.
102 * @param {(any) => any} expression - Function to transform each item.
103 * @return {List} A new list with transformed items.
104 *
105 * Handled Edge Cases:
106 * - Mapping over an empty list
38107 */
39108 map(expression) {
40 throw new Error('Remove this line and implement the function');
109 // Edge Case: Mapping over an empty list
110 if (this.length() === 0) {
111 return new List([]);
112 }
113
114 const mapped = [];
115 for (let i = 0; i < this.length(); i++) {
116 mapped.push(expression(this.values[i]));
117 }
118
119 return new List(mapped);
41120 }
42121
43122 /**
44 * @return {number}
123 * Returns the number of items in the list.
124 * @return {number} The length of the list.
125 *
126 * Handled Edge Cases:
127 * - Getting length of an empty list
45128 */
46129 length() {
47 throw new Error('Remove this line and implement the function');
130 // Edge Case: Getting length of an empty list
131 if (!this.values || this.values.length === undefined) {
132 return 0;
133 }
134
135 let count = 0;
136 for (let i = 0; this.values[i] !== undefined; i++) {
137 count++;
138 }
139 return count;
48140 }
49141
50142 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
143 * Folds (reduces) the list from the left using a callback function.
144 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
145 * @param {any} initial - Initial accumulator value.
146 * @return {any} The final accumulator value.
147 *
148 * Handled Edge Cases:
149 * - Folding an empty list (returns initial value)
54150 */
55151 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
152 // Edge Case: Folding an empty list (returns initial value)
153 if (this.length() === 0) {
154 return initial;
155 }
156
157 let accumulator = initial;
158 for (let i = 0; i < this.length(); i++) {
159 accumulator = callback(accumulator, this.values[i]);
160 }
161
162 return accumulator;
57163 }
58164
59165 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
166 * Folds (reduces) the list from the right using a callback function.
167 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
168 * @param {any} initial - Initial accumulator value.
169 * @return {any} The final accumulator value.
170 *
171 * Handled Edge Cases:
172 * - Folding an empty list (returns initial value)
63173 */
64174 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
175 // Edge Case: Folding an empty list (returns initial value)
176 if (this.length() === 0) {
177 return initial;
178 }
179
180 let accumulator = initial;
181 for (let i = this.length() - 1; i >= 0; i--) {
182 accumulator = callback(accumulator, this.values[i]);
183 }
184
185 return accumulator;
66186 }
67187
68188 /**
69 * @return {List}
189 * Reverses the order of items in the list.
190 * @return {List} A new list with items in reversed order.
191 *
192 * Handled Edge Cases:
193 * - Reversing an empty list
70194 */
71195 reverse() {
72 throw new Error('Remove this line and implement the function');
196 // Edge Case: Reversing an empty list
197 if (this.length() === 0) {
198 return new List([]);
199 }
200
201 const reversed = [];
202 for (let i = this.length() - 1; i >= 0; i--) {
203 reversed.push(this.values[i]);
204 }
205
206 return new List(reversed);
73207 }
74208}
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.