rockstar

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 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 let result = [...this.values];
49
50 // Edge Case: Concatenating with an empty list of lists
51 if (others.length() === 0) {
52 return new List(result);
53 }
54
55 for (let i = 0; i < others.length(); i++) {
56 const list = others.values[i];
57
58 // Edge Case: Concatenating empty lists
59 if (list instanceof List) {
60 result = [...result, ...list.values];
61 } else {
62 result = [...result, list];
63 }
64 }
65
66 return new List(result);
2567 }
2668
2769 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
70 * Filters the list based on a predicate function.
71 * @param {(any) => boolean} predicate - Function to test each item.
72 * @return {List} A new list containing only items that satisfy the predicate.
73 *
74 * Handled Edge Cases:
75 * - Filtering an empty list
76 * - Predicate that matches no items
77 * - Predicate that matches all items
3078 */
3179 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
80 // Edge Case: Filtering an empty list
81 if (this.length() === 0) {
82 return new List([]);
83 }
84
85 const result = [];
86 for (let i = 0; i < this.values.length; i++) {
87 // Edge Case: Predicate that matches no items
88 // Edge Case: Predicate that matches all items
89 if (predicate(this.values[i])) {
90 result.push(this.values[i]);
91 }
92 }
93
94 return new List(result);
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 apply to each item.
100 * @return {List} A new list with the results of applying the function.
101 *
102 * Handled Edge Cases:
103 * - Mapping an empty list
38104 */
39105 map(expression) {
40 throw new Error('Remove this line and implement the function');
106 // Edge Case: Mapping an empty list
107 if (this.length() === 0) {
108 return new List([]);
109 }
110
111 const result = [];
112 for (let i = 0; i < this.values.length; i++) {
113 result.push(expression(this.values[i]));
114 }
115
116 return new List(result);
41117 }
42118
43119 /**
44 * @return {number}
120 * Returns the total number of items in the list.
121 * @return {number} The length of the list.
122 *
123 * Handled Edge Cases:
124 * - Getting length of an empty list
45125 */
46126 length() {
47 throw new Error('Remove this line and implement the function');
127 // Edge Case: Getting length of an empty list
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
137 return count;
48138 }
49139
50140 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
141 * Folds (reduces) the list from the left using a callback function.
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.
145 *
146 * Handled Edge Cases:
147 * - Folding an empty list
148 * - Using different types for accumulator and list items
54149 */
55150 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
151 // Edge Case: Folding an empty list
152 if (this.length() === 0) {
153 return initial;
154 }
155
156 let accumulator = initial;
157
158 // Edge Case: Using different types for accumulator and list items
159 for (let i = 0; i < this.values.length; i++) {
160 accumulator = callback(accumulator, this.values[i]);
161 }
162
163 return accumulator;
57164 }
58165
59166 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
167 * Folds (reduces) the list from the right using a callback function.
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.
171 *
172 * Handled Edge Cases:
173 * - Folding an empty list
174 * - Using different types for accumulator and list items
63175 */
64176 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
177 // Edge Case: Folding an empty list
178 if (this.length() === 0) {
179 return initial;
180 }
181
182 let accumulator = initial;
183
184 // Edge Case: Using different types for accumulator and list items
185 for (let i = this.values.length - 1; i >= 0; i--) {
186 accumulator = callback(accumulator, this.values[i]);
187 }
188
189 return accumulator;
66190 }
67191
68192 /**
69 * @return {List}
193 * Reverses the order of items in the list.
194 * @return {List} A new list with items in reversed order.
195 *
196 * Handled Edge Cases:
197 * - Reversing an empty list
198 * - Reversing a list with one item
70199 */
71200 reverse() {
72 throw new Error('Remove this line and implement the function');
201 // Edge Case: Reversing an empty list
202 if (this.length() === 0) {
203 return new List([]);
204 }
205
206 // Edge Case: Reversing a list with one item
207 if (this.length() === 1) {
208 return new List([this.values[0]]);
209 }
210
211 const result = [];
212 for (let i = this.values.length - 1; i >= 0; i--) {
213 result.push(this.values[i]);
214 }
215
216 return new List(result);
73217 }
74218}
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.