perfect

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 elements from both lists.
19 *
20 * Edge Case: Handles empty lists correctly by returning a new list with combined elements.
1421 */
1522 append(other) {
16 throw new Error('Remove this line and implement the function');
23 return new List(this.values.concat(other.values));
1724 }
1825
1926 /**
20 * @param {List} others
21 * @return {List}
27 * Concatenates multiple lists into one flattened list.
28 * @param {List} others - A list of lists to concatenate.
29 * @return {List} A new list with all elements from all lists.
30 *
31 * Edge Case: Handles empty lists and nested empty lists correctly.
2232 */
2333 concat(others) {
24 throw new Error('Remove this line and implement the function');
34 let result = this.values.slice(); // Copy current values
35
36 // Edge Case: Handle when others is not a List of Lists
37 for (let i = 0; i < others.length(); i++) {
38 const item = others.values[i];
39 if (item instanceof List) {
40 result = result.concat(item.values);
41 } else {
42 result.push(item);
43 }
44 }
45
46 return new List(result);
2547 }
2648
2749 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
50 * Filters the list based on a predicate function.
51 * @param {Function} predicate - Function to test each element.
52 * @return {List} A new list containing only elements that pass the predicate.
53 *
54 * Edge Case: Returns an empty list when no elements match the predicate.
55 * Edge Case: Returns an empty list when the original list is empty.
3056 */
3157 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
58 const result = [];
59
60 // Edge Case: Handle empty list
61 if (this.length() === 0) {
62 return new List(result);
63 }
64
65 for (let i = 0; i < this.values.length; i++) {
66 if (predicate(this.values[i])) {
67 result.push(this.values[i]);
68 }
69 }
70
71 return new List(result);
3372 }
3473
3574 /**
36 * @param {(any) => any} expression
37 * @return {List}
75 * Transforms each element in the list using a mapping function.
76 * @param {Function} expression - Function to transform each element.
77 * @return {List} A new list with transformed elements.
78 *
79 * Edge Case: Returns an empty list when the original list is empty.
3880 */
3981 map(expression) {
40 throw new Error('Remove this line and implement the function');
82 const result = [];
83
84 // Edge Case: Handle empty list
85 if (this.length() === 0) {
86 return new List(result);
87 }
88
89 for (let i = 0; i < this.values.length; i++) {
90 result.push(expression(this.values[i]));
91 }
92
93 return new List(result);
4194 }
4295
4396 /**
44 * @return {number}
97 * Returns the number of elements in the list.
98 * @return {number} The length of the list.
99 *
100 * Edge Case: Returns 0 for an empty list.
45101 */
46102 length() {
47 throw new Error('Remove this line and implement the function');
103 let count = 0;
104
105 // Edge Case: Handle empty list
106 if (this.values.length === 0) {
107 return 0;
108 }
109
110 for (let i = 0; i < this.values.length; i++) {
111 count++;
112 }
113
114 return count;
48115 }
49116
50117 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
118 * Folds the list from the left using a callback function and initial accumulator.
119 * @param {Function} callback - Function to apply to each element and accumulator.
120 * @param {any} initial - Initial value for the accumulator.
121 * @return {any} The final accumulated value.
122 *
123 * Edge Case: Returns the initial value when the list is empty.
54124 */
55125 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
126 let accumulator = initial;
127
128 // Edge Case: Handle empty list
129 if (this.length() === 0) {
130 return accumulator;
131 }
132
133 for (let i = 0; i < this.values.length; i++) {
134 accumulator = callback(accumulator, this.values[i]);
135 }
136
137 return accumulator;
57138 }
58139
59140 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
141 * Folds the list from the right using a callback function and initial accumulator.
142 * @param {Function} callback - Function to apply to each element and accumulator.
143 * @param {any} initial - Initial value for the accumulator.
144 * @return {any} The final accumulated value.
145 *
146 * Edge Case: Returns the initial value when the list is empty.
63147 */
64148 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
149 let accumulator = initial;
150
151 // Edge Case: Handle empty list
152 if (this.length() === 0) {
153 return accumulator;
154 }
155
156 // Process from right to left
157 for (let i = this.values.length - 1; i >= 0; i--) {
158 accumulator = callback(accumulator, this.values[i]);
159 }
160
161 return accumulator;
66162 }
67163
68164 /**
69 * @return {List}
165 * Reverses the order of elements in the list.
166 * @return {List} A new list with elements in reversed order.
167 *
168 * Edge Case: Returns an empty list when the original list is empty.
169 * Edge Case: Returns the same list when it has only one element.
70170 */
71171 reverse() {
72 throw new Error('Remove this line and implement the function');
172 const result = [];
173
174 // Edge Case: Handle empty list
175 if (this.length() === 0) {
176 return new List(result);
177 }
178
179 // Edge Case: Handle single element list
180 if (this.length() === 1) {
181 return new List([this.values[0]]);
182 }
183
184 for (let i = this.values.length - 1; i >= 0; i--) {
185 result.push(this.values[i]);
186 }
187
188 return new List(result);
73189 }
190
191 // Handled Edge Cases: Empty list handling, single element lists, proper concatenation of nested lists
74192}
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.