couple

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:
21 * - Appending an empty list to another list
22 * - Appending a list to an empty list
23 * - Appending two empty lists
1424 */
1525 append(other) {
16 throw new Error('Remove this line and implement the function');
26 // Edge Case: Appending an empty list to another list
27 if (other.length() === 0) {
28 return new List(this.values);
29 }
30
31 // Edge Case: Appending a list to an empty list
32 if (this.length() === 0) {
33 return new List(other.values);
34 }
35
36 // Edge Case: Appending two empty lists
37 if (this.length() === 0 && other.length() === 0) {
38 return new List([]);
39 }
40
41 const result = [];
42 for (let i = 0; i < this.values.length; i++) {
43 result.push(this.values[i]);
44 }
45 for (let i = 0; i < other.values.length; i++) {
46 result.push(other.values[i]);
47 }
48 return new List(result);
1749 }
1850
1951 /**
20 * @param {List} others
21 * @return {List}
52 * Concatenates multiple lists into one
53 * @param {List} others - A list of lists to concatenate
54 * @return {List} A new list with all elements from all lists flattened
55 *
56 * Handled Edge Cases:
57 * - Concatenating with an empty list of lists
58 * - Concatenating empty lists
59 * - Concatenating a mix of empty and non-empty lists
2260 */
2361 concat(others) {
24 throw new Error('Remove this line and implement the function');
62 // Edge Case: Concatenating with an empty list of lists
63 if (others.length() === 0) {
64 return new List(this.values);
65 }
66
67 let result = [];
68
69 // Add elements from the current list
70 for (let i = 0; i < this.values.length; i++) {
71 result.push(this.values[i]);
72 }
73
74 // Add elements from each list in others
75 for (let i = 0; i < others.values.length; i++) {
76 const list = others.values[i];
77
78 // Edge Case: Handling empty lists in the concatenation
79 if (list instanceof List) {
80 for (let j = 0; j < list.values.length; j++) {
81 result.push(list.values[j]);
82 }
83 } else {
84 result.push(list);
85 }
86 }
87
88 return new List(result);
2589 }
2690
2791 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
92 * Filters the list based on a predicate function
93 * @param {(any) => boolean} predicate - Function to test each element
94 * @return {List} A new list containing only elements that satisfy the predicate
95 *
96 * Handled Edge Cases:
97 * - Filtering an empty list
98 * - Predicate that matches no elements
99 * - Predicate that matches all elements
30100 */
31101 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
102 // Edge Case: Filtering an empty list
103 if (this.length() === 0) {
104 return new List([]);
105 }
106
107 const result = [];
108 for (let i = 0; i < this.values.length; i++) {
109 // Edge Case: Predicate that matches no elements
110 // Edge Case: Predicate that matches all elements
111 if (predicate(this.values[i])) {
112 result.push(this.values[i]);
113 }
114 }
115
116 return new List(result);
33117 }
34118
35119 /**
36 * @param {(any) => any} expression
37 * @return {List}
120 * Transforms each element using a mapping function
121 * @param {(any) => any} expression - Function to transform each element
122 * @return {List} A new list with transformed elements
123 *
124 * Handled Edge Cases:
125 * - Mapping an empty list
126 * - Mapping with identity function
38127 */
39128 map(expression) {
40 throw new Error('Remove this line and implement the function');
129 // Edge Case: Mapping an empty list
130 if (this.length() === 0) {
131 return new List([]);
132 }
133
134 const result = [];
135 for (let i = 0; i < this.values.length; i++) {
136 result.push(expression(this.values[i]));
137 }
138
139 return new List(result);
41140 }
42141
43142 /**
44 * @return {number}
143 * Returns the number of elements in the list
144 * @return {number} The length of the list
145 *
146 * Handled Edge Cases:
147 * - Getting length of an empty list
45148 */
46149 length() {
47 throw new Error('Remove this line and implement the function');
150 // Edge Case: Getting length of an empty list
151 let count = 0;
152 for (let i = 0; i < this.values.length; i++) {
153 count++;
154 }
155 return count;
48156 }
49157
50158 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
159 * Folds the list from the left using a callback function
160 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
161 * @param {any} initial - Initial accumulator value
162 * @return {any} The final accumulator value
163 *
164 * Handled Edge Cases:
165 * - Folding an empty list
166 * - Folding with different accumulator types
54167 */
55168 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
169 // Edge Case: Folding an empty list
170 if (this.length() === 0) {
171 return initial;
172 }
173
174 let accumulator = initial;
175 for (let i = 0; i < this.values.length; i++) {
176 accumulator = callback(accumulator, this.values[i]);
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:
189 * - Folding an empty list
190 * - Folding with different accumulator types
63191 */
64192 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
193 // Edge Case: Folding an empty list
194 if (this.length() === 0) {
195 return initial;
196 }
197
198 let accumulator = initial;
199 for (let i = this.values.length - 1; i >= 0; i--) {
200 accumulator = callback(accumulator, this.values[i]);
201 }
202
203 return accumulator;
66204 }
67205
68206 /**
69 * @return {List}
207 * Reverses the order of elements in the list
208 * @return {List} A new list with elements in reverse order
209 *
210 * Handled Edge Cases:
211 * - Reversing an empty list
212 * - Reversing a list with one element
70213 */
71214 reverse() {
72 throw new Error('Remove this line and implement the function');
215 // Edge Case: Reversing an empty list
216 if (this.length() === 0) {
217 return new List([]);
218 }
219
220 // Edge Case: Reversing a list with one element
221 if (this.length() === 1) {
222 return new List([this.values[0]]);
223 }
224
225 const result = [];
226 for (let i = this.values.length - 1; i >= 0; i--) {
227 result.push(this.values[i]);
228 }
229
230 return new List(result);
73231 }
74232}
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.