let it go

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
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 another list to this list
17 * @param {List} other - The list to append
18 * @return {List} A new list containing all elements 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: other is null or undefined
24 if (other === null || other === undefined) {
25 return new List(this.values);
26 }
27
28 // Edge Case: both lists are empty
29 if (this.values.length === 0 && other.values.length === 0) {
30 return new List([]);
31 }
32
33 // Edge Case: this list is empty
34 if (this.values.length === 0) {
35 return new List(other.values);
36 }
37
38 // Edge Case: other list is empty
39 if (other.values.length === 0) {
40 return new List(this.values);
41 }
42
43 return new List([...this.values, ...other.values]);
1744 }
1845
1946 /**
20 * @param {List} others
21 * @return {List}
47 * Concatenates multiple lists into one
48 * @param {List} others - A list of lists to concatenate
49 * @return {List} A new list with all elements from all lists flattened
50 *
51 * Handled Edge Cases: empty lists, nested empty lists, null/undefined values
2252 */
2353 concat(others) {
24 throw new Error('Remove this line and implement the function');
54 // Edge Case: others is null or undefined
55 if (others === null || others === undefined) {
56 return new List(this.values);
57 }
58
59 let result = [...this.values];
60
61 // Edge Case: others is empty
62 if (others.values.length === 0) {
63 return new List(result);
64 }
65
66 for (const list of others.values) {
67 // Edge Case: element in others is not a List object
68 if (list instanceof List) {
69 result = [...result, ...list.values];
70 } else {
71 // If it's not a List, treat it as a single element
72 result = [...result, list];
73 }
74 }
75
76 return new List(result);
2577 }
2678
2779 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
80 * Filters the list based on a predicate function
81 * @param {(any) => boolean} predicate - Function to test each element
82 * @return {List} A new list containing only elements that pass the predicate
83 *
84 * Handled Edge Cases: empty list, predicate returning all false, predicate returning all true
3085 */
3186 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
87 // Edge Case: empty list
88 if (this.values.length === 0) {
89 return new List([]);
90 }
91
92 // Edge Case: predicate is null or undefined
93 if (predicate === null || predicate === undefined) {
94 return new List([]);
95 }
96
97 const result = [];
98 for (const item of this.values) {
99 if (predicate(item)) {
100 result.push(item);
101 }
102 }
103
104 return new List(result);
33105 }
34106
35107 /**
36 * @param {(any) => any} expression
37 * @return {List}
108 * Transforms each element using the provided function
109 * @param {(any) => any} expression - Function to transform each element
110 * @return {List} A new list with transformed elements
111 *
112 * Handled Edge Cases: empty list, expression is null/undefined
38113 */
39114 map(expression) {
40 throw new Error('Remove this line and implement the function');
115 // Edge Case: empty list
116 if (this.values.length === 0) {
117 return new List([]);
118 }
119
120 // Edge Case: expression is null or undefined
121 if (expression === null || expression === undefined) {
122 return new List(this.values.map(() => undefined));
123 }
124
125 const result = [];
126 for (const item of this.values) {
127 result.push(expression(item));
128 }
129
130 return new List(result);
41131 }
42132
43133 /**
44 * @return {number}
134 * Returns the number of elements in the list
135 * @return {number} The length of the list
136 *
137 * Handled Edge Cases: empty list
45138 */
46139 length() {
47 throw new Error('Remove this line and implement the function');
140 // Edge Case: empty list
141 return this.values.length;
48142 }
49143
50144 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
145 * Folds the list from the left using a callback function
146 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
147 * @param {any} initial - Initial accumulator value
148 * @return {any} The final accumulator value
149 *
150 * Handled Edge Cases: empty list, callback is null/undefined
54151 */
55152 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
153 // Edge Case: callback is null or undefined
154 if (callback === null || callback === undefined) {
155 return initial;
156 }
157
158 // Edge Case: empty list
159 if (this.values.length === 0) {
160 return initial;
161 }
162
163 let accumulator = initial;
164 for (const item of this.values) {
165 accumulator = callback(accumulator, item);
166 }
167
168 return accumulator;
57169 }
58170
59171 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
172 * Folds the list from the right using a callback function
173 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
174 * @param {any} initial - Initial accumulator value
175 * @return {any} The final accumulator value
176 *
177 * Handled Edge Cases: empty list, callback is null/undefined
63178 */
64179 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
180 // Edge Case: callback is null or undefined
181 if (callback === null || callback === undefined) {
182 return initial;
183 }
184
185 // Edge Case: empty list
186 if (this.values.length === 0) {
187 return initial;
188 }
189
190 let accumulator = initial;
191 // Process from right to left
192 for (let i = this.values.length - 1; i >= 0; i--) {
193 accumulator = callback(accumulator, this.values[i]);
194 }
195
196 return accumulator;
66197 }
67198
68199 /**
69 * @return {List}
200 * Reverses the order of elements in the list
201 * @return {List} A new list with elements in reverse order
202 *
203 * Handled Edge Cases: empty list, single element list
70204 */
71205 reverse() {
72 throw new Error('Remove this line and implement the function');
206 // Edge Case: empty list
207 if (this.values.length === 0) {
208 return new List([]);
209 }
210
211 // Edge Case: single element list
212 if (this.values.length === 1) {
213 return new List([this.values[0]]);
214 }
215
216 const result = [];
217 for (let i = this.values.length - 1; i >= 0; i--) {
218 result.push(this.values[i]);
219 }
220
221 return new List(result);
73222 }
74223}
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.