sn62

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 * Handled Edge Cases: Appending empty lists, appending to empty lists
1420 */
1521 append(other) {
16 throw new Error('Remove this line and implement the function');
22 // Edge Case: Appending empty lists
23 if (!other || !other.values || other.length() === 0) {
24 return new List(this.values);
25 }
26
27 // Edge Case: Appending to empty lists
28 if (this.length() === 0) {
29 return new List(other.values);
30 }
31
32 const result = [];
33 for (let i = 0; i < this.values.length; i++) {
34 result.push(this.values[i]);
35 }
36 for (let i = 0; i < other.values.length; i++) {
37 result.push(other.values[i]);
38 }
39 return new List(result);
1740 }
1841
1942 /**
20 * @param {List} others
21 * @return {List}
43 * Concatenates a series of lists into one flattened list.
44 * @param {List} others - A list containing other lists to concatenate.
45 * @return {List} A new List with all items from all lists.
46 * Handled Edge Cases: Concatenating empty lists, concatenating lists with empty sublists
2247 */
2348 concat(others) {
24 throw new Error('Remove this line and implement the function');
49 let result = new List(this.values);
50
51 // Edge Case: Concatenating empty lists
52 if (!others || !others.values) {
53 return result;
54 }
55
56 for (let i = 0; i < others.values.length; i++) {
57 const sublist = others.values[i];
58
59 // Edge Case: Concatenating lists with empty sublists
60 if (sublist instanceof List) {
61 result = result.append(sublist);
62 } else {
63 result = result.append(new List([sublist]));
64 }
65 }
66
67 return result;
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.
74 * Handled Edge Cases: Empty list, predicate that matches no items, predicate that matches all items
3075 */
3176 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
77 // Edge Case: Empty list
78 if (this.length() === 0) {
79 return new List([]);
80 }
81
82 const result = [];
83 for (let i = 0; i < this.values.length; i++) {
84 // Edge Case: Predicate that matches no items or all items
85 if (predicate(this.values[i])) {
86 result.push(this.values[i]);
87 }
88 }
89
90 return new List(result);
3391 }
3492
3593 /**
36 * @param {(any) => any} expression
37 * @return {List}
94 * Transforms each item in the list using a provided function.
95 * @param {(any) => any} expression - Function to transform each item.
96 * @return {List} A new List with transformed items.
97 * Handled Edge Cases: Empty list, transformation that produces falsy values
3898 */
3999 map(expression) {
40 throw new Error('Remove this line and implement the function');
100 // Edge Case: Empty list
101 if (this.length() === 0) {
102 return new List([]);
103 }
104
105 const result = [];
106 for (let i = 0; i < this.values.length; i++) {
107 // Edge Case: Transformation that produces falsy values (0, false, null, etc.)
108 result.push(expression(this.values[i]));
109 }
110
111 return new List(result);
41112 }
42113
43114 /**
44 * @return {number}
115 * Returns the number of items in the list.
116 * @return {number} The length of the list.
117 * Handled Edge Cases: Empty list
45118 */
46119 length() {
47 throw new Error('Remove this line and implement the function');
120 let count = 0;
121 for (let i = 0; i < this.values.length; i++) {
122 // Edge Case: Empty list (count remains 0)
123 count++;
124 }
125 return count;
48126 }
49127
50128 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
129 * Folds (reduces) the list from left to right using a callback function.
130 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
131 * @param {any} initial - Initial accumulator value.
132 * @return {any} The final accumulator value.
133 * Handled Edge Cases: Empty list, callback that returns falsy values
54134 */
55135 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
136 let accumulator = initial;
137
138 // Edge Case: Empty list (returns initial value)
139 if (this.length() === 0) {
140 return accumulator;
141 }
142
143 for (let i = 0; i < this.values.length; i++) {
144 // Edge Case: Callback that returns falsy values
145 accumulator = callback(accumulator, this.values[i]);
146 }
147
148 return accumulator;
57149 }
58150
59151 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
152 * Folds (reduces) the list from right to left using a callback function.
153 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
154 * @param {any} initial - Initial accumulator value.
155 * @return {any} The final accumulator value.
156 * Handled Edge Cases: Empty list, callback that returns falsy values
63157 */
64158 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
159 let accumulator = initial;
160
161 // Edge Case: Empty list (returns initial value)
162 if (this.length() === 0) {
163 return accumulator;
164 }
165
166 // Process from right to left
167 for (let i = this.values.length - 1; i >= 0; i--) {
168 // Edge Case: Callback that returns falsy values
169 accumulator = callback(accumulator, this.values[i]);
170 }
171
172 return accumulator;
66173 }
67174
68175 /**
69 * @return {List}
176 * Reverses the order of items in the list.
177 * @return {List} A new List with items in reversed order.
178 * Handled Edge Cases: Empty list, single-item list
70179 */
71180 reverse() {
72 throw new Error('Remove this line and implement the function');
181 // Edge Case: Empty list
182 if (this.length() === 0) {
183 return new List([]);
184 }
185
186 // Edge Case: Single-item list
187 if (this.length() === 1) {
188 return new List([this.values[0]]);
189 }
190
191 const result = [];
192 for (let i = this.values.length - 1; i >= 0; i--) {
193 result.push(this.values[i]);
194 }
195
196 return new List(result);
73197 }
74198}
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.