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
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
1419 */
1520 append(other) {
16 throw new Error('Remove this line and implement the function');
21 // Edge Case: Appending an empty list should return a copy of the original list
22 if (!other || !other.values || other.values.length === 0) {
23 return new List(this.values);
24 }
25
26 const result = [];
27 for (let i = 0; i < this.values.length; i++) {
28 result.push(this.values[i]);
29 }
30 for (let i = 0; i < other.values.length; i++) {
31 result.push(other.values[i]);
32 }
33 return new List(result);
34 // Handled Edge Cases: Appending empty list
1735 }
1836
1937 /**
20 * @param {List} others
21 * @return {List}
38 * Concatenates this list with a series of lists
39 * @param {List} others - A list containing other lists to concatenate
40 * @return {List} A new list with all items flattened
2241 */
2342 concat(others) {
24 throw new Error('Remove this line and implement the function');
43 // Edge Case: Concatenating with an empty list of lists
44 if (!others || !others.values || others.values.length === 0) {
45 return new List(this.values);
46 }
47
48 let result = [];
49
50 // Add items from the current list
51 for (let i = 0; i < this.values.length; i++) {
52 result.push(this.values[i]);
53 }
54
55 // Add items from each list in others
56 for (let i = 0; i < others.values.length; i++) {
57 const list = others.values[i];
58 // Edge Case: Handling null or undefined lists in the series
59 if (list && list.values) {
60 for (let j = 0; j < list.values.length; j++) {
61 result.push(list.values[j]);
62 }
63 }
64 }
65
66 return new List(result);
67 // Handled Edge Cases: Concatenating with empty list, handling null/undefined lists
2568 }
2669
2770 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
71 * Filters the list based on a predicate function
72 * @param {(any) => boolean} predicate - Function to test each item
73 * @return {List} A new list containing only items that satisfy the predicate
3074 */
3175 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
76 // Edge Case: Empty list should return an empty list
77 if (this.values.length === 0) {
78 return new List([]);
79 }
80
81 const result = [];
82 for (let i = 0; i < this.values.length; i++) {
83 // Edge Case: Handling items that cause predicate to throw an error
84 try {
85 if (predicate(this.values[i])) {
86 result.push(this.values[i]);
87 }
88 } catch (e) {
89 // Silently skip items that cause errors in predicate
90 continue;
91 }
92 }
93 return new List(result);
94 // Handled Edge Cases: Empty list, predicate throwing errors
3395 }
3496
3597 /**
36 * @param {(any) => any} expression
37 * @return {List}
98 * Maps each item in the list using a provided function
99 * @param {(any) => any} expression - Function to transform each item
100 * @return {List} A new list with transformed items
38101 */
39102 map(expression) {
40 throw new Error('Remove this line and implement the function');
103 // Edge Case: Empty list should return an empty list
104 if (this.values.length === 0) {
105 return new List([]);
106 }
107
108 const result = [];
109 for (let i = 0; i < this.values.length; i++) {
110 // Edge Case: Handling items that cause expression to throw an error
111 try {
112 result.push(expression(this.values[i]));
113 } catch (e) {
114 // Push undefined for items that cause errors
115 result.push(undefined);
116 }
117 }
118 return new List(result);
119 // Handled Edge Cases: Empty list, expression throwing errors
41120 }
42121
43122 /**
44 * @return {number}
123 * Returns the number of items in the list
124 * @return {number} The length of the list
45125 */
46126 length() {
47 throw new Error('Remove this line and implement the function');
127 // Edge Case: Empty list should return 0
128 if (!this.values) {
129 return 0;
130 }
131
132 let count = 0;
133 for (let i = 0; i < this.values.length; i++) {
134 count++;
135 }
136 return count;
137 // Handled Edge Cases: Null values array, empty list
48138 }
49139
50140 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
141 * Folds the list from the left using a callback function and initial accumulator
142 * @param {(acc: any, el: any) => any} callback - Function to apply to each item
143 * @param {any} initial - Initial accumulator value
144 * @return {any} The final accumulator value
54145 */
55146 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
147 // Edge Case: Empty list should return the initial value
148 if (this.values.length === 0) {
149 return initial;
150 }
151
152 let accumulator = initial;
153 for (let i = 0; i < this.values.length; i++) {
154 // Edge Case: Handling items that cause callback to throw an error
155 try {
156 accumulator = callback(accumulator, this.values[i]);
157 } catch (e) {
158 // Continue with the current accumulator value
159 continue;
160 }
161 }
162 return accumulator;
163 // Handled Edge Cases: Empty list, callback throwing errors
57164 }
58165
59166 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
167 * Folds the list from the right using a callback function and initial accumulator
168 * @param {(acc: any, el: any) => any} callback - Function to apply to each item
169 * @param {any} initial - Initial accumulator value
170 * @return {any} The final accumulator value
63171 */
64172 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
173 // Edge Case: Empty list should return the initial value
174 if (this.values.length === 0) {
175 return initial;
176 }
177
178 let accumulator = initial;
179 // Process from right to left
180 for (let i = this.values.length - 1; i >= 0; i--) {
181 // Edge Case: Handling items that cause callback to throw an error
182 try {
183 accumulator = callback(accumulator, this.values[i]);
184 } catch (e) {
185 // Continue with the current accumulator value
186 continue;
187 }
188 }
189 return accumulator;
190 // Handled Edge Cases: Empty list, callback throwing errors
66191 }
67192
68193 /**
69 * @return {List}
194 * Reverses the order of items in the list
195 * @return {List} A new list with items in reverse order
70196 */
71197 reverse() {
72 throw new Error('Remove this line and implement the function');
198 // Edge Case: Empty list should return an empty list
199 if (this.values.length === 0) {
200 return new List([]);
201 }
202
203 const result = [];
204 for (let i = this.values.length - 1; i >= 0; i--) {
205 result.push(this.values[i]);
206 }
207 return new List(result);
208 // Handled Edge Cases: Empty list
73209 }
74210}
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.