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 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 all items from another list to the end of this list.
17 * @param {List} other - The list to append.
18 * @return {List} A new list containing all items from both lists.
19 *
20 * Handled Edge Cases:
21 * - When other list is empty
22 * - When this list is empty
1423 */
1524 append(other) {
16 throw new Error('Remove this line and implement the function');
25 // Edge Case: When other list is empty, return a copy of this list
26 if (!other || other.length() === 0) {
27 return new List(this.values);
28 }
29
30 // Edge Case: When this list is empty, return a copy of the other list
31 if (this.length() === 0) {
32 return new List(other.values);
33 }
34
35 return new List(this.values.concat(other.values));
1736 }
1837
1938 /**
20 * @param {List} others
21 * @return {List}
39 * Concatenates a series of lists into one flattened list.
40 * @param {List} others - A list containing other lists to concatenate.
41 * @return {List} A new list with all items from all lists.
42 *
43 * Handled Edge Cases:
44 * - When others list is empty
45 * - When this list is empty
46 * - When nested lists are empty
2247 */
2348 concat(others) {
24 throw new Error('Remove this line and implement the function');
49 // Edge Case: When others list is empty, return a copy of this list
50 if (!others || others.length() === 0) {
51 return new List(this.values);
52 }
53
54 let result = new List(this.values);
55
56 // Process each list in the others list
57 for (let i = 0; i < others.length(); i++) {
58 const item = others.values[i];
59
60 // Edge Case: Handle nested List objects
61 if (item instanceof List) {
62 result = result.append(item);
63 } else {
64 // Edge Case: Handle non-List items (should not normally occur based on problem description)
65 result = result.append(new List([item]));
66 }
67 }
68
69 return result;
2570 }
2671
2772 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
73 * Filters the list based on a predicate function.
74 * @param {Function} predicate - Function that returns true for items to keep.
75 * @return {List} A new list containing only items for which predicate returned true.
76 *
77 * Handled Edge Cases:
78 * - When this list is empty
79 * - When no items match the predicate
80 * - When all items match the predicate
3081 */
3182 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
83 // Edge Case: When this list is empty, return an empty list
84 if (this.length() === 0) {
85 return new List([]);
86 }
87
88 const filtered = [];
89
90 for (let i = 0; i < this.values.length; i++) {
91 if (predicate(this.values[i])) {
92 filtered.push(this.values[i]);
93 }
94 }
95
96 // Edge Case: When no items match (filtered array is empty)
97 // Edge Case: When all items match (filtered array equals original)
98 return new List(filtered);
3399 }
34100
35101 /**
36 * @param {(any) => any} expression
37 * @return {List}
102 * Maps a function over each item in the list.
103 * @param {Function} expression - Function to apply to each item.
104 * @return {List} A new list containing the results of applying the function.
105 *
106 * Handled Edge Cases:
107 * - When this list is empty
38108 */
39109 map(expression) {
40 throw new Error('Remove this line and implement the function');
110 // Edge Case: When this list is empty, return an empty list
111 if (this.length() === 0) {
112 return new List([]);
113 }
114
115 const mapped = [];
116
117 for (let i = 0; i < this.values.length; i++) {
118 mapped.push(expression(this.values[i]));
119 }
120
121 return new List(mapped);
41122 }
42123
43124 /**
44 * @return {number}
125 * Returns the number of items in the list.
126 * @return {number} The count of items.
127 *
128 * Handled Edge Cases:
129 * - When list is empty (returns 0)
45130 */
46131 length() {
47 throw new Error('Remove this line and implement the function');
132 // Edge Case: When list is empty, return 0
133 let count = 0;
134 for (let i = 0; i < this.values.length; i++) {
135 count++;
136 }
137 return count;
48138 }
49139
50140 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
141 * Folds (reduces) the list from the left using a callback function.
142 * @param {Function} callback - Function to apply to accumulator and each element.
143 * @param {any} initial - Initial value for the accumulator.
144 * @return {any} The final accumulator value.
145 *
146 * Handled Edge Cases:
147 * - When this list is empty (returns initial value)
54148 */
55149 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
150 // Edge Case: When this list is empty, return the initial value
151 if (this.length() === 0) {
152 return initial;
153 }
154
155 let accumulator = initial;
156
157 for (let i = 0; i < this.values.length; i++) {
158 accumulator = callback(accumulator, this.values[i]);
159 }
160
161 return accumulator;
57162 }
58163
59164 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
165 * Folds (reduces) the list from the right using a callback function.
166 * @param {Function} callback - Function to apply to accumulator and each element.
167 * @param {any} initial - Initial value for the accumulator.
168 * @return {any} The final accumulator value.
169 *
170 * Handled Edge Cases:
171 * - When this list is empty (returns initial value)
63172 */
64173 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
174 // Edge Case: When this list is empty, return the initial value
175 if (this.length() === 0) {
176 return initial;
177 }
178
179 let accumulator = initial;
180
181 // Process from right to left
182 for (let i = this.values.length - 1; i >= 0; i--) {
183 accumulator = callback(accumulator, this.values[i]);
184 }
185
186 return accumulator;
66187 }
67188
68189 /**
69 * @return {List}
190 * Reverses the order of items in the list.
191 * @return {List} A new list with items in reversed order.
192 *
193 * Handled Edge Cases:
194 * - When this list is empty
195 * - When this list has only one element
70196 */
71197 reverse() {
72 throw new Error('Remove this line and implement the function');
198 // Edge Case: When this list is empty, return an empty list
199 if (this.length() === 0) {
200 return new List([]);
201 }
202
203 // Edge Case: When this list has only one element, return a copy of the list
204 if (this.length() === 1) {
205 return new List([this.values[0]]);
206 }
207
208 const reversed = [];
209
210 for (let i = this.values.length - 1; i >= 0; i--) {
211 reversed.push(this.values[i]);
212 }
213
214 return new List(reversed);
73215 }
74216}
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.