weekends

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 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 const result = [];
34 for (let i = 0; i < this.values.length; i++) {
35 result.push(this.values[i]);
36 }
37 for (let i = 0; i < other.values.length; i++) {
38 result.push(other.values[i]);
39 }
40 return new List(result);
1741 }
1842
1943 /**
20 * @param {List} others
21 * @return {List}
44 * Concatenates multiple lists into one
45 * @param {List} others - A list of lists to concatenate
46 * @return {List} A new list with all elements from all lists flattened
47 *
48 * Handled Edge Cases: Empty lists, null/undefined others, empty nested lists
2249 */
2350 concat(others) {
24 throw new Error('Remove this line and implement the function');
51 // Edge Case: Handle null or undefined others
52 if (!others) {
53 return new List(this.values);
54 }
55
56 let result = [];
57
58 // Add elements from the current list
59 for (let i = 0; i < this.values.length; i++) {
60 result.push(this.values[i]);
61 }
62
63 // Add elements from each list in others
64 for (let i = 0; i < others.values.length; i++) {
65 const list = others.values[i];
66 // Edge Case: Handle if an element in others is not a List
67 if (list && list.values) {
68 for (let j = 0; j < list.values.length; j++) {
69 result.push(list.values[j]);
70 }
71 }
72 }
73
74 return new List(result);
2575 }
2676
2777 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
78 * Filters the list based on a predicate function
79 * @param {(any) => boolean} predicate - Function to test each element
80 * @return {List} A new list containing only elements that satisfy the predicate
81 *
82 * Handled Edge Cases: Empty list, null/undefined predicate, predicate throwing errors
3083 */
3184 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
85 // Edge Case: Handle null or undefined predicate
86 if (typeof predicate !== 'function') {
87 return new List([]);
88 }
89
90 const result = [];
91
92 // Edge Case: Handle empty list
93 if (this.values.length === 0) {
94 return new List(result);
95 }
96
97 for (let i = 0; i < this.values.length; i++) {
98 try {
99 if (predicate(this.values[i])) {
100 result.push(this.values[i]);
101 }
102 } catch (error) {
103 // Edge Case: Handle predicate function throwing errors
104 // Silently skip elements that cause the predicate to throw
105 continue;
106 }
107 }
108
109 return new List(result);
33110 }
34111
35112 /**
36 * @param {(any) => any} expression
37 * @return {List}
113 * Transforms each element in the list using a mapping function
114 * @param {(any) => any} expression - Function to transform each element
115 * @return {List} A new list containing transformed elements
116 *
117 * Handled Edge Cases: Empty list, null/undefined expression, expression throwing errors
38118 */
39119 map(expression) {
40 throw new Error('Remove this line and implement the function');
120 // Edge Case: Handle null or undefined expression
121 if (typeof expression !== 'function') {
122 return new List([]);
123 }
124
125 const result = [];
126
127 // Edge Case: Handle empty list
128 if (this.values.length === 0) {
129 return new List(result);
130 }
131
132 for (let i = 0; i < this.values.length; i++) {
133 try {
134 result.push(expression(this.values[i]));
135 } catch (error) {
136 // Edge Case: Handle expression function throwing errors
137 // Add undefined for elements that cause the expression to throw
138 result.push(undefined);
139 }
140 }
141
142 return new List(result);
41143 }
42144
43145 /**
44 * @return {number}
146 * Returns the number of elements in the list
147 * @return {number} The length of the list
148 *
149 * Handled Edge Cases: None (length is always a non-negative integer)
45150 */
46151 length() {
47 throw new Error('Remove this line and implement the function');
152 let count = 0;
153 for (let i = 0; i < this.values.length; i++) {
154 count++;
155 }
156 return count;
48157 }
49158
50159 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
160 * Folds the list from the left using a callback function and initial accumulator
161 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
162 * @param {any} initial - Initial accumulator value
163 * @return {any} The final accumulator value
164 *
165 * Handled Edge Cases: Empty list, null/undefined callback
54166 */
55167 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
168 // Edge Case: Handle null or undefined callback
169 if (typeof callback !== 'function') {
170 return initial;
171 }
172
173 let accumulator = initial;
174
175 // Edge Case: Handle empty list
176 if (this.values.length === 0) {
177 return accumulator;
178 }
179
180 for (let i = 0; i < this.values.length; i++) {
181 accumulator = callback(accumulator, this.values[i]);
182 }
183
184 return accumulator;
57185 }
58186
59187 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
188 * Folds the list from the right using a callback function and initial accumulator
189 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
190 * @param {any} initial - Initial accumulator value
191 * @return {any} The final accumulator value
192 *
193 * Handled Edge Cases: Empty list, null/undefined callback
63194 */
64195 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
196 // Edge Case: Handle null or undefined callback
197 if (typeof callback !== 'function') {
198 return initial;
199 }
200
201 let accumulator = initial;
202
203 // Edge Case: Handle empty list
204 if (this.values.length === 0) {
205 return accumulator;
206 }
207
208 // Process from right to left
209 for (let i = this.values.length - 1; i >= 0; i--) {
210 accumulator = callback(accumulator, this.values[i]);
211 }
212
213 return accumulator;
66214 }
67215
68216 /**
69 * @return {List}
217 * Returns a new list with elements in reverse order
218 * @return {List} A new list with elements in reverse order
219 *
220 * Handled Edge Cases: Empty list
70221 */
71222 reverse() {
72 throw new Error('Remove this line and implement the function');
223 const result = [];
224
225 // Edge Case: Handle empty list
226 if (this.values.length === 0) {
227 return new List(result);
228 }
229
230 // Build reversed list
231 for (let i = this.values.length - 1; i >= 0; i--) {
232 result.push(this.values[i]);
233 }
234
235 return new List(result);
73236 }
74237}
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.