harrison

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
26 if (other.length() === 0) {
27 return new List(this.values);
28 }
29
30 // Edge Case: Appending 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 when the input contains 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 (const list of others.values) {
56 // Edge Case: Handling empty lists in the input
57 if (list instanceof List) {
58 result = [...result, ...list.values];
59 }
60 }
61
62 return new List(result);
2563 }
2664
2765 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
66 * Filters the list based on a predicate function.
67 * @param {(any) => boolean} predicate - Function to test each item.
68 * @return {List} A new list containing only items that satisfy the predicate.
69 *
70 * Handled Edge Cases:
71 * - Filtering an empty list
72 * - Predicate that matches no items
73 * - Predicate that matches all items
3074 */
3175 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
76 // Edge Case: Filtering an empty list
77 if (this.length() === 0) {
78 return new List([]);
79 }
80
81 const result = [];
82 for (const item of this.values) {
83 if (predicate(item)) {
84 result.push(item);
85 }
86 }
87
88 // Edge Case: Predicate matches no items or all items (implicitly handled)
89 return new List(result);
3390 }
3491
3592 /**
36 * @param {(any) => any} expression
37 * @return {List}
93 * Transforms each item in the list using a provided function.
94 * @param {(any) => any} expression - Function to apply to each item.
95 * @return {List} A new list with transformed items.
96 *
97 * Handled Edge Cases:
98 * - Mapping over an empty list
3899 */
39100 map(expression) {
40 throw new Error('Remove this line and implement the function');
101 // Edge Case: Mapping over an empty list
102 if (this.length() === 0) {
103 return new List([]);
104 }
105
106 const result = [];
107 for (const item of this.values) {
108 result.push(expression(item));
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 *
118 * Handled Edge Cases:
119 * - Getting length of an empty list
45120 */
46121 length() {
47 throw new Error('Remove this line and implement the function');
122 // Edge Case: Empty list
123 let count = 0;
124 for (const _ of this.values) {
125 count++;
126 }
127 return count;
48128 }
49129
50130 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
131 * Folds (reduces) the list from the left using a callback function.
132 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
133 * @param {any} initial - Initial accumulator value.
134 * @return {any} The final accumulator value.
135 *
136 * Handled Edge Cases:
137 * - Folding an empty list (returns initial value)
54138 */
55139 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
140 // Edge Case: Folding an empty list
141 if (this.length() === 0) {
142 return initial;
143 }
144
145 let accumulator = initial;
146 for (const item of this.values) {
147 accumulator = callback(accumulator, item);
148 }
149
150 return accumulator;
57151 }
58152
59153 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
154 * Folds (reduces) the list from the right using a callback function.
155 * @param {(el: any, acc: any) => any} callback - Function to apply to each item.
156 * @param {any} initial - Initial accumulator value.
157 * @return {any} The final accumulator value.
158 *
159 * Handled Edge Cases:
160 * - Folding an empty list (returns initial value)
63161 */
64162 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
163 // Edge Case: Folding an empty list
164 if (this.length() === 0) {
165 return initial;
166 }
167
168 let accumulator = initial;
169 // Process items from right to left
170 for (let i = this.values.length - 1; i >= 0; i--) {
171 accumulator = callback(this.values[i], accumulator);
172 }
173
174 return accumulator;
66175 }
67176
68177 /**
69 * @return {List}
178 * Reverses the order of items in the list.
179 * @return {List} A new list with items in reversed order.
180 *
181 * Handled Edge Cases:
182 * - Reversing an empty list
70183 */
71184 reverse() {
72 throw new Error('Remove this line and implement the function');
185 // Edge Case: Reversing an empty list
186 if (this.length() === 0) {
187 return new List([]);
188 }
189
190 const result = [];
191 for (let i = this.values.length - 1; i >= 0; i--) {
192 result.push(this.values[i]);
193 }
194
195 return new List(result);
73196 }
74197}
Test NameStatus
direction dependent function applied to non-empty list
Fail
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
empty list
Pass
non-empty list
Pass
list of lists is not flattened
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.