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 *
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 all items from a series of lists into one 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.
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 result = result.append(others.values[i]);
63 }
64 return result;
2565 }
2666
2767 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
68 * Filters the list based on a predicate function.
69 * @param {(any) => boolean} predicate - Function to test each item.
70 * @return {List} A new list containing only items for which predicate returns true.
71 *
72 * Handled Edge Cases:
73 * - Filtering an empty list
74 * - Predicate that returns true for all items
75 * - Predicate that returns false for all items
3076 */
3177 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
78 // Edge Case: Filtering an empty list
79 if (this.length() === 0) {
80 return new List([]);
81 }
82
83 const result = [];
84 for (let i = 0; i < this.length(); i++) {
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 *
98 * Handled Edge Cases:
99 * - Mapping over an empty list
38100 */
39101 map(expression) {
40 throw new Error('Remove this line and implement the function');
102 // Edge Case: Mapping over an empty list
103 if (this.length() === 0) {
104 return new List([]);
105 }
106
107 const result = [];
108 for (let i = 0; i < this.length(); i++) {
109 result.push(expression(this.values[i]));
110 }
111
112 return new List(result);
41113 }
42114
43115 /**
44 * @return {number}
116 * Returns the total number of items in the list.
117 * @return {number} The length of the list.
118 *
119 * Handled Edge Cases:
120 * - Getting length of an empty list
45121 */
46122 length() {
47 throw new Error('Remove this line and implement the function');
123 // Edge Case: Getting length of an empty list
124 if (this.values.length === 0) {
125 return 0;
126 }
127
128 let count = 0;
129 for (let i = 0; i < this.values.length; i++) {
130 count++;
131 }
132 return count;
48133 }
49134
50135 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
136 * Folds (reduces) the list from the left using a callback function and initial accumulator.
137 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
138 * @param {any} initial - Initial accumulator value.
139 * @return {any} The final accumulator value.
140 *
141 * Handled Edge Cases:
142 * - Folding an empty list
54143 */
55144 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
145 // Edge Case: Folding an empty list
146 if (this.length() === 0) {
147 return initial;
148 }
149
150 let accumulator = initial;
151 for (let i = 0; i < this.length(); i++) {
152 accumulator = callback(accumulator, this.values[i]);
153 }
154
155 return accumulator;
57156 }
58157
59158 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
159 * Folds (reduces) the list from the right using a callback function and initial accumulator.
160 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
161 * @param {any} initial - Initial accumulator value.
162 * @return {any} The final accumulator value.
163 *
164 * Handled Edge Cases:
165 * - Folding an empty list
63166 */
64167 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
168 // Edge Case: Folding an empty list
169 if (this.length() === 0) {
170 return initial;
171 }
172
173 let accumulator = initial;
174 for (let i = this.length() - 1; i >= 0; i--) {
175 accumulator = callback(accumulator, this.values[i]);
176 }
177
178 return accumulator;
66179 }
67180
68181 /**
69 * @return {List}
182 * Reverses the order of items in the list.
183 * @return {List} A new list with items in reversed order.
184 *
185 * Handled Edge Cases:
186 * - Reversing an empty list
70187 */
71188 reverse() {
72 throw new Error('Remove this line and implement the function');
189 // Edge Case: Reversing an empty list
190 if (this.length() === 0) {
191 return new List([]);
192 }
193
194 const result = [];
195 for (let i = this.length() - 1; i >= 0; i--) {
196 result.push(this.values[i]);
197 }
198
199 return new List(result);
73200 }
74201}
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.