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
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 const result = [];
34 for (let i = 0; i < this.values.length; i++) {
35 result.push(this.values[i]);
36 }
37 for (let i = 0; i < other.values.length; i++) {
38 result.push(other.values[i]);
39 }
40 return new List(result);
1741 }
1842
1943 /**
20 * @param {List} others
21 * @return {List}
44 * Concatenates multiple lists into one
45 * @param {List} others - A list of lists to concatenate
46 * @return {List} A new list with all elements from all lists flattened
47 *
48 * Handled Edge Cases: Empty lists, nested empty lists
2249 */
2350 concat(others) {
24 throw new Error('Remove this line and implement the function');
51 // Edge Case: Handle empty others list
52 if (!others || others.values.length === 0) {
53 return new List(this.values);
54 }
55
56 let result = [];
57
58 // Add elements from current list
59 for (let i = 0; i < this.values.length; i++) {
60 result.push(this.values[i]);
61 }
62
63 // Add elements from each list in others
64 for (let i = 0; i < others.values.length; i++) {
65 const list = others.values[i];
66 // Edge Case: Handle if an element in others is not a List
67 if (list && Array.isArray(list.values)) {
68 for (let j = 0; j < list.values.length; j++) {
69 result.push(list.values[j]);
70 }
71 }
72 }
73
74 return new List(result);
2575 }
2676
2777 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
78 * Filters the list based on a predicate function
79 * @param {(any) => boolean} predicate - Function to test each element
80 * @return {List} A new list containing only elements that pass the predicate
81 *
82 * Handled Edge Cases: Empty list, all elements filtered out, all elements pass
3083 */
3184 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
85 // Edge Case: Handle empty list
86 if (this.values.length === 0) {
87 return new List([]);
88 }
89
90 const result = [];
91 for (let i = 0; i < this.values.length; i++) {
92 if (predicate(this.values[i])) {
93 result.push(this.values[i]);
94 }
95 }
96
97 return new List(result);
3398 }
3499
35100 /**
36 * @param {(any) => any} expression
37 * @return {List}
101 * Transforms each element using the provided function
102 * @param {(any) => any} expression - Function to transform each element
103 * @return {List} A new list with transformed elements
104 *
105 * Handled Edge Cases: Empty list
38106 */
39107 map(expression) {
40 throw new Error('Remove this line and implement the function');
108 // Edge Case: Handle empty list
109 if (this.values.length === 0) {
110 return new List([]);
111 }
112
113 const result = [];
114 for (let i = 0; i < this.values.length; i++) {
115 result.push(expression(this.values[i]));
116 }
117
118 return new List(result);
41119 }
42120
43121 /**
44 * @return {number}
122 * Returns the number of elements in the list
123 * @return {number} The length of the list
124 *
125 * Handled Edge Cases: Empty list
45126 */
46127 length() {
47 throw new Error('Remove this line and implement the function');
128 // Edge Case: Handle empty list
129 let count = 0;
130 for (let i = 0; i < this.values.length; i++) {
131 count++;
132 }
133 return count;
48134 }
49135
50136 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
137 * Folds the list from the left using a callback function
138 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
139 * @param {any} initial - Initial accumulator value
140 * @return {any} The final accumulator value
141 *
142 * Handled Edge Cases: Empty list
54143 */
55144 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
145 // Edge Case: Handle empty list
146 if (this.values.length === 0) {
147 return initial;
148 }
149
150 let accumulator = initial;
151 for (let i = 0; i < this.values.length; i++) {
152 accumulator = callback(accumulator, this.values[i]);
153 }
154
155 return accumulator;
57156 }
58157
59158 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
159 * Folds the list from the right using a callback function
160 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
161 * @param {any} initial - Initial accumulator value
162 * @return {any} The final accumulator value
163 *
164 * Handled Edge Cases: Empty list
63165 */
64166 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
167 // Edge Case: Handle empty list
168 if (this.values.length === 0) {
169 return initial;
170 }
171
172 let accumulator = initial;
173 for (let i = this.values.length - 1; i >= 0; i--) {
174 accumulator = callback(accumulator, this.values[i]);
175 }
176
177 return accumulator;
66178 }
67179
68180 /**
69 * @return {List}
181 * Reverses the order of elements in the list
182 * @return {List} A new list with elements in reverse order
183 *
184 * Handled Edge Cases: Empty list
70185 */
71186 reverse() {
72 throw new Error('Remove this line and implement the function');
187 // Edge Case: Handle empty list
188 if (this.values.length === 0) {
189 return new List([]);
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.