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 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 flattened
48 *
49 * Handled Edge Cases: Empty lists, nested empty lists
2250 */
2351 concat(others) {
24 throw new Error('Remove this line and implement the function');
52 // Edge Case: Handle empty others list
53 if (!others || others.values.length === 0) {
54 return new List(this.values);
55 }
56
57 let result = [...this.values];
58
59 // Edge Case: Handle nested empty lists
60 for (const list of others.values) {
61 if (list && list.values) {
62 result = [...result, ...list.values];
63 }
64 }
65
66 return new List(result);
2567 }
2668
2769 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
70 * Filters the list based on a predicate function
71 * @param {(any) => boolean} predicate - Function to test each element
72 * @return {List} A new list with elements that pass the predicate
73 *
74 * Handled Edge Cases: Empty list, predicate returning all false, predicate returning all true
3075 */
3176 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
77 // Edge Case: Handle empty list
78 if (this.values.length === 0) {
79 return new List([]);
80 }
81
82 // Edge Case: Handle null or undefined predicate
83 if (typeof predicate !== 'function') {
84 throw new Error('Predicate must be a function');
85 }
86
87 const result = [];
88 for (const value of this.values) {
89 if (predicate(value)) {
90 result.push(value);
91 }
92 }
93
94 return new List(result);
3395 }
3496
3597 /**
36 * @param {(any) => any} expression
37 * @return {List}
98 * Maps each element using a provided function
99 * @param {(any) => any} expression - Function to transform each element
100 * @return {List} A new list with transformed elements
101 *
102 * Handled Edge Cases: Empty list, expression function throws error
38103 */
39104 map(expression) {
40 throw new Error('Remove this line and implement the function');
105 // Edge Case: Handle empty list
106 if (this.values.length === 0) {
107 return new List([]);
108 }
109
110 // Edge Case: Handle null or undefined expression
111 if (typeof expression !== 'function') {
112 throw new Error('Expression must be a function');
113 }
114
115 const result = [];
116 for (const value of this.values) {
117 result.push(expression(value));
118 }
119
120 return new List(result);
41121 }
42122
43123 /**
44 * @return {number}
124 * Returns the length of the list
125 * @return {number} The number of elements in the list
126 *
127 * Handled Edge Cases: Empty list
45128 */
46129 length() {
47 throw new Error('Remove this line and implement the function');
130 // Edge Case: Handle empty list
131 return this.values.length;
48132 }
49133
50134 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
135 * Folds the list from the left using a callback function
136 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
137 * @param {any} initial - Initial accumulator value
138 * @return {any} The final accumulator value
139 *
140 * Handled Edge Cases: Empty list, callback function throws error
54141 */
55142 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
143 // Edge Case: Handle null or undefined callback
144 if (typeof callback !== 'function') {
145 throw new Error('Callback must be a function');
146 }
147
148 let accumulator = initial;
149
150 // Edge Case: Handle empty list
151 if (this.values.length === 0) {
152 return accumulator;
153 }
154
155 for (const value of this.values) {
156 accumulator = callback(accumulator, value);
157 }
158
159 return accumulator;
57160 }
58161
59162 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
163 * Folds the list from the right using a callback function
164 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
165 * @param {any} initial - Initial accumulator value
166 * @return {any} The final accumulator value
167 *
168 * Handled Edge Cases: Empty list, callback function throws error
63169 */
64170 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
171 // Edge Case: Handle null or undefined callback
172 if (typeof callback !== 'function') {
173 throw new Error('Callback must be a function');
174 }
175
176 let accumulator = initial;
177
178 // Edge Case: Handle empty list
179 if (this.values.length === 0) {
180 return accumulator;
181 }
182
183 // Process from right to left
184 for (let i = this.values.length - 1; i >= 0; i--) {
185 accumulator = callback(accumulator, this.values[i]);
186 }
187
188 return accumulator;
66189 }
67190
68191 /**
69 * @return {List}
192 * Reverses the list
193 * @return {List} A new list with elements in reverse order
194 *
195 * Handled Edge Cases: Empty list, single element list
70196 */
71197 reverse() {
72 throw new Error('Remove this line and implement the function');
198 // Edge Case: Handle empty list
199 if (this.values.length === 0) {
200 return new List([]);
201 }
202
203 // Edge Case: Handle single element list
204 if (this.values.length === 1) {
205 return new List([this.values[0]]);
206 }
207
208 const result = [];
209 for (let i = this.values.length - 1; i >= 0; i--) {
210 result.push(this.values[i]);
211 }
212
213 return new List(result);
73214 }
74215}
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.