goodagent

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 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 empty lists
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 empty lists
63 for (let i = 0; i < others.values.length; i++) {
64 const list = others.values[i];
65 result = result.append(list);
66 }
67
68 return result;
2569 }
2670
2771 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
72 * Filters the list based on a predicate function.
73 * @param {(any) => boolean} predicate - Function to test each item.
74 * @return {List} A new list containing only items that satisfy the predicate.
75 *
76 * Handled Edge Cases:
77 * - Filtering an empty list
78 * - Predicate that matches no items
79 * - Predicate that matches all items
3080 */
3181 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
82 // Edge Case: Filtering an empty list
83 if (this.length() === 0) {
84 return new List([]);
85 }
86
87 const result = [];
88
89 for (let i = 0; i < this.values.length; i++) {
90 // Edge Case: Predicate that matches no items
91 // Edge Case: Predicate that matches all items
92 if (predicate(this.values[i])) {
93 result.push(this.values[i]);
94 }
95 }
96
97 return new List(result);
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 an empty list
38107 */
39108 map(expression) {
40 throw new Error('Remove this line and implement the function');
109 // Edge Case: Mapping an empty list
110 if (this.length() === 0) {
111 return new List([]);
112 }
113
114 const result = [];
115
116 for (let i = 0; i < this.values.length; i++) {
117 result.push(expression(this.values[i]));
118 }
119
120 return new List(result);
41121 }
42122
43123 /**
44 * @return {number}
124 * Returns the total number of items in the list.
125 * @return {number} The length of the list.
126 *
127 * Handled Edge Cases:
128 * - Getting length of an empty list
45129 */
46130 length() {
47 throw new Error('Remove this line and implement the function');
131 // Edge Case: Getting length of an empty list
132 let count = 0;
133 for (let i = 0; i < this.values.length; i++) {
134 count++;
135 }
136 return count;
48137 }
49138
50139 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
140 * Folds (reduces) the list from the left using a callback function.
141 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
142 * @param {any} initial - Initial accumulator value.
143 * @return {any} The final accumulator value.
144 *
145 * Handled Edge Cases:
146 * - Folding an empty list
54147 */
55148 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
149 // Edge Case: Folding an empty list
150 if (this.length() === 0) {
151 return initial;
152 }
153
154 let accumulator = initial;
155
156 for (let i = 0; i < this.values.length; i++) {
157 accumulator = callback(accumulator, this.values[i]);
158 }
159
160 return accumulator;
57161 }
58162
59163 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
164 * Folds (reduces) the list from the right using a callback function.
165 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
166 * @param {any} initial - Initial accumulator value.
167 * @return {any} The final accumulator value.
168 *
169 * Handled Edge Cases:
170 * - Folding an empty list
63171 */
64172 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
173 // Edge Case: Folding an empty list
174 if (this.length() === 0) {
175 return initial;
176 }
177
178 let accumulator = initial;
179
180 for (let i = this.values.length - 1; i >= 0; i--) {
181 accumulator = callback(accumulator, this.values[i]);
182 }
183
184 return accumulator;
66185 }
67186
68187 /**
69 * @return {List}
188 * Returns a new list with all original items in reversed order.
189 * @return {List} A new list with items in reversed order.
190 *
191 * Handled Edge Cases:
192 * - Reversing an empty list
70193 */
71194 reverse() {
72 throw new Error('Remove this line and implement the function');
195 // Edge Case: Reversing an empty list
196 if (this.length() === 0) {
197 return new List([]);
198 }
199
200 const result = [];
201
202 for (let i = this.values.length - 1; i >= 0; i--) {
203 result.push(this.values[i]);
204 }
205
206 return new List(result);
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.