JiaYou

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.
19 * Handled Edge Cases: Appending empty lists, appending to empty lists
1420 */
1521 append(other) {
16 throw new Error('Remove this line and implement the function');
22 // Edge Case: Appending empty lists
23 if (!other || !other.values || other.length() === 0) {
24 return new List(this.values);
25 }
26
27 // Edge Case: Appending to empty lists
28 if (this.length() === 0) {
29 return new List(other.values);
30 }
31
32 const result = [];
33 for (let i = 0; i < this.values.length; i++) {
34 result.push(this.values[i]);
35 }
36 for (let i = 0; i < other.values.length; i++) {
37 result.push(other.values[i]);
38 }
39 return new List(result);
1740 }
1841
1942 /**
20 * @param {List} others
21 * @return {List}
43 * Concatenates a series of lists into one flattened list.
44 * @param {List} others - A list containing other lists to concatenate.
45 * @return {List} A new list with all items from all lists.
46 * Handled Edge Cases: Concatenating empty lists, concatenating lists with empty sublists
2247 */
2348 concat(others) {
24 throw new Error('Remove this line and implement the function');
49 let result = new List(this.values);
50
51 // Edge Case: Concatenating empty lists
52 if (!others || others.length() === 0) {
53 return result;
54 }
55
56 for (let i = 0; i < others.values.length; i++) {
57 const sublist = others.values[i];
58 // Edge Case: Handling null or undefined sublists
59 if (sublist && sublist.values) {
60 result = result.append(sublist);
61 } else if (Array.isArray(sublist)) {
62 result = result.append(new List(sublist));
63 }
64 }
65 return result;
2566 }
2667
2768 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
69 * Filters the list based on a predicate function.
70 * @param {(any) => boolean} predicate - Function to test each item.
71 * @return {List} A new list containing only items that satisfy the predicate.
72 * Handled Edge Cases: Empty list, predicate function throws exception
3073 */
3174 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
75 // Edge Case: Empty list
76 if (this.length() === 0) {
77 return new List([]);
78 }
79
80 const result = [];
81 for (let i = 0; i < this.values.length; i++) {
82 try {
83 // Edge Case: Predicate function throws exception
84 if (predicate(this.values[i])) {
85 result.push(this.values[i]);
86 }
87 } catch (e) {
88 // Silently ignore items that cause predicate to throw
89 continue;
90 }
91 }
92 return new List(result);
3393 }
3494
3595 /**
36 * @param {(any) => any} expression
37 * @return {List}
96 * Transforms each item in the list using a provided function.
97 * @param {(any) => any} expression - Function to transform each item.
98 * @return {List} A new list with transformed items.
99 * Handled Edge Cases: Empty list, transformation function throws exception
38100 */
39101 map(expression) {
40 throw new Error('Remove this line and implement the function');
102 // Edge Case: Empty list
103 if (this.length() === 0) {
104 return new List([]);
105 }
106
107 const result = [];
108 for (let i = 0; i < this.values.length; i++) {
109 try {
110 // Edge Case: Transformation function throws exception
111 result.push(expression(this.values[i]));
112 } catch (e) {
113 // Add undefined for items that cause transformation to throw
114 result.push(undefined);
115 }
116 }
117 return new List(result);
41118 }
42119
43120 /**
44 * @return {number}
121 * Returns the number of items in the list.
122 * @return {number} The length of the list.
123 * Handled Edge Cases: Empty list, list with null/undefined values
45124 */
46125 length() {
47 throw new Error('Remove this line and implement the function');
126 // Edge Case: Empty list
127 if (!this.values) {
128 return 0;
129 }
130
131 let count = 0;
132 for (let i = 0; i < this.values.length; i++) {
133 count++;
134 }
135 return count;
48136 }
49137
50138 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
139 * Folds (reduces) the list from the left using a callback function.
140 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
141 * @param {any} initial - Initial accumulator value.
142 * @return {any} The final accumulator value.
143 * Handled Edge Cases: Empty list, callback function throws exception
54144 */
55145 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
146 let accumulator = initial;
147
148 // Edge Case: Empty list
149 if (this.length() === 0) {
150 return accumulator;
151 }
152
153 for (let i = 0; i < this.values.length; i++) {
154 try {
155 // Edge Case: Callback function throws exception
156 accumulator = callback(accumulator, this.values[i]);
157 } catch (e) {
158 // Return current accumulator if callback throws
159 return accumulator;
160 }
161 }
162 return accumulator;
57163 }
58164
59165 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
166 * Folds (reduces) the list from the right using a callback function.
167 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
168 * @param {any} initial - Initial accumulator value.
169 * @return {any} The final accumulator value.
170 * Handled Edge Cases: Empty list, callback function throws exception
63171 */
64172 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
173 let accumulator = initial;
174
175 // Edge Case: Empty list
176 if (this.length() === 0) {
177 return accumulator;
178 }
179
180 // Process from right to left
181 for (let i = this.values.length - 1; i >= 0; i--) {
182 try {
183 // Edge Case: Callback function throws exception
184 accumulator = callback(accumulator, this.values[i]);
185 } catch (e) {
186 // Return current accumulator if callback throws
187 return accumulator;
188 }
189 }
190 return accumulator;
66191 }
67192
68193 /**
69 * @return {List}
194 * Reverses the order of items in the list.
195 * @return {List} A new list with items in reversed order.
196 * Handled Edge Cases: Empty list, list with single item
70197 */
71198 reverse() {
72 throw new Error('Remove this line and implement the function');
199 // Edge Case: Empty list
200 if (this.length() === 0) {
201 return new List([]);
202 }
203
204 // Edge Case: List with single item
205 if (this.length() === 1) {
206 return new List([this.values[0]]);
207 }
208
209 const result = [];
210 for (let i = this.values.length - 1; i >= 0; i--) {
211 result.push(this.values[i]);
212 }
213 return new List(result);
73214 }
74215}
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.