great

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 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 result = result.append(list);
59 }
60 return result;
2561 }
2662
2763 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
64 * Filters the list based on a predicate function.
65 * @param {(any) => boolean} predicate - Function to test each item.
66 * @return {List} A new list containing only items that satisfy the predicate.
67 * Handled Edge Cases: Empty list, predicate that matches no items, predicate that matches all items
3068 */
3169 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
70 // Edge Case: Empty list
71 if (this.length() === 0) {
72 return new List([]);
73 }
74
75 const result = [];
76 for (let i = 0; i < this.values.length; i++) {
77 if (predicate(this.values[i])) {
78 result.push(this.values[i]);
79 }
80 }
81
82 // Edge Case: Predicate that matches no items
83 // Edge Case: Predicate that matches all items
84 return new List(result);
3385 }
3486
3587 /**
36 * @param {(any) => any} expression
37 * @return {List}
88 * Transforms each item in the list using a provided function.
89 * @param {(any) => any} expression - Function to transform each item.
90 * @return {List} A new list with transformed items.
91 * Handled Edge Cases: Empty list
3892 */
3993 map(expression) {
40 throw new Error('Remove this line and implement the function');
94 // Edge Case: Empty list
95 if (this.length() === 0) {
96 return new List([]);
97 }
98
99 const result = [];
100 for (let i = 0; i < this.values.length; i++) {
101 result.push(expression(this.values[i]));
102 }
103 return new List(result);
41104 }
42105
43106 /**
44 * @return {number}
107 * Returns the number of items in the list.
108 * @return {number} The length of the list.
109 * Handled Edge Cases: Empty list
45110 */
46111 length() {
47 throw new Error('Remove this line and implement the function');
112 let count = 0;
113 for (let i = 0; i < this.values.length; i++) {
114 count++;
115 }
116
117 // Edge Case: Empty list
118 return count;
48119 }
49120
50121 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
122 * Folds (reduces) the list from the left using a callback function.
123 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
124 * @param {any} initial - Initial accumulator value.
125 * @return {any} The final accumulator value.
126 * Handled Edge Cases: Empty list
54127 */
55128 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
129 let accumulator = initial;
130
131 // Edge Case: Empty list
132 if (this.length() === 0) {
133 return accumulator;
134 }
135
136 for (let i = 0; i < this.values.length; i++) {
137 accumulator = callback(accumulator, this.values[i]);
138 }
139 return accumulator;
57140 }
58141
59142 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
143 * Folds (reduces) the list from the right using a callback function.
144 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
145 * @param {any} initial - Initial accumulator value.
146 * @return {any} The final accumulator value.
147 * Handled Edge Cases: Empty list
63148 */
64149 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
150 let accumulator = initial;
151
152 // Edge Case: Empty list
153 if (this.length() === 0) {
154 return accumulator;
155 }
156
157 for (let i = this.values.length - 1; i >= 0; i--) {
158 accumulator = callback(accumulator, this.values[i]);
159 }
160 return accumulator;
66161 }
67162
68163 /**
69 * @return {List}
164 * Reverses the order of items in the list.
165 * @return {List} A new list with items in reversed order.
166 * Handled Edge Cases: Empty list, single item list
70167 */
71168 reverse() {
72 throw new Error('Remove this line and implement the function');
169 // Edge Case: Empty list
170 if (this.length() === 0) {
171 return new List([]);
172 }
173
174 // Edge Case: Single item list
175 if (this.length() === 1) {
176 return new List([this.values[0]]);
177 }
178
179 const result = [];
180 for (let i = this.values.length - 1; i >= 0; i--) {
181 result.push(this.values[i]);
182 }
183 return new List(result);
73184 }
74185}
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.