let it go

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.
1419 */
1520 append(other) {
16 throw new Error('Remove this line and implement the function');
21 // Edge Case: Appending an empty list should return the original list
22 if (!other || !other.values || other.values.length === 0) {
23 return new List(this.values);
24 }
25
26 // Edge Case: Appending to an empty list should return the other list
27 if (this.values.length === 0) {
28 return new List(other.values);
29 }
30
31 return new List([...this.values, ...other.values]);
1732 }
1833
1934 /**
20 * @param {List} others
21 * @return {List}
35 * Concatenates a series 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.
2238 */
2339 concat(others) {
24 throw new Error('Remove this line and implement the function');
40 // Edge Case: Concatenating with an empty list of lists
41 if (!others || !others.values || others.values.length === 0) {
42 return new List(this.values);
43 }
44
45 let result = [...this.values];
46 for (const list of others.values) {
47 // Edge Case: Handling null or undefined lists in the series
48 if (list && list.values) {
49 result = [...result, ...list.values];
50 }
51 }
52
53 return new List(result);
2554 }
2655
2756 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
57 * Filters the list based on a predicate function.
58 * @param {(any) => boolean} predicate - Function to test each item.
59 * @return {List} A new list containing only items that satisfy the predicate.
3060 */
3161 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
62 // Edge Case: Filtering an empty list
63 if (this.values.length === 0) {
64 return new List([]);
65 }
66
67 // Edge Case: Handling invalid predicate
68 if (typeof predicate !== 'function') {
69 return new List([]);
70 }
71
72 const result = [];
73 for (const item of this.values) {
74 if (predicate(item)) {
75 result.push(item);
76 }
77 }
78
79 return new List(result);
3380 }
3481
3582 /**
36 * @param {(any) => any} expression
37 * @return {List}
83 * Maps each item in the list using a provided function.
84 * @param {(any) => any} expression - Function to apply to each item.
85 * @return {List} A new list containing the results of applying the function.
3886 */
3987 map(expression) {
40 throw new Error('Remove this line and implement the function');
88 // Edge Case: Mapping an empty list
89 if (this.values.length === 0) {
90 return new List([]);
91 }
92
93 // Edge Case: Handling invalid expression function
94 if (typeof expression !== 'function') {
95 throw new Error('Expression must be a function');
96 }
97
98 const result = [];
99 for (const item of this.values) {
100 result.push(expression(item));
101 }
102
103 return new List(result);
41104 }
42105
43106 /**
44 * @return {number}
107 * Returns the total number of items in the list.
108 * @return {number} The length of the list.
45109 */
46110 length() {
47 throw new Error('Remove this line and implement the function');
111 // Edge Case: Calculating length of an empty list
112 let count = 0;
113 for (const _ of this.values) {
114 count++;
115 }
116 return count;
48117 }
49118
50119 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
120 * Folds (reduces) the list from the left using a callback function.
121 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
122 * @param {any} initial - Initial accumulator value.
123 * @return {any} The final accumulator value.
54124 */
55125 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
126 // Edge Case: Folding an empty list returns the initial value
127 if (this.values.length === 0) {
128 return initial;
129 }
130
131 // Edge Case: Handling invalid callback
132 if (typeof callback !== 'function') {
133 throw new Error('Callback must be a function');
134 }
135
136 let accumulator = initial;
137 for (const item of this.values) {
138 accumulator = callback(accumulator, item);
139 }
140
141 return accumulator;
57142 }
58143
59144 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
145 * Folds (reduces) the list from the right using a callback function.
146 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
147 * @param {any} initial - Initial accumulator value.
148 * @return {any} The final accumulator value.
63149 */
64150 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
151 // Edge Case: Folding an empty list returns the initial value
152 if (this.values.length === 0) {
153 return initial;
154 }
155
156 // Edge Case: Handling invalid callback
157 if (typeof callback !== 'function') {
158 throw new Error('Callback must be a function');
159 }
160
161 let accumulator = initial;
162 // Process from right to left
163 for (let i = this.values.length - 1; i >= 0; i--) {
164 accumulator = callback(accumulator, this.values[i]);
165 }
166
167 return accumulator;
66168 }
67169
68170 /**
69 * @return {List}
171 * Returns a new list with all items in reversed order.
172 * @return {List} A new list with items in reversed order.
70173 */
71174 reverse() {
72 throw new Error('Remove this line and implement the function');
175 // Edge Case: Reversing an empty list
176 if (this.values.length === 0) {
177 return new List([]);
178 }
179
180 const result = [];
181 for (let i = this.values.length - 1; i >= 0; i--) {
182 result.push(this.values[i]);
183 }
184
185 return new List(result);
73186 }
74187}
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.