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
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 * - When other list is empty
22 * - When this list is empty
23 * - When both lists are empty
1424 */
1525 append(other) {
16 throw new Error('Remove this line and implement the function');
26 // Edge Case: Handle when other list is not a List instance
27 if (!(other instanceof List)) {
28 throw new Error('Argument must be a List');
29 }
30
31 return new List(this.values.concat(other.values));
1732 }
1833
1934 /**
20 * @param {List} others
21 * @return {List}
35 * Concatenates a list of lists into one flattened list
36 * @param {List} others - A list containing other lists to concatenate
37 * @return {List} A new List with all items from all lists flattened
38 *
39 * Handled Edge Cases:
40 * - When others list is empty
41 * - When some contained lists are empty
42 * - When all contained lists are empty
2243 */
2344 concat(others) {
24 throw new Error('Remove this line and implement the function');
45 // Edge Case: Handle when others is not a List instance
46 if (!(others instanceof List)) {
47 throw new Error('Argument must be a List');
48 }
49
50 let result = this.values.slice(); // Start with a copy of current list
51
52 // Edge Case: Handle when others list is empty
53 if (others.length() === 0) {
54 return new List(result);
55 }
56
57 for (let i = 0; i < others.length(); i++) {
58 const item = others.values[i];
59 // Edge Case: Handle when item is not a List instance
60 if (item instanceof List) {
61 result = result.concat(item.values);
62 } else {
63 // If item is not a List, we treat it as a single element
64 result.push(item);
65 }
66 }
67
68 return new List(result);
2569 }
2670
2771 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
72 * Filters the list based on a predicate function
73 * @param {Function} predicate - Function that returns true for items to keep
74 * @return {List} A new List containing only items for which predicate returned true
75 *
76 * Handled Edge Cases:
77 * - When list is empty
78 * - When no items match the predicate
79 * - When all items match the predicate
3080 */
3181 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
82 // Edge Case: Handle when predicate is not a function
83 if (typeof predicate !== 'function') {
84 throw new Error('Predicate must be a function');
85 }
86
87 const result = [];
88
89 // Edge Case: Handle when list is empty
90 if (this.length() === 0) {
91 return new List(result);
92 }
93
94 for (let i = 0; i < this.values.length; i++) {
95 if (predicate(this.values[i])) {
96 result.push(this.values[i]);
97 }
98 }
99
100 return new List(result);
33101 }
34102
35103 /**
36 * @param {(any) => any} expression
37 * @return {List}
104 * Transforms each item in the list using a provided function
105 * @param {Function} expression - Function to apply to each item
106 * @return {List} A new List containing transformed items
107 *
108 * Handled Edge Cases:
109 * - When list is empty
110 * - When expression function modifies types of elements
38111 */
39112 map(expression) {
40 throw new Error('Remove this line and implement the function');
113 // Edge Case: Handle when expression is not a function
114 if (typeof expression !== 'function') {
115 throw new Error('Expression must be a function');
116 }
117
118 const result = [];
119
120 // Edge Case: Handle when list is empty
121 if (this.length() === 0) {
122 return new List(result);
123 }
124
125 for (let i = 0; i < this.values.length; i++) {
126 result.push(expression(this.values[i]));
127 }
128
129 return new List(result);
41130 }
42131
43132 /**
44 * @return {number}
133 * Returns the number of items in the list
134 * @return {number} The count of items in the list
135 *
136 * Handled Edge Cases:
137 * - When list is empty (returns 0)
45138 */
46139 length() {
47 throw new Error('Remove this line and implement the function');
140 let count = 0;
141
142 // Edge Case: Handle when list is empty
143 if (this.values === undefined || this.values === null) {
144 return count;
145 }
146
147 for (let i = 0; i < this.values.length; i++) {
148 count++;
149 }
150
151 return count;
48152 }
49153
50154 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
155 * Folds (reduces) the list from left to right using a callback function
156 * @param {Function} callback - Function to apply to each item with accumulator
157 * @param {any} initial - Initial value for the accumulator
158 * @return {any} The final accumulated value
159 *
160 * Handled Edge Cases:
161 * - When list is empty (returns initial value)
162 * - When callback is not a function
54163 */
55164 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
165 // Edge Case: Handle when callback is not a function
166 if (typeof callback !== 'function') {
167 throw new Error('Callback must be a function');
168 }
169
170 let accumulator = initial;
171
172 // Edge Case: Handle when list is empty
173 if (this.length() === 0) {
174 return accumulator;
175 }
176
177 for (let i = 0; i < this.values.length; i++) {
178 accumulator = callback(accumulator, this.values[i]);
179 }
180
181 return accumulator;
57182 }
58183
59184 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
185 * Folds (reduces) the list from right to left using a callback function
186 * @param {Function} callback - Function to apply to each item with accumulator
187 * @param {any} initial - Initial value for the accumulator
188 * @return {any} The final accumulated value
189 *
190 * Handled Edge Cases:
191 * - When list is empty (returns initial value)
192 * - When callback is not a function
63193 */
64194 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
195 // Edge Case: Handle when callback is not a function
196 if (typeof callback !== 'function') {
197 throw new Error('Callback must be a function');
198 }
199
200 let accumulator = initial;
201
202 // Edge Case: Handle when list is empty
203 if (this.length() === 0) {
204 return accumulator;
205 }
206
207 // Process from right to left
208 for (let i = this.values.length - 1; i >= 0; i--) {
209 accumulator = callback(accumulator, this.values[i]);
210 }
211
212 return accumulator;
66213 }
67214
68215 /**
69 * @return {List}
216 * Returns a new list with items in reverse order
217 * @return {List} A new List with items in reverse order
218 *
219 * Handled Edge Cases:
220 * - When list is empty
221 * - When list has only one item
70222 */
71223 reverse() {
72 throw new Error('Remove this line and implement the function');
224 const result = [];
225
226 // Edge Case: Handle when list is empty
227 if (this.length() === 0) {
228 return new List(result);
229 }
230
231 // Edge Case: Handle when list has only one item
232 if (this.length() === 1) {
233 return new List([this.values[0]]);
234 }
235
236 for (let i = this.values.length - 1; i >= 0; i--) {
237 result.push(this.values[i]);
238 }
239
240 return new List(result);
73241 }
74242}
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.