viola

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 with appended items.
19 * Handled Edge Cases: Appending an empty list, appending to an empty list
1420 */
1521 append(other) {
16 throw new Error('Remove this line and implement the function');
22 // Edge Case: Appending an empty list
23 if (other.length() === 0) {
24 return new List(this.values);
25 }
26
27 // Edge Case: Appending to an empty list
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 with an empty list of lists, concatenating empty lists
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 with an empty list of lists
52 if (others.length() === 0) {
53 return result;
54 }
55
56 for (let i = 0; i < others.values.length; i++) {
57 const list = others.values[i];
58 // Edge Case: Concatenating empty lists
59 if (list instanceof List) {
60 result = result.append(list);
61 }
62 }
63 return result;
2564 }
2665
2766 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
67 * Filters the list based on a predicate function.
68 * @param {(any) => boolean} predicate - Function to test each item.
69 * @return {List} A new List with items that pass the predicate test.
70 * Handled Edge Cases: Empty list, no items match predicate, all items match predicate
3071 */
3172 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
73 // Edge Case: Empty list
74 if (this.length() === 0) {
75 return new List([]);
76 }
77
78 const result = [];
79 for (let i = 0; i < this.values.length; i++) {
80 // Edge Case: No items match predicate
81 if (predicate(this.values[i])) {
82 result.push(this.values[i]);
83 }
84 }
85 // Edge Case: All items match predicate (implicitly handled)
86 return new List(result);
3387 }
3488
3589 /**
36 * @param {(any) => any} expression
37 * @return {List}
90 * Transforms each item in the list using a provided function.
91 * @param {(any) => any} expression - Function to transform each item.
92 * @return {List} A new List with transformed items.
93 * Handled Edge Cases: Empty list
3894 */
3995 map(expression) {
40 throw new Error('Remove this line and implement the function');
96 // Edge Case: Empty list
97 if (this.length() === 0) {
98 return new List([]);
99 }
100
101 const result = [];
102 for (let i = 0; i < this.values.length; i++) {
103 result.push(expression(this.values[i]));
104 }
105 return new List(result);
41106 }
42107
43108 /**
44 * @return {number}
109 * Returns the total number of items in the list.
110 * @return {number} The length of the list.
111 * Handled Edge Cases: Empty list
45112 */
46113 length() {
47 throw new Error('Remove this line and implement the function');
114 let count = 0;
115 for (let i = 0; i < this.values.length; i++) {
116 count++;
117 }
118 // Edge Case: Empty list (count remains 0)
119 return count;
48120 }
49121
50122 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
123 * Folds (reduces) the list from the left using a callback function and initial accumulator.
124 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
125 * @param {any} initial - Initial accumulator value.
126 * @return {any} The final accumulator value.
127 * Handled Edge Cases: Empty list
54128 */
55129 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
130 let accumulator = initial;
131
132 // Edge Case: Empty list
133 if (this.length() === 0) {
134 return accumulator;
135 }
136
137 for (let i = 0; i < this.values.length; i++) {
138 accumulator = callback(accumulator, this.values[i]);
139 }
140 return accumulator;
57141 }
58142
59143 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
144 * Folds (reduces) the list from the right using a callback function and initial accumulator.
145 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
146 * @param {any} initial - Initial accumulator value.
147 * @return {any} The final accumulator value.
148 * Handled Edge Cases: Empty list
63149 */
64150 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
151 let accumulator = initial;
152
153 // Edge Case: Empty list
154 if (this.length() === 0) {
155 return accumulator;
156 }
157
158 for (let i = this.values.length - 1; i >= 0; i--) {
159 accumulator = callback(accumulator, this.values[i]);
160 }
161 return accumulator;
66162 }
67163
68164 /**
69 * @return {List}
165 * Returns a new list with all original items in reversed order.
166 * @return {List} A new List with reversed items.
167 * Handled Edge Cases: Empty list
70168 */
71169 reverse() {
72 throw new Error('Remove this line and implement the function');
170 // Edge Case: Empty list
171 if (this.length() === 0) {
172 return new List([]);
173 }
174
175 const result = [];
176 for (let i = this.values.length - 1; i >= 0; i--) {
177 result.push(this.values[i]);
178 }
179 return new List(result);
73180 }
74181}
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.