A.2.1

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 array of 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 * - Appending an empty list to another list
22 * - Appending a list to an empty list
1423 */
1524 append(other) {
16 throw new Error('Remove this line and implement the function');
25 // Edge Case: Appending an empty list to another list
26 if (other.length() === 0) {
27 return new List(this.values);
28 }
29
30 // Edge Case: Appending a list to an empty list
31 if (this.length() === 0) {
32 return new List(other.values);
33 }
34
35 const result = [];
36 for (let i = 0; i < this.values.length; i++) {
37 result.push(this.values[i]);
38 }
39 for (let i = 0; i < other.values.length; i++) {
40 result.push(other.values[i]);
41 }
42 return new List(result);
1743 }
1844
1945 /**
20 * @param {List} others
21 * @return {List}
46 * Concatenates a list of lists into a single flattened list
47 * @param {List} others - A list containing other lists to concatenate
48 * @return {List} A new List with all items from all lists flattened
49 *
50 * Handled Edge Cases:
51 * - Concatenating an empty list of lists
52 * - Concatenating lists that contain empty lists
2253 */
2354 concat(others) {
24 throw new Error('Remove this line and implement the function');
55 // Edge Case: Concatenating an empty list of lists
56 if (others.length() === 0) {
57 return new List(this.values);
58 }
59
60 let result = new List(this.values);
61
62 // Edge Case: Concatenating lists that contain empty lists
63 for (let i = 0; i < others.values.length; i++) {
64 const list = others.values[i];
65 if (list instanceof List) {
66 result = result.append(list);
67 }
68 }
69
70 return result;
2571 }
2672
2773 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
74 * Filters the list based on a predicate function
75 * @param {(any) => boolean} predicate - Function to test each element
76 * @return {List} A new List containing only elements that satisfy the predicate
77 *
78 * Handled Edge Cases:
79 * - Filtering an empty list
80 * - Predicate that matches no elements
81 * - Predicate that matches all elements
3082 */
3183 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
84 // Edge Case: Filtering an empty list
85 if (this.length() === 0) {
86 return new List([]);
87 }
88
89 const result = [];
90
91 // Edge Case: Predicate that matches no elements
92 // Edge Case: Predicate that matches all elements
93 for (let i = 0; i < this.values.length; i++) {
94 if (predicate(this.values[i])) {
95 result.push(this.values[i]);
96 }
97 }
98
99 return new List(result);
33100 }
34101
35102 /**
36 * @param {(any) => any} expression
37 * @return {List}
103 * Transforms each element in the list using a provided function
104 * @param {(any) => any} expression - Function to transform each element
105 * @return {List} A new List containing transformed elements
106 *
107 * Handled Edge Cases:
108 * - Mapping over an empty list
38109 */
39110 map(expression) {
40 throw new Error('Remove this line and implement the function');
111 // Edge Case: Mapping over an empty list
112 if (this.length() === 0) {
113 return new List([]);
114 }
115
116 const result = [];
117 for (let i = 0; i < this.values.length; i++) {
118 result.push(expression(this.values[i]));
119 }
120
121 return new List(result);
41122 }
42123
43124 /**
44 * @return {number}
125 * Returns the number of elements in the list
126 * @return {number} The count of elements in the list
127 *
128 * Handled Edge Cases:
129 * - Getting length of an empty list
45130 */
46131 length() {
47 throw new Error('Remove this line and implement the function');
132 // Edge Case: Getting length of an empty list
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 {(acc: any, el: any) => any} callback - Function to apply to each element
143 * @param {any} initial - Initial accumulator value
144 * @return {any} The final accumulator value
145 *
146 * Handled Edge Cases:
147 * - Folding an empty list (returns initial value)
54148 */
55149 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
150 // Edge Case: Folding an empty list (returns initial value)
151 if (this.length() === 0) {
152 return initial;
153 }
154
155 let accumulator = initial;
156 for (let i = 0; i < this.values.length; i++) {
157 accumulator = callback(accumulator, this.values[i]);
158 }
159
160 return accumulator;
57161 }
58162
59163 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
164 * Folds (reduces) the list from the right using a callback function
165 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
166 * @param {any} initial - Initial accumulator value
167 * @return {any} The final accumulator value
168 *
169 * Handled Edge Cases:
170 * - Folding an empty list (returns initial value)
63171 */
64172 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
173 // Edge Case: Folding an empty list (returns initial value)
174 if (this.length() === 0) {
175 return initial;
176 }
177
178 let accumulator = initial;
179 for (let i = this.values.length - 1; i >= 0; i--) {
180 accumulator = callback(accumulator, this.values[i]);
181 }
182
183 return accumulator;
66184 }
67185
68186 /**
69 * @return {List}
187 * Returns a new list with elements in reverse order
188 * @return {List} A new List with elements in reverse order
189 *
190 * Handled Edge Cases:
191 * - Reversing an empty list
70192 */
71193 reverse() {
72 throw new Error('Remove this line and implement the function');
194 // Edge Case: Reversing an empty list
195 if (this.length() === 0) {
196 return new List([]);
197 }
198
199 const result = [];
200 for (let i = this.values.length - 1; i >= 0; i--) {
201 result.push(this.values[i]);
202 }
203
204 return new List(result);
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.