harrison

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 // Store values without using spread operator or other built-in functions
13 this.values = [];
14 for (let i = 0; i < values.length; i++) {
15 this.values[i] = values[i];
16 }
917 }
1018
1119 /**
12 * @param {List} other
13 * @return {List}
20 * Appends all items from another list to the end of this list.
21 * @param {List} other - The list to append.
22 * @return {List} A new List containing all items from both lists.
23 *
24 * Handled Edge Cases:
25 * - Appending an empty list to another list
26 * - Appending a list to an empty list
1427 */
1528 append(other) {
16 throw new Error('Remove this line and implement the function');
29 // Create a new array to hold all values without using spread operator
30 const result = [];
31
32 // Copy values from this list
33 for (let i = 0; i < this.values.length; i++) {
34 result[i] = this.values[i];
35 }
36
37 // Copy values from other list
38 for (let i = 0; i < other.values.length; i++) {
39 result[this.values.length + i] = other.values[i];
40 }
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 * - Concatenating with an empty list of lists
52 * - Concatenating when the input list contains empty lists
2253 */
2354 concat(others) {
24 throw new Error('Remove this line and implement the function');
55 // Start with values from this list
56 const result = [];
57 for (let i = 0; i < this.values.length; i++) {
58 result[i] = this.values[i];
59 }
60
61 // Add values from each list in others
62 let resultIndex = this.values.length;
63 for (let i = 0; i < others.values.length; i++) {
64 const list = others.values[i];
65 if (list instanceof List) {
66 for (let j = 0; j < list.values.length; j++) {
67 result[resultIndex] = list.values[j];
68 resultIndex++;
69 }
70 }
71 }
72
73 return new List(result);
2574 }
2675
2776 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
77 * Filters the list based on a predicate function.
78 * @param {(any) => boolean} predicate - Function to test each item.
79 * @return {List} A new List containing only items that satisfy the predicate.
80 *
81 * Handled Edge Cases:
82 * - Filtering an empty list
83 * - Predicate that matches no items
84 * - Predicate that matches all items
3085 */
3186 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
87 const result = [];
88 let resultIndex = 0;
89
90 for (let i = 0; i < this.values.length; i++) {
91 if (predicate(this.values[i])) {
92 result[resultIndex] = this.values[i];
93 resultIndex++;
94 }
95 }
96
97 return new List(result);
3398 }
3499
35100 /**
36 * @param {(any) => any} expression
37 * @return {List}
101 * Maps each item in the list using a provided function.
102 * @param {(any) => any} expression - Function to apply to each item.
103 * @return {List} A new List with the results of applying the function.
104 *
105 * Handled Edge Cases:
106 * - Mapping an empty list
38107 */
39108 map(expression) {
40 throw new Error('Remove this line and implement the function');
109 const result = [];
110
111 for (let i = 0; i < this.values.length; i++) {
112 result[i] = expression(this.values[i]);
113 }
114
115 return new List(result);
41116 }
42117
43118 /**
44 * @return {number}
119 * Returns the number of items in the list.
120 * @return {number} The length of the list.
121 *
122 * Handled Edge Cases:
123 * - Getting length of an empty list
45124 */
46125 length() {
47 throw new Error('Remove this line and implement the function');
126 // Count elements manually without using array.length property directly in logic
127 let count = 0;
128 for (let i = 0; i < this.values.length; i++) {
129 count++;
130 }
131
132 return count;
48133 }
49134
50135 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
136 * Folds (reduces) the list from the left using a callback function.
137 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
138 * @param {any} initial - Initial accumulator value.
139 * @return {any} The final accumulator value.
140 *
141 * Handled Edge Cases:
142 * - Folding an empty list (returns initial value)
54143 */
55144 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
145 let accumulator = initial;
146 for (let i = 0; i < this.values.length; i++) {
147 accumulator = callback(accumulator, this.values[i]);
148 }
149
150 return accumulator;
57151 }
58152
59153 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
154 * Folds (reduces) the list from the right using a callback function.
155 * @param {(acc: any, el: any) => any} callback - Function to apply to each item.
156 * @param {any} initial - Initial accumulator value.
157 * @return {any} The final accumulator value.
158 *
159 * Handled Edge Cases:
160 * - Folding an empty list (returns initial value)
63161 */
64162 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
163 let accumulator = initial;
164 for (let i = this.values.length - 1; i >= 0; i--) {
165 accumulator = callback(accumulator, this.values[i]);
166 }
167
168 return accumulator;
66169 }
67170
68171 /**
69 * @return {List}
172 * Reverses the order of items in the list.
173 * @return {List} A new List with items in reversed order.
174 *
175 * Handled Edge Cases:
176 * - Reversing an empty list
70177 */
71178 reverse() {
72 throw new Error('Remove this line and implement the function');
179 const result = [];
180
181 for (let i = 0; i < this.values.length; i++) {
182 result[i] = this.values[this.values.length - 1 - i];
183 }
184
185 return new List(result);
73186 }
74187}
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.