agent

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 with 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 result = [];
36 for (let i = 0; i < this.values.length; i++) {
37 result.push(this.values[i]);
38 }
39 for (let i = 0; i < other.values.length; i++) {
40 result.push(other.values[i]);
41 }
42 return new List(result);
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
62 // Edge Case: Concatenating when this list is empty
63 if (this.length() === 0) {
64 result = new List([]);
65 }
66
67 for (let i = 0; i < others.values.length; i++) {
68 const list = others.values[i];
69 result = result.append(list);
70 }
71
72 return result;
2573 }
2674
2775 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
76 * Filters the list based on a predicate function.
77 * @param {(any) => boolean} predicate - Function to test each item.
78 * @return {List} A new List with items that satisfy the predicate.
79 *
80 * Handled Edge Cases:
81 * - Filtering an empty list
82 * - Predicate that matches no items
83 * - Predicate that matches all items
3084 */
3185 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
86 // Edge Case: Filtering an empty list
87 if (this.length() === 0) {
88 return new List([]);
89 }
90
91 const result = [];
92
93 for (let i = 0; i < this.values.length; i++) {
94 // Edge Case: Predicate that matches no items
95 // Edge Case: Predicate that matches all items
96 if (predicate(this.values[i])) {
97 result.push(this.values[i]);
98 }
99 }
100
101 return new List(result);
33102 }
34103
35104 /**
36 * @param {(any) => any} expression
37 * @return {List}
105 * Transforms each item in the list using a provided function.
106 * @param {(any) => any} expression - Function to transform each item.
107 * @return {List} A new List with transformed items.
108 *
109 * Handled Edge Cases:
110 * - Mapping an empty list
38111 */
39112 map(expression) {
40 throw new Error('Remove this line and implement the function');
113 // Edge Case: Mapping an empty list
114 if (this.length() === 0) {
115 return new List([]);
116 }
117
118 const result = [];
119
120 for (let i = 0; i < this.values.length; i++) {
121 result.push(expression(this.values[i]));
122 }
123
124 return new List(result);
41125 }
42126
43127 /**
44 * @return {number}
128 * Returns the number of items in the list.
129 * @return {number} The length of the list.
130 *
131 * Handled Edge Cases:
132 * - Getting length of an empty list
45133 */
46134 length() {
47 throw new Error('Remove this line and implement the function');
135 // Edge Case: Getting length of an empty list
136 if (this.values.length === 0) {
137 return 0;
138 }
139
140 let count = 0;
141 for (let i = 0; i < this.values.length; i++) {
142 count++;
143 }
144
145 return count;
48146 }
49147
50148 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
149 * Folds (reduces) the list from the left using a callback function.
150 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
151 * @param {any} initial - Initial accumulator value.
152 * @return {any} The final accumulator value.
153 *
154 * Handled Edge Cases:
155 * - Folding an empty list
54156 */
55157 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
158 // Edge Case: Folding an empty list
159 if (this.length() === 0) {
160 return initial;
161 }
162
163 let accumulator = initial;
164
165 for (let i = 0; i < this.values.length; i++) {
166 accumulator = callback(accumulator, this.values[i]);
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 (reduces) the list from the right using a callback function.
174 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
175 * @param {any} initial - Initial accumulator value.
176 * @return {any} The final accumulator value.
177 *
178 * Handled Edge Cases:
179 * - Folding an empty list
63180 */
64181 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
182 // Edge Case: Folding an empty list
183 if (this.length() === 0) {
184 return initial;
185 }
186
187 let accumulator = initial;
188
189 for (let i = this.values.length - 1; i >= 0; i--) {
190 accumulator = callback(accumulator, this.values[i]);
191 }
192
193 return accumulator;
66194 }
67195
68196 /**
69 * @return {List}
197 * Reverses the order of items in the list.
198 * @return {List} A new List with items in reversed order.
199 *
200 * Handled Edge Cases:
201 * - Reversing an empty list
70202 */
71203 reverse() {
72 throw new Error('Remove this line and implement the function');
204 // Edge Case: Reversing an empty list
205 if (this.length() === 0) {
206 return new List([]);
207 }
208
209 const result = [];
210
211 for (let i = this.values.length - 1; i >= 0; i--) {
212 result.push(this.values[i]);
213 }
214
215 return new List(result);
73216 }
74217}
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.