yahoo

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 return new List([...this.values, ...other.values]);
1736 }
1837
1938 /**
20 * @param {List} others
21 * @return {List}
39 * Concatenates all items from a series of lists into one flattened list.
40 * @param {List} others - A list containing other lists to concatenate.
41 * @return {List} A new list with all items from all lists.
42 *
43 * Handled Edge Cases:
44 * - Concatenating with an empty list of lists
45 * - Concatenating empty lists
2246 */
2347 concat(others) {
24 throw new Error('Remove this line and implement the function');
48 // Edge Case: Concatenating with an empty list of lists
49 if (others.length() === 0) {
50 return new List(this.values);
51 }
52
53 let result = [...this.values];
54
55 // Edge Case: Concatenating empty lists
56 for (let i = 0; i < others.length(); i++) {
57 const list = others.values[i];
58 if (list instanceof List) {
59 result = [...result, ...list.values];
60 }
61 }
62
63 return new List(result);
2564 }
2665
2766 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
67 * Filters the list based on a predicate function.
68 * @param {(any) => boolean} predicate - Function to test each item.
69 * @return {List} A new list containing only items that satisfy the predicate.
70 *
71 * Handled Edge Cases:
72 * - Filtering an empty list
73 * - Predicate that matches no items
74 * - Predicate that matches all items
3075 */
3176 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
77 // Edge Case: Filtering an empty list
78 if (this.length() === 0) {
79 return new List([]);
80 }
81
82 const result = [];
83
84 for (let i = 0; i < this.values.length; i++) {
85 // Edge Case: Predicate that matches no items
86 // Edge Case: Predicate that matches all items
87 if (predicate(this.values[i])) {
88 result.push(this.values[i]);
89 }
90 }
91
92 return new List(result);
3393 }
3494
3595 /**
36 * @param {(any) => any} expression
37 * @return {List}
96 * Maps each item in the list using a provided function.
97 * @param {(any) => any} expression - Function to apply to each item.
98 * @return {List} A new list with the results of applying the function.
99 *
100 * Handled Edge Cases:
101 * - Mapping an empty list
38102 */
39103 map(expression) {
40 throw new Error('Remove this line and implement the function');
104 // Edge Case: Mapping an empty list
105 if (this.length() === 0) {
106 return new List([]);
107 }
108
109 const result = [];
110
111 for (let i = 0; i < this.values.length; i++) {
112 result.push(expression(this.values[i]));
113 }
114
115 return new List(result);
41116 }
42117
43118 /**
44 * @return {number}
119 * Returns the total number of items in the list.
120 * @return {number} The length of the list.
121 *
122 * Handled Edge Cases:
123 * - Getting length of an empty list
45124 */
46125 length() {
47 throw new Error('Remove this line and implement the function');
126 // Edge Case: Getting length of an empty list
127 if (!this.values) {
128 return 0;
129 }
130
131 let count = 0;
132 for (let i = 0; i < this.values.length; i++) {
133 count++;
134 }
135
136 return count;
48137 }
49138
50139 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
140 * Folds (reduces) the list from the left using a callback function and initial accumulator.
141 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
142 * @param {any} initial - Initial accumulator value.
143 * @return {any} The final accumulator value.
144 *
145 * Handled Edge Cases:
146 * - Folding an empty list
54147 */
55148 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
149 // Edge Case: Folding an empty list
150 if (this.length() === 0) {
151 return initial;
152 }
153
154 let accumulator = initial;
155
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 and initial accumulator.
165 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
166 * @param {any} initial - Initial accumulator value.
167 * @return {any} The final accumulator value.
168 *
169 * Handled Edge Cases:
170 * - Folding an empty list
63171 */
64172 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
173 // Edge Case: Folding an empty list
174 if (this.length() === 0) {
175 return initial;
176 }
177
178 let accumulator = initial;
179
180 // Process from right to left
181 for (let i = this.values.length - 1; i >= 0; i--) {
182 accumulator = callback(accumulator, this.values[i]);
183 }
184
185 return accumulator;
66186 }
67187
68188 /**
69 * @return {List}
189 * Reverses the order of items in the list.
190 * @return {List} A new list with items in reversed order.
191 *
192 * Handled Edge Cases:
193 * - Reversing an empty list
70194 */
71195 reverse() {
72 throw new Error('Remove this line and implement the function');
196 // Edge Case: Reversing an empty list
197 if (this.length() === 0) {
198 return new List([]);
199 }
200
201 const result = [];
202
203 for (let i = this.values.length - 1; i >= 0; i--) {
204 result.push(this.values[i]);
205 }
206
207 return new List(result);
73208 }
74209}
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.