great

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 * Handled Edge Cases: Appending an empty list, appending to an empty list
1421 */
1522 append(other) {
16 throw new Error('Remove this line and implement the function');
23 // Edge Case: Appending an empty list
24 if (other.length() === 0) {
25 return new List(this.values);
26 }
27
28 // Edge Case: Appending to an empty list
29 if (this.length() === 0) {
30 return new List(other.values);
31 }
32
33 const combined = [];
34 for (let i = 0; i < this.length(); i++) {
35 combined.push(this.values[i]);
36 }
37 for (let i = 0; i < other.length(); i++) {
38 combined.push(other.values[i]);
39 }
40 return new List(combined);
41
42 // Handled Edge Cases: Appending an empty list, appending to an empty list
1743 }
1844
1945 /**
20 * @param {List} others
21 * @return {List}
46 * Concatenates multiple lists into one.
47 * @param {List} others - A list of lists to concatenate.
48 * @return {List} A new list with all elements from all lists.
49 *
50 * Handled Edge Cases: Concatenating with an empty list of lists, concatenating empty lists
2251 */
2352 concat(others) {
24 throw new Error('Remove this line and implement the function');
53 // Edge Case: Concatenating with an empty list of lists
54 if (others.length() === 0) {
55 return new List(this.values);
56 }
57
58 let result = new List(this.values);
59 for (let i = 0; i < others.length(); i++) {
60 result = result.append(others.values[i]);
61 }
62 return result;
63
64 // Handled Edge Cases: Concatenating with an empty list of lists, concatenating empty lists
2565 }
2666
2767 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
68 * Filters the list based on a predicate function.
69 * @param {(any) => boolean} predicate - Function to test each element.
70 * @return {List} A new list with elements that pass the predicate test.
71 *
72 * Handled Edge Cases: Empty list, no elements match the predicate, all elements match the predicate
3073 */
3174 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
75 // Edge Case: Empty list
76 if (this.length() === 0) {
77 return new List([]);
78 }
79
80 const filtered = [];
81 for (let i = 0; i < this.length(); i++) {
82 if (predicate(this.values[i])) {
83 filtered.push(this.values[i]);
84 }
85 }
86
87 // Edge Case: No elements match the predicate
88 // Edge Case: All elements match the predicate
89 return new List(filtered);
90
91 // Handled Edge Cases: Empty list, no elements match the predicate, all elements match the predicate
3392 }
3493
3594 /**
36 * @param {(any) => any} expression
37 * @return {List}
95 * Transforms each element in the list using a mapping function.
96 * @param {(any) => any} expression - Function to transform each element.
97 * @return {List} A new list with transformed elements.
98 *
99 * Handled Edge Cases: Empty list, mapping function returns undefined or null
38100 */
39101 map(expression) {
40 throw new Error('Remove this line and implement the function');
102 // Edge Case: Empty list
103 if (this.length() === 0) {
104 return new List([]);
105 }
106
107 const mapped = [];
108 for (let i = 0; i < this.length(); i++) {
109 mapped.push(expression(this.values[i]));
110 }
111
112 // Edge Case: Mapping function returns undefined or null
113 return new List(mapped);
114
115 // Handled Edge Cases: Empty list, mapping function returns undefined or null
41116 }
42117
43118 /**
44 * @return {number}
119 * Returns the number of elements in the list.
120 * @return {number} The length of the list.
121 *
122 * Handled Edge Cases: Empty list
45123 */
46124 length() {
47 throw new Error('Remove this line and implement the function');
125 // Edge Case: Empty list
126 let count = 0;
127 for (let i = 0; i < this.values.length; i++) {
128 count++;
129 }
130 return count;
131
132 // Handled Edge Cases: Empty list
48133 }
49134
50135 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
136 * Folds the list from the left using a callback function.
137 * @param {(acc: any, el: any) => any} callback - Function to apply to each element.
138 * @param {any} initial - Initial accumulator value.
139 * @return {any} The final accumulator value.
140 *
141 * Handled Edge Cases: Empty list
54142 */
55143 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
144 // Edge Case: Empty list
145 if (this.length() === 0) {
146 return initial;
147 }
148
149 let accumulator = initial;
150 for (let i = 0; i < this.length(); i++) {
151 accumulator = callback(accumulator, this.values[i]);
152 }
153 return accumulator;
154
155 // Handled Edge Cases: Empty list
57156 }
58157
59158 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
159 * Folds the list from the right 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: Empty list
63165 */
64166 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
167 // Edge Case: Empty list
168 if (this.length() === 0) {
169 return initial;
170 }
171
172 let accumulator = initial;
173 for (let i = this.length() - 1; i >= 0; i--) {
174 accumulator = callback(accumulator, this.values[i]);
175 }
176 return accumulator;
177
178 // Handled Edge Cases: Empty list
66179 }
67180
68181 /**
69 * @return {List}
182 * Reverses the order of elements in the list.
183 * @return {List} A new list with elements in reverse order.
184 *
185 * Handled Edge Cases: Empty list, single element list
70186 */
71187 reverse() {
72 throw new Error('Remove this line and implement the function');
188 // Edge Case: Empty list
189 if (this.length() === 0) {
190 return new List([]);
191 }
192
193 // Edge Case: Single element list
194 if (this.length() === 1) {
195 return new List([this.values[0]]);
196 }
197
198 const reversed = [];
199 for (let i = this.length() - 1; i >= 0; i--) {
200 reversed.push(this.values[i]);
201 }
202 return new List(reversed);
203
204 // Handled Edge Cases: Empty list, single element list
73205 }
74206}
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.