sn62

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 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 item in others is not a List
72 if (list instanceof List) {
73 for (let j = 0; j < list.values.length; j++) {
74 result.push(list.values[j]);
75 }
76 } else {
77 // If it's not a List, just add the item itself
78 result.push(list);
79 }
80 }
81
82 return new List(result);
2583 }
2684
2785 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
86 * Filters the list based on a predicate function
87 * @param {(any) => boolean} predicate - Function to test each item
88 * @return {List} A new List containing only items that pass the predicate
89 *
90 * Handled Edge Cases: Empty list, all items filtered out, all items pass
3091 */
3192 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
93 // Edge Case: Handle empty list
94 if (this.values.length === 0) {
95 return new List([]);
96 }
97
98 const result = [];
99
100 for (let i = 0; i < this.values.length; i++) {
101 // Edge Case: Handle if predicate is not a function
102 if (typeof predicate === 'function' && predicate(this.values[i])) {
103 result.push(this.values[i]);
104 }
105 }
106
107 return new List(result);
33108 }
34109
35110 /**
36 * @param {(any) => any} expression
37 * @return {List}
111 * Transforms each item in the list using a mapping function
112 * @param {(any) => any} expression - Function to transform each item
113 * @return {List} A new List containing transformed items
114 *
115 * Handled Edge Cases: Empty list
38116 */
39117 map(expression) {
40 throw new Error('Remove this line and implement the function');
118 // Edge Case: Handle empty list
119 if (this.values.length === 0) {
120 return new List([]);
121 }
122
123 const result = [];
124
125 for (let i = 0; i < this.values.length; i++) {
126 // Edge Case: Handle if expression is not a function
127 if (typeof expression === 'function') {
128 result.push(expression(this.values[i]));
129 } else {
130 result.push(this.values[i]);
131 }
132 }
133
134 return new List(result);
41135 }
42136
43137 /**
44 * @return {number}
138 * Returns the number of items in the list
139 * @return {number} The length of the list
140 *
141 * Handled Edge Cases: Empty list
45142 */
46143 length() {
47 throw new Error('Remove this line and implement the function');
144 // Edge Case: Handle empty list
145 let count = 0;
146 for (let i = 0; i < this.values.length; i++) {
147 count++;
148 }
149 return count;
48150 }
49151
50152 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
153 * Folds (reduces) the list from left to right using a callback function
154 * @param {(acc: any, el: any) => any} callback - Function to apply to each item
155 * @param {any} initial - Initial accumulator value
156 * @return {any} The final accumulator value
157 *
158 * Handled Edge Cases: Empty list, null/undefined callback
54159 */
55160 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
161 // Edge Case: Handle empty list
162 if (this.values.length === 0) {
163 return initial;
164 }
165
166 // Edge Case: Handle null/undefined callback
167 if (typeof callback !== 'function') {
168 return initial;
169 }
170
171 let accumulator = initial;
172
173 for (let i = 0; i < this.values.length; i++) {
174 accumulator = callback(accumulator, this.values[i]);
175 }
176
177 return accumulator;
57178 }
58179
59180 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
181 * Folds (reduces) the list from right to left using a callback function
182 * @param {(acc: any, el: any) => any} callback - Function to apply to each item
183 * @param {any} initial - Initial accumulator value
184 * @return {any} The final accumulator value
185 *
186 * Handled Edge Cases: Empty list, null/undefined callback
63187 */
64188 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
189 // Edge Case: Handle empty list
190 if (this.values.length === 0) {
191 return initial;
192 }
193
194 // Edge Case: Handle null/undefined callback
195 if (typeof callback !== 'function') {
196 return initial;
197 }
198
199 let accumulator = initial;
200
201 // Process from right to left
202 for (let i = this.values.length - 1; i >= 0; i--) {
203 accumulator = callback(accumulator, this.values[i]);
204 }
205
206 return accumulator;
66207 }
67208
68209 /**
69 * @return {List}
210 * Returns a new list with items in reverse order
211 * @return {List} A new List with items in reverse order
212 *
213 * Handled Edge Cases: Empty list, single item list
70214 */
71215 reverse() {
72 throw new Error('Remove this line and implement the function');
216 // Edge Case: Handle empty list
217 if (this.values.length === 0) {
218 return new List([]);
219 }
220
221 // Edge Case: Handle single item list
222 if (this.values.length === 1) {
223 return new List([this.values[0]]);
224 }
225
226 const result = [];
227
228 // Add items in reverse order
229 for (let i = this.values.length - 1; i >= 0; i--) {
230 result.push(this.values[i]);
231 }
232
233 return new List(result);
73234 }
74235}
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.