fistinz

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 // Edge Case: Handle empty others list
53 if (!others || others.length() === 0) {
54 return new List(this.values);
55 }
56
57 let result = [...this.values];
58
59 // Edge Case: Handle nested empty lists
60 for (let i = 0; i < others.length(); i++) {
61 const list = others.values[i];
62 if (list instanceof List) {
63 result = [...result, ...list.values];
64 }
65 }
66
67 return new List(result);
2568 }
2669
2770 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
71 * Filters the list based on a predicate function
72 * @param {(any) => boolean} predicate - Function to test each element
73 * @return {List} A new list containing only elements that pass the predicate
74 *
75 * Handled Edge Cases: Empty list, all elements filtered out, no elements filtered out
3076 */
3177 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
78 // Edge Case: Handle empty list
79 if (this.values.length === 0) {
80 return new List([]);
81 }
82
83 // Edge Case: Handle invalid predicate
84 if (typeof predicate !== 'function') {
85 return new List([]);
86 }
87
88 const result = [];
89 for (let i = 0; i < this.values.length; i++) {
90 if (predicate(this.values[i])) {
91 result.push(this.values[i]);
92 }
93 }
94
95 return new List(result);
3396 }
3497
3598 /**
36 * @param {(any) => any} expression
37 * @return {List}
99 * Maps each element using a provided function
100 * @param {(any) => any} expression - Function to transform each element
101 * @return {List} A new list with transformed elements
102 *
103 * Handled Edge Cases: Empty list, null/undefined expression
38104 */
39105 map(expression) {
40 throw new Error('Remove this line and implement the function');
106 // Edge Case: Handle empty list
107 if (this.values.length === 0) {
108 return new List([]);
109 }
110
111 // Edge Case: Handle invalid expression
112 if (typeof expression !== 'function') {
113 return new List([]);
114 }
115
116 const result = [];
117 for (let i = 0; i < this.values.length; i++) {
118 result.push(expression(this.values[i]));
119 }
120
121 return new List(result);
41122 }
42123
43124 /**
44 * @return {number}
125 * Returns the length of the list
126 * @return {number} The number of elements in the list
127 *
128 * Handled Edge Cases: Empty list
45129 */
46130 length() {
47 throw new Error('Remove this line and implement the function');
131 // Edge Case: Handle empty list
132 let count = 0;
133 for (let i = 0; i < this.values.length; i++) {
134 count++;
135 }
136 return count;
48137 }
49138
50139 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
140 * Folds the list from the left using a callback function
141 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
142 * @param {any} initial - Initial accumulator value
143 * @return {any} The final accumulator value
144 *
145 * Handled Edge Cases: Empty list, null/undefined callback
54146 */
55147 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
148 // Edge Case: Handle invalid callback
149 if (typeof callback !== 'function') {
150 return initial;
151 }
152
153 // Edge Case: Handle empty list
154 if (this.values.length === 0) {
155 return initial;
156 }
157
158 let accumulator = initial;
159 for (let i = 0; i < this.values.length; i++) {
160 accumulator = callback(accumulator, this.values[i]);
161 }
162
163 return accumulator;
57164 }
58165
59166 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
167 * Folds the list from the right using a callback function
168 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
169 * @param {any} initial - Initial accumulator value
170 * @return {any} The final accumulator value
171 *
172 * Handled Edge Cases: Empty list, null/undefined callback
63173 */
64174 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
175 // Edge Case: Handle invalid callback
176 if (typeof callback !== 'function') {
177 return initial;
178 }
179
180 // Edge Case: Handle empty list
181 if (this.values.length === 0) {
182 return initial;
183 }
184
185 let accumulator = initial;
186 for (let i = this.values.length - 1; i >= 0; i--) {
187 accumulator = callback(accumulator, this.values[i]);
188 }
189
190 return accumulator;
66191 }
67192
68193 /**
69 * @return {List}
194 * Reverses the list
195 * @return {List} A new list with elements in reverse order
196 *
197 * Handled Edge Cases: Empty list, single element list
70198 */
71199 reverse() {
72 throw new Error('Remove this line and implement the function');
200 // Edge Case: Handle empty list
201 if (this.values.length === 0) {
202 return new List([]);
203 }
204
205 // Edge Case: Handle single element list
206 if (this.values.length === 1) {
207 return new List([this.values[0]]);
208 }
209
210 const result = [];
211 for (let i = this.values.length - 1; i >= 0; i--) {
212 result.push(this.values[i]);
213 }
214
215 return new List(result);
73216 }
74217}
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.