perfect

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 another list to this list
17 * @param {List} other - The list to append
18 * @return {List} A new list containing all elements from both lists
19 *
20 * Handled Edge Cases: Empty lists, null/undefined other list
1421 */
1522 append(other) {
16 throw new Error('Remove this line and implement the function');
23 // Edge Case: Handle null or undefined other list
24 if (!other) {
25 return new List(this.values);
26 }
27
28 // Edge Case: Handle empty lists
29 if (this.values.length === 0 && other.values.length === 0) {
30 return new List([]);
31 }
32
33 if (this.values.length === 0) {
34 return new List(other.values);
35 }
36
37 if (other.values.length === 0) {
38 return new List(this.values);
39 }
40
41 return new List([...this.values, ...other.values]);
1742 }
1843
1944 /**
20 * @param {List} others
21 * @return {List}
45 * Concatenates multiple lists into one
46 * @param {List} others - A list of lists to concatenate
47 * @return {List} A new list with all elements from all lists flattened
48 *
49 * Handled Edge Cases: Empty lists, nested empty lists
2250 */
2351 concat(others) {
24 throw new Error('Remove this line and implement the function');
52 let result = [...this.values];
53
54 // Edge Case: Handle empty others list
55 if (!others || others.length() === 0) {
56 return new List(result);
57 }
58
59 // Process each list in others
60 for (let i = 0; i < others.values.length; i++) {
61 const list = others.values[i];
62 if (list instanceof List) {
63 result = [...result, ...list.values];
64 } else {
65 result.push(list);
66 }
67 }
68
69 return new List(result);
2570 }
2671
2772 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
73 * Filters the list based on a predicate function
74 * @param {(any) => boolean} predicate - Function to test each element
75 * @return {List} A new list containing only elements that pass the predicate
76 *
77 * Handled Edge Cases: Empty list, all elements filtered out, no elements filtered out
3078 */
3179 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
80 // Edge Case: Handle empty list
81 if (this.values.length === 0) {
82 return new List([]);
83 }
84
85 const result = [];
86 for (let i = 0; i < this.values.length; i++) {
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 * Transforms each element using the provided function
97 * @param {(any) => any} expression - Function to transform each element
98 * @return {List} A new list with transformed elements
99 *
100 * Handled Edge Cases: Empty list
38101 */
39102 map(expression) {
40 throw new Error('Remove this line and implement the function');
103 // Edge Case: Handle empty list
104 if (this.values.length === 0) {
105 return new List([]);
106 }
107
108 const result = [];
109 for (let i = 0; i < this.values.length; i++) {
110 result.push(expression(this.values[i]));
111 }
112
113 return new List(result);
41114 }
42115
43116 /**
44 * @return {number}
117 * Returns the number of elements in the list
118 * @return {number} The length of the list
119 *
120 * Handled Edge Cases: Empty list
45121 */
46122 length() {
47 throw new Error('Remove this line and implement the function');
123 // Edge Case: Handle empty list
124 let count = 0;
125 for (let i = 0; i < this.values.length; i++) {
126 count++;
127 }
128 return count;
48129 }
49130
50131 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
132 * Folds the list from the left using a callback function
133 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
134 * @param {any} initial - Initial accumulator value
135 * @return {any} The final accumulator value
136 *
137 * Handled Edge Cases: Empty list
54138 */
55139 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
140 // Edge Case: Handle empty list
141 if (this.values.length === 0) {
142 return initial;
143 }
144
145 let accumulator = initial;
146 for (let i = 0; i < this.values.length; i++) {
147 accumulator = callback(accumulator, this.values[i]);
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 the list from the right using a callback function
155 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
156 * @param {any} initial - Initial accumulator value
157 * @return {any} The final accumulator value
158 *
159 * Handled Edge Cases: Empty list
63160 */
64161 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
162 // Edge Case: Handle empty list
163 if (this.values.length === 0) {
164 return initial;
165 }
166
167 let accumulator = initial;
168 for (let i = this.values.length - 1; i >= 0; i--) {
169 accumulator = callback(accumulator, this.values[i]);
170 }
171
172 return accumulator;
66173 }
67174
68175 /**
69 * @return {List}
176 * Reverses the order of elements in the list
177 * @return {List} A new list with elements in reverse order
178 *
179 * Handled Edge Cases: Empty list, single element list
70180 */
71181 reverse() {
72 throw new Error('Remove this line and implement the function');
182 // Edge Case: Handle empty list
183 if (this.values.length === 0) {
184 return new List([]);
185 }
186
187 // Edge Case: Handle single element list
188 if (this.values.length === 1) {
189 return new List([this.values[0]]);
190 }
191
192 const result = [];
193 for (let i = this.values.length - 1; i >= 0; i--) {
194 result.push(this.values[i]);
195 }
196
197 return new List(result);
73198 }
74199}
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.