chris

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: Empty lists for both this and other lists
1420 */
1521 append(other) {
16 throw new Error('Remove this line and implement the function');
22 // Edge Case: When both lists are empty
23 if (this.values.length === 0 && other.values.length === 0) {
24 return new List([]);
25 }
26
27 // Edge Case: When this list is empty
28 if (this.values.length === 0) {
29 return new List([...other.values]);
30 }
31
32 // Edge Case: When other list is empty
33 if (other.values.length === 0) {
34 return new List([...this.values]);
35 }
36
37 const result = [];
38 for (let i = 0; i < this.values.length; i++) {
39 result.push(this.values[i]);
40 }
41 for (let i = 0; i < other.values.length; i++) {
42 result.push(other.values[i]);
43 }
44
45 return new List(result);
1746 }
1847
1948 /**
20 * @param {List} others
21 * @return {List}
49 * Concatenates a list of lists into one flattened list.
50 * @param {List} others - A list containing other lists to concatenate.
51 * @return {List} A new list with all items from all lists.
52 * Handled Edge Cases: Empty list of lists, lists with empty sublists
2253 */
2354 concat(others) {
24 throw new Error('Remove this line and implement the function');
55 // Edge Case: When others is empty
56 if (others.values.length === 0) {
57 return new List([...this.values]);
58 }
59
60 const result = [];
61
62 // Add items from this list
63 for (let i = 0; i < this.values.length; i++) {
64 result.push(this.values[i]);
65 }
66
67 // Add items from each list in others
68 for (let i = 0; i < others.values.length; i++) {
69 const sublist = others.values[i];
70 // Edge Case: When sublist is not a List instance (defensive programming)
71 if (sublist instanceof List) {
72 for (let j = 0; j < sublist.values.length; j++) {
73 result.push(sublist.values[j]);
74 }
75 } else {
76 // If it's a regular array, add its elements
77 for (let j = 0; j < sublist.length; j++) {
78 result.push(sublist[j]);
79 }
80 }
81 }
82
83 return new List(result);
2584 }
2685
2786 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
87 * Filters the list based on a predicate function.
88 * @param {(any) => boolean} predicate - Function to test each item.
89 * @return {List} A new list containing only items that satisfy the predicate.
90 * Handled Edge Cases: Empty list, no items match predicate, all items match predicate
3091 */
3192 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
93 // Edge Case: When this list is empty
94 if (this.values.length === 0) {
95 return new List([]);
96 }
97
98 const result = [];
99 for (let i = 0; i < this.values.length; i++) {
100 if (predicate(this.values[i])) {
101 result.push(this.values[i]);
102 }
103 }
104
105 return new List(result);
33106 }
34107
35108 /**
36 * @param {(any) => any} expression
37 * @return {List}
109 * Transforms each item in the list using a mapping function.
110 * @param {(any) => any} expression - Function to transform each item.
111 * @return {List} A new list with transformed items.
112 * Handled Edge Cases: Empty list
38113 */
39114 map(expression) {
40 throw new Error('Remove this line and implement the function');
115 // Edge Case: When this list is empty
116 if (this.values.length === 0) {
117 return new List([]);
118 }
119
120 const result = [];
121 for (let i = 0; i < this.values.length; i++) {
122 result.push(expression(this.values[i]));
123 }
124
125 return new List(result);
41126 }
42127
43128 /**
44 * @return {number}
129 * Returns the number of items in the list.
130 * @return {number} The length of the list.
131 * Handled Edge Cases: Empty list
45132 */
46133 length() {
47 throw new Error('Remove this line and implement the function');
134 // Edge Case: When this list is empty
135 let count = 0;
136 for (let i = 0; i < this.values.length; i++) {
137 count++;
138 }
139 return count;
48140 }
49141
50142 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
143 * Folds (reduces) the list from left to 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
54148 */
55149 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
150 // Edge Case: When this list is empty
151 if (this.values.length === 0) {
152 return initial;
153 }
154
155 let accumulator = initial;
156 for (let i = 0; i < this.values.length; i++) {
157 accumulator = callback(accumulator, this.values[i]);
158 }
159
160 return accumulator;
57161 }
58162
59163 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
164 * Folds (reduces) the list from right to left using a callback function.
165 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
166 * @param {any} initial - Initial accumulator value.
167 * @return {any} The final accumulator value.
168 * Handled Edge Cases: Empty list
63169 */
64170 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
171 // Edge Case: When this list is empty
172 if (this.values.length === 0) {
173 return initial;
174 }
175
176 let accumulator = initial;
177 // Process from right to left
178 for (let i = this.values.length - 1; i >= 0; i--) {
179 accumulator = callback(accumulator, this.values[i]);
180 }
181
182 return accumulator;
66183 }
67184
68185 /**
69 * @return {List}
186 * Returns a new list with items in reverse order.
187 * @return {List} A new list with reversed items.
188 * Handled Edge Cases: Empty list
70189 */
71190 reverse() {
72 throw new Error('Remove this line and implement the function');
191 // Edge Case: When this list is empty
192 if (this.values.length === 0) {
193 return new List([]);
194 }
195
196 const result = [];
197 for (let i = this.values.length - 1; i >= 0; i--) {
198 result.push(this.values[i]);
199 }
200
201 return new List(result);
73202 }
74203}
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.