armor

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 *
20 * Handled Edge Cases:
21 * - When other list is empty
22 * - When this list is empty
1423 */
1524 append(other) {
16 throw new Error('Remove this line and implement the function');
25 // Edge Case: When other list is empty, return a copy of this list
26 if (other.length() === 0) {
27 return new List(this.values);
28 }
29
30 // Edge Case: When this list is empty, return a copy of other list
31 if (this.length() === 0) {
32 return new List(other.values);
33 }
34
35 const result = [];
36 for (let i = 0; i < this.values.length; i++) {
37 result.push(this.values[i]);
38 }
39 for (let i = 0; i < other.values.length; i++) {
40 result.push(other.values[i]);
41 }
42 return new List(result);
1743 }
1844
1945 /**
20 * @param {List} others
21 * @return {List}
46 * Concatenates a series of lists into one flattened list.
47 * @param {List} others - A list containing other lists to concatenate.
48 * @return {List} A new list with all items from all lists.
49 *
50 * Handled Edge Cases:
51 * - When others list is empty
52 * - When this list is empty
53 * - When nested lists are empty
2254 */
2355 concat(others) {
24 throw new Error('Remove this line and implement the function');
56 // Edge Case: When others list is empty, return a copy of this list
57 if (others.length() === 0) {
58 return new List(this.values);
59 }
60
61 let result = [];
62
63 // Add items from this list
64 for (let i = 0; i < this.values.length; i++) {
65 result.push(this.values[i]);
66 }
67
68 // Add items from each list in others
69 for (let i = 0; i < others.values.length; i++) {
70 const item = others.values[i];
71
72 // Edge Case: Handle nested lists
73 if (item instanceof List) {
74 for (let j = 0; j < item.values.length; j++) {
75 result.push(item.values[j]);
76 }
77 } else {
78 result.push(item);
79 }
80 }
81
82 return new List(result);
2583 }
2684
2785 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
86 * Filters the list based on a predicate function.
87 * @param {(any) => boolean} predicate - Function to test each item.
88 * @return {List} A new list containing only items that satisfy the predicate.
89 *
90 * Handled Edge Cases:
91 * - When this list is empty
92 * - When no items satisfy the predicate
93 * - When all items satisfy the predicate
3094 */
3195 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
96 // Edge Case: When this list is empty, return an empty list
97 if (this.values.length === 0) {
98 return new List([]);
99 }
100
101 const result = [];
102 for (let i = 0; i < this.values.length; i++) {
103 if (predicate(this.values[i])) {
104 result.push(this.values[i]);
105 }
106 }
107
108 // Edge Case: When no items satisfy the predicate, return empty list
109 // Edge Case: When all items satisfy the predicate, return all items
110 return new List(result);
33111 }
34112
35113 /**
36 * @param {(any) => any} expression
37 * @return {List}
114 * Transforms each item in the list using a provided function.
115 * @param {(any) => any} expression - Function to transform each item.
116 * @return {List} A new list with transformed items.
117 *
118 * Handled Edge Cases:
119 * - When this list is empty
38120 */
39121 map(expression) {
40 throw new Error('Remove this line and implement the function');
122 // Edge Case: When this list is empty, return an empty list
123 if (this.values.length === 0) {
124 return new List([]);
125 }
126
127 const result = [];
128 for (let i = 0; i < this.values.length; i++) {
129 result.push(expression(this.values[i]));
130 }
131 return new List(result);
41132 }
42133
43134 /**
44 * @return {number}
135 * Returns the number of items in the list.
136 * @return {number} The length of the list.
137 *
138 * Handled Edge Cases:
139 * - When list is empty
45140 */
46141 length() {
47 throw new Error('Remove this line and implement the function');
142 // Edge Case: When list is empty
143 let count = 0;
144 for (let i = 0; i < this.values.length; i++) {
145 count++;
146 }
147 return count;
48148 }
49149
50150 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
151 * Folds (reduces) the list from the left using a callback function.
152 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
153 * @param {any} initial - Initial accumulator value.
154 * @return {any} The final accumulator value.
155 *
156 * Handled Edge Cases:
157 * - When this list is empty
54158 */
55159 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
160 // Edge Case: When this list is empty, return initial value
161 if (this.values.length === 0) {
162 return initial;
163 }
164
165 let accumulator = initial;
166 for (let i = 0; i < this.values.length; i++) {
167 accumulator = callback(accumulator, this.values[i]);
168 }
169 return accumulator;
57170 }
58171
59172 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
173 * Folds (reduces) the list from the right using a callback function.
174 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
175 * @param {any} initial - Initial accumulator value.
176 * @return {any} The final accumulator value.
177 *
178 * Handled Edge Cases:
179 * - When this list is empty
63180 */
64181 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
182 // Edge Case: When this list is empty, return initial value
183 if (this.values.length === 0) {
184 return initial;
185 }
186
187 let accumulator = initial;
188 for (let i = this.values.length - 1; i >= 0; i--) {
189 accumulator = callback(accumulator, this.values[i]);
190 }
191 return accumulator;
66192 }
67193
68194 /**
69 * @return {List}
195 * Returns a new list with items in reversed order.
196 * @return {List} A new list with reversed items.
197 *
198 * Handled Edge Cases:
199 * - When this list is empty
200 * - When this list has only one item
70201 */
71202 reverse() {
72 throw new Error('Remove this line and implement the function');
203 // Edge Case: When this list is empty, return an empty list
204 if (this.values.length === 0) {
205 return new List([]);
206 }
207
208 // Edge Case: When this list has only one item, return a copy of the list
209 if (this.values.length === 1) {
210 return new List([this.values[0]]);
211 }
212
213 const result = [];
214 for (let i = this.values.length - 1; i >= 0; i--) {
215 result.push(this.values[i]);
216 }
217 return new List(result);
73218 }
74219}
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.