bohdan

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: 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 if (this.values.length === 0) {
34 return new List(other.values);
35 }
36
37 if (other.values.length === 0) {
38 return new List(this.values);
39 }
40
41 return new List([...this.values, ...other.values]);
1742 }
1843
1944 /**
20 * @param {List} others
21 * @return {List}
45 * Concatenates multiple lists into one
46 * @param {List} others - A list of lists to concatenate
47 * @return {List} A new list with all elements from all lists flattened
48 *
49 * Handled Edge Cases: Empty lists, null/undefined others, empty nested lists
2250 */
2351 concat(others) {
24 throw new Error('Remove this line and implement the function');
52 // Edge Case: Handle null or undefined others
53 if (!others) {
54 return new List(this.values);
55 }
56
57 let result = [...this.values];
58
59 // Edge Case: Handle empty others list
60 if (others.values.length === 0) {
61 return new List(result);
62 }
63
64 for (const list of others.values) {
65 // Edge Case: Handle null or undefined nested lists
66 if (list && list.values) {
67 result = [...result, ...list.values];
68 }
69 }
70
71 return new List(result);
2572 }
2673
2774 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
75 * Filters the list based on a predicate function
76 * @param {(any) => boolean} predicate - Function to test each element
77 * @return {List} A new list containing only elements that pass the predicate
78 *
79 * Handled Edge Cases: Empty list, null/undefined predicate, predicate throwing errors
3080 */
3181 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
82 // Edge Case: Handle null or undefined predicate
83 if (typeof predicate !== 'function') {
84 return new List([]);
85 }
86
87 // Edge Case: Handle empty list
88 if (this.values.length === 0) {
89 return new List([]);
90 }
91
92 const result = [];
93
94 for (const item of this.values) {
95 try {
96 if (predicate(item)) {
97 result.push(item);
98 }
99 } catch (error) {
100 // Edge Case: Handle predicate function throwing errors
101 // Silently skip items that cause the predicate to throw
102 continue;
103 }
104 }
105
106 return new List(result);
33107 }
34108
35109 /**
36 * @param {(any) => any} expression
37 * @return {List}
110 * Maps each element in the list using the provided function
111 * @param {(any) => any} expression - Function to transform each element
112 * @return {List} A new list with transformed elements
113 *
114 * Handled Edge Cases: Empty list, null/undefined expression, expression throwing errors
38115 */
39116 map(expression) {
40 throw new Error('Remove this line and implement the function');
117 // Edge Case: Handle null or undefined expression
118 if (typeof expression !== 'function') {
119 return new List([]);
120 }
121
122 // Edge Case: Handle empty list
123 if (this.values.length === 0) {
124 return new List([]);
125 }
126
127 const result = [];
128
129 for (const item of this.values) {
130 try {
131 result.push(expression(item));
132 } catch (error) {
133 // Edge Case: Handle expression function throwing errors
134 // Push undefined for items that cause the expression to throw
135 result.push(undefined);
136 }
137 }
138
139 return new List(result);
41140 }
42141
43142 /**
44 * @return {number}
143 * Returns the length of the list
144 * @return {number} The number of elements in the list
145 *
146 * Handled Edge Cases: None (this operation is always safe)
45147 */
46148 length() {
47 throw new Error('Remove this line and implement the function');
149 return this.values.length;
48150 }
49151
50152 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
153 * Folds the list from the left using a callback function and initial accumulator
154 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
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 null or undefined callback
162 if (typeof callback !== 'function') {
163 return initial;
164 }
165
166 // Edge Case: Handle empty list
167 if (this.values.length === 0) {
168 return initial;
169 }
170
171 let accumulator = initial;
172
173 for (const item of this.values) {
174 accumulator = callback(accumulator, item);
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 the list from the right using a callback function and initial accumulator
182 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
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 null or undefined callback
190 if (typeof callback !== 'function') {
191 return initial;
192 }
193
194 // Edge Case: Handle empty list
195 if (this.values.length === 0) {
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 * Reverses the list
211 * @return {List} A new list with elements in reverse order
212 *
213 * Handled Edge Cases: Empty 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 const result = [];
222
223 for (let i = this.values.length - 1; i >= 0; i--) {
224 result.push(this.values[i]);
225 }
226
227 return new List(result);
73228 }
74229}
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.