unicode

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: other is null or undefined
24 if (other === null || other === undefined) {
25 return new List(this.values);
26 }
27
28 // Edge Case: both lists are empty
29 if (this.values.length === 0 && other.values.length === 0) {
30 return new List([]);
31 }
32
33 // Edge Case: this list is empty
34 if (this.values.length === 0) {
35 return new List(other.values);
36 }
37
38 // Edge Case: other list is empty
39 if (other.values.length === 0) {
40 return new List(this.values);
41 }
42
43 return new List([...this.values, ...other.values]);
1744 }
1845
1946 /**
20 * @param {List} others
21 * @return {List}
47 * Concatenates multiple lists into one
48 * @param {List} others - A list of lists to concatenate
49 * @return {List} A new list with all elements from all lists flattened
50 *
51 * Handled Edge Cases: empty lists, nested empty lists, null/undefined elements
2252 */
2353 concat(others) {
24 throw new Error('Remove this line and implement the function');
54 // Edge Case: others is null or undefined
55 if (others === null || others === undefined) {
56 return new List(this.values);
57 }
58
59 let result = [...this.values];
60
61 // Edge Case: others is empty
62 if (others.length() === 0) {
63 return new List(result);
64 }
65
66 for (let i = 0; i < others.values.length; i++) {
67 const list = others.values[i];
68 // Edge Case: element in others is not a List object
69 if (list instanceof List) {
70 result = [...result, ...list.values];
71 } else {
72 // If it's not a List, treat it as a single element
73 result.push(list);
74 }
75 }
76
77 return new List(result);
2578 }
2679
2780 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
81 * Filters the list based on a predicate function
82 * @param {(any) => boolean} predicate - Function to test each element
83 * @return {List} A new list containing only elements that pass the predicate
84 *
85 * Handled Edge Cases: empty list, predicate function throws error, all elements filtered out
3086 */
3187 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
88 // Edge Case: empty list
89 if (this.values.length === 0) {
90 return new List([]);
91 }
92
93 // Edge Case: predicate is not a function
94 if (typeof predicate !== 'function') {
95 throw new Error('Predicate must be a function');
96 }
97
98 const result = [];
99 for (let i = 0; i < this.values.length; i++) {
100 try {
101 if (predicate(this.values[i])) {
102 result.push(this.values[i]);
103 }
104 } catch (error) {
105 // Edge Case: predicate throws an error for an element
106 // We skip that element and continue with others
107 continue;
108 }
109 }
110
111 return new List(result);
33112 }
34113
35114 /**
36 * @param {(any) => any} expression
37 * @return {List}
115 * Maps each element using a provided function
116 * @param {(any) => any} expression - Function to transform each element
117 * @return {List} A new list with transformed elements
118 *
119 * Handled Edge Cases: empty list, expression function throws error
38120 */
39121 map(expression) {
40 throw new Error('Remove this line and implement the function');
122 // Edge Case: empty list
123 if (this.values.length === 0) {
124 return new List([]);
125 }
126
127 // Edge Case: expression is not a function
128 if (typeof expression !== 'function') {
129 throw new Error('Expression must be a function');
130 }
131
132 const result = [];
133 for (let i = 0; i < this.values.length; i++) {
134 try {
135 result.push(expression(this.values[i]));
136 } catch (error) {
137 // Edge Case: expression throws an error for an element
138 // We re-throw to maintain expected behavior
139 throw error;
140 }
141 }
142
143 return new List(result);
41144 }
42145
43146 /**
44 * @return {number}
147 * Returns the length of the list
148 * @return {number} The number of elements in the list
149 *
150 * Handled Edge Cases: empty list
45151 */
46152 length() {
47 throw new Error('Remove this line and implement the function');
153 // Edge Case: empty list
154 return this.values.length;
48155 }
49156
50157 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
158 * Folds the list from the left using a callback function and initial accumulator
159 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
160 * @param {any} initial - Initial accumulator value
161 * @return {any} The final accumulator value
162 *
163 * Handled Edge Cases: empty list, callback function throws error
54164 */
55165 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
166 // Edge Case: callback is not a function
167 if (typeof callback !== 'function') {
168 throw new Error('Callback must be a function');
169 }
170
171 let accumulator = initial;
172
173 // Edge Case: empty list - return initial value
174 if (this.values.length === 0) {
175 return accumulator;
176 }
177
178 for (let i = 0; i < this.values.length; i++) {
179 try {
180 accumulator = callback(accumulator, this.values[i]);
181 } catch (error) {
182 // Edge Case: callback throws an error
183 // We re-throw to maintain expected behavior
184 throw error;
185 }
186 }
187
188 return accumulator;
57189 }
58190
59191 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
192 * Folds the list from the right using a callback function and initial accumulator
193 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
194 * @param {any} initial - Initial accumulator value
195 * @return {any} The final accumulator value
196 *
197 * Handled Edge Cases: empty list, callback function throws error
63198 */
64199 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
200 // Edge Case: callback is not a function
201 if (typeof callback !== 'function') {
202 throw new Error('Callback must be a function');
203 }
204
205 let accumulator = initial;
206
207 // Edge Case: empty list - return initial value
208 if (this.values.length === 0) {
209 return accumulator;
210 }
211
212 // Process from right to left
213 for (let i = this.values.length - 1; i >= 0; i--) {
214 try {
215 accumulator = callback(accumulator, this.values[i]);
216 } catch (error) {
217 // Edge Case: callback throws an error
218 // We re-throw to maintain expected behavior
219 throw error;
220 }
221 }
222
223 return accumulator;
66224 }
67225
68226 /**
69 * @return {List}
227 * Reverses the list
228 * @return {List} A new list with elements in reverse order
229 *
230 * Handled Edge Cases: empty list, single element list
70231 */
71232 reverse() {
72 throw new Error('Remove this line and implement the function');
233 // Edge Case: empty list
234 if (this.values.length === 0) {
235 return new List([]);
236 }
237
238 // Edge Case: single element list
239 if (this.values.length === 1) {
240 return new List([this.values[0]]);
241 }
242
243 const result = [];
244 for (let i = this.values.length - 1; i >= 0; i--) {
245 result.push(this.values[i]);
246 }
247
248 return new List(result);
73249 }
74250}
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.