yahoo

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 parameter
24 if (!other) {
25 return new List(this.values);
26 }
27
28 // Edge Case: Handle when this list is empty
29 if (this.length() === 0) {
30 return new List(other.values);
31 }
32
33 // Edge Case: Handle when other list is empty
34 if (other.length() === 0) {
35 return new List(this.values);
36 }
37
38 return new List([...this.values, ...other.values]);
1739 }
1840
1941 /**
20 * @param {List} others
21 * @return {List}
42 * Concatenates multiple lists into one
43 * @param {List} others - A list of lists to concatenate
44 * @return {List} A new list with all elements from all lists flattened
45 *
46 * Handled Edge Cases: Empty lists, nested empty lists, null/undefined others
2247 */
2348 concat(others) {
24 throw new Error('Remove this line and implement the function');
49 // Edge Case: Handle null or undefined others parameter
50 if (!others) {
51 return new List(this.values);
52 }
53
54 let result = [...this.values];
55
56 // Edge Case: Handle empty others list
57 if (others.length() === 0) {
58 return new List(result);
59 }
60
61 for (let i = 0; i < others.length(); i++) {
62 const list = others.values[i];
63 // Edge Case: Handle if an element in others is not a List
64 if (list && Array.isArray(list.values)) {
65 result = [...result, ...list.values];
66 }
67 }
68
69 return new List(result);
2570 }
2671
2772 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
73 * Filters the list based on a predicate function
74 * @param {(any) => boolean} predicate - Function to test each element
75 * @return {List} A new list containing only elements that pass the predicate
76 *
77 * Handled Edge Cases: Empty list, null/undefined predicate, predicate throwing errors
3078 */
3179 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
80 // Edge Case: Handle null or undefined predicate
81 if (typeof predicate !== 'function') {
82 return new List([]);
83 }
84
85 // Edge Case: Handle empty list
86 if (this.length() === 0) {
87 return new List([]);
88 }
89
90 const result = [];
91 for (let i = 0; i < this.values.length; i++) {
92 try {
93 // Edge Case: Handle predicate function throwing errors
94 if (predicate(this.values[i])) {
95 result.push(this.values[i]);
96 }
97 } catch (e) {
98 // Silently skip elements that cause predicate to throw
99 continue;
100 }
101 }
102
103 return new List(result);
33104 }
34105
35106 /**
36 * @param {(any) => any} expression
37 * @return {List}
107 * Maps each element using a provided function
108 * @param {(any) => any} expression - Function to transform each element
109 * @return {List} A new list with transformed elements
110 *
111 * Handled Edge Cases: Empty list, null/undefined expression, expression throwing errors
38112 */
39113 map(expression) {
40 throw new Error('Remove this line and implement the function');
114 // Edge Case: Handle null or undefined expression
115 if (typeof expression !== 'function') {
116 return new List([]);
117 }
118
119 // Edge Case: Handle empty list
120 if (this.length() === 0) {
121 return new List([]);
122 }
123
124 const result = [];
125 for (let i = 0; i < this.values.length; i++) {
126 try {
127 // Edge Case: Handle expression function throwing errors
128 result.push(expression(this.values[i]));
129 } catch (e) {
130 // Add undefined for elements that cause expression to throw
131 result.push(undefined);
132 }
133 }
134
135 return new List(result);
41136 }
42137
43138 /**
44 * @return {number}
139 * Returns the length of the list
140 * @return {number} The number of elements in the list
141 *
142 * Handled Edge Cases: None (this is a straightforward operation)
45143 */
46144 length() {
47 throw new Error('Remove this line and implement the function');
145 return this.values.length;
48146 }
49147
50148 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
149 * Folds the list from the left using a callback function
150 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
151 * @param {any} initial - Initial accumulator value
152 * @return {any} The final accumulator value
153 *
154 * Handled Edge Cases: Empty list, null/undefined callback
54155 */
55156 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
157 // Edge Case: Handle null or undefined callback
158 if (typeof callback !== 'function') {
159 return initial;
160 }
161
162 let accumulator = initial;
163
164 // Edge Case: Handle empty list
165 if (this.length() === 0) {
166 return accumulator;
167 }
168
169 for (let i = 0; i < this.values.length; i++) {
170 try {
171 // Edge Case: Handle callback function throwing errors
172 accumulator = callback(accumulator, this.values[i]);
173 } catch (e) {
174 // Continue with the current accumulator if callback throws
175 continue;
176 }
177 }
178
179 return accumulator;
57180 }
58181
59182 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
183 * Folds the list from the right using a callback function
184 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
185 * @param {any} initial - Initial accumulator value
186 * @return {any} The final accumulator value
187 *
188 * Handled Edge Cases: Empty list, null/undefined callback
63189 */
64190 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
191 // Edge Case: Handle null or undefined callback
192 if (typeof callback !== 'function') {
193 return initial;
194 }
195
196 let accumulator = initial;
197
198 // Edge Case: Handle empty list
199 if (this.length() === 0) {
200 return accumulator;
201 }
202
203 // Process from right to left
204 for (let i = this.values.length - 1; i >= 0; i--) {
205 try {
206 // Edge Case: Handle callback function throwing errors
207 accumulator = callback(accumulator, this.values[i]);
208 } catch (e) {
209 // Continue with the current accumulator if callback throws
210 continue;
211 }
212 }
213
214 return accumulator;
66215 }
67216
68217 /**
69 * @return {List}
218 * Reverses the list
219 * @return {List} A new list with elements in reverse order
220 *
221 * Handled Edge Cases: Empty list
70222 */
71223 reverse() {
72 throw new Error('Remove this line and implement the function');
224 // Edge Case: Handle empty list
225 if (this.length() === 0) {
226 return new List([]);
227 }
228
229 const result = [];
230 for (let i = this.values.length - 1; i >= 0; i--) {
231 result.push(this.values[i]);
232 }
233
234 return new List(result);
73235 }
74236}
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.