armor

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 * - 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.length(); i++) {
37 result.push(this.values[i]);
38 }
39 for (let i = 0; i < other.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 series of lists into one flattened list.
47 * @param {List} others - A list of lists to concatenate.
48 * @return {List} A new list containing all items from all lists.
49 *
50 * Handled Edge Cases:
51 * - Concatenating with an empty list of lists
52 * - Concatenating when this list is empty
2253 */
2354 concat(others) {
24 throw new Error('Remove this line and implement the function');
55 // Edge Case: Concatenating with 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 for (let i = 0; i < others.length(); i++) {
62 const list = others.values[i];
63 // Edge Case: Handling non-List objects in the list of lists
64 if (list instanceof List) {
65 result = result.append(list);
66 } else {
67 // If it's not a List, treat it as a single element
68 result = result.append(new List([list]));
69 }
70 }
71 return result;
2572 }
2673
2774 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
75 * Filters the list based on a predicate function.
76 * @param {(any) => boolean} predicate - Function to test each element.
77 * @return {List} A new list containing only elements that satisfy the predicate.
78 *
79 * Handled Edge Cases:
80 * - Filtering an empty list
81 * - Predicate function that returns true for all elements
82 * - Predicate function that returns false for all elements
3083 */
3184 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
85 // Edge Case: Filtering an empty list
86 if (this.length() === 0) {
87 return new List([]);
88 }
89
90 const result = [];
91 for (let i = 0; i < this.length(); i++) {
92 // Edge Case: Predicate function throwing an error
93 try {
94 if (predicate(this.values[i])) {
95 result.push(this.values[i]);
96 }
97 } catch (error) {
98 // Re-throw the error as the predicate is expected to work correctly
99 throw error;
100 }
101 }
102
103 // Edge Case: Predicate function that returns true for all elements
104 // Edge Case: Predicate function that returns false for all elements
105 return new List(result);
33106 }
34107
35108 /**
36 * @param {(any) => any} expression
37 * @return {List}
109 * Maps each element of the list using a provided function.
110 * @param {(any) => any} expression - Function to apply to each element.
111 * @return {List} A new list containing the results of applying the function.
112 *
113 * Handled Edge Cases:
114 * - Mapping an empty list
115 * - Expression function that returns undefined
38116 */
39117 map(expression) {
40 throw new Error('Remove this line and implement the function');
118 // Edge Case: Mapping an empty list
119 if (this.length() === 0) {
120 return new List([]);
121 }
122
123 const result = [];
124 for (let i = 0; i < this.length(); i++) {
125 // Edge Case: Expression function throwing an error
126 try {
127 result.push(expression(this.values[i]));
128 } catch (error) {
129 // Re-throw the error as the expression is expected to work correctly
130 throw error;
131 }
132 }
133
134 // Edge Case: Expression function that returns undefined
135 return new List(result);
41136 }
42137
43138 /**
44 * @return {number}
139 * Returns the number of elements in the list.
140 * @return {number} The length of the list.
141 *
142 * Handled Edge Cases:
143 * - Getting length of an empty list
45144 */
46145 length() {
47 throw new Error('Remove this line and implement the function');
146 // Edge Case: Getting length of an empty list
147 let count = 0;
148 for (let i = 0; i < this.values.length; i++) {
149 count++;
150 }
151 return count;
48152 }
49153
50154 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
155 * Folds (reduces) the list from the left using a callback function.
156 * @param {(acc: any, el: any) => any} callback - Function to apply to each element.
157 * @param {any} initial - Initial accumulator value.
158 * @return {any} The final accumulator value.
159 *
160 * Handled Edge Cases:
161 * - Folding an empty list
162 * - Callback function that throws an error
54163 */
55164 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
165 // Edge Case: Folding an empty list
166 if (this.length() === 0) {
167 return initial;
168 }
169
170 let accumulator = initial;
171 for (let i = 0; i < this.length(); i++) {
172 // Edge Case: Callback function throwing an error
173 try {
174 accumulator = callback(accumulator, this.values[i]);
175 } catch (error) {
176 // Re-throw the error as the callback is expected to work correctly
177 throw error;
178 }
179 }
180 return accumulator;
57181 }
58182
59183 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
184 * Folds (reduces) the list from the right using a callback function.
185 * @param {(acc: any, el: any) => any} callback - Function to apply to each element.
186 * @param {any} initial - Initial accumulator value.
187 * @return {any} The final accumulator value.
188 *
189 * Handled Edge Cases:
190 * - Folding an empty list
191 * - Callback function that throws an error
63192 */
64193 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
194 // Edge Case: Folding an empty list
195 if (this.length() === 0) {
196 return initial;
197 }
198
199 let accumulator = initial;
200 // Process elements from right to left
201 for (let i = this.length() - 1; i >= 0; i--) {
202 // Edge Case: Callback function throwing an error
203 try {
204 accumulator = callback(accumulator, this.values[i]);
205 } catch (error) {
206 // Re-throw the error as the callback is expected to work correctly
207 throw error;
208 }
209 }
210 return accumulator;
66211 }
67212
68213 /**
69 * @return {List}
214 * Reverses the order of elements in the list.
215 * @return {List} A new list with elements in reversed order.
216 *
217 * Handled Edge Cases:
218 * - Reversing an empty list
219 * - Reversing a list with one element
70220 */
71221 reverse() {
72 throw new Error('Remove this line and implement the function');
222 // Edge Case: Reversing an empty list
223 if (this.length() === 0) {
224 return new List([]);
225 }
226
227 // Edge Case: Reversing a list with one element
228 if (this.length() === 1) {
229 return new List([this.values[0]]);
230 }
231
232 const result = [];
233 for (let i = this.length() - 1; i >= 0; i--) {
234 result.push(this.values[i]);
235 }
236 return new List(result);
73237 }
74238}
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.