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
9 * @param {Array} [values=[]] - Initial values for the list
10 */
11 constructor(values = []) {
12 // Edge Case: Handle when values is not an array by defaulting to empty array
13 this.values = Array.isArray(values) ? values : [];
914 }
1015
1116 /**
12 * @param {List} other
13 * @return {List}
17 * Appends all items from another list to the end of this list
18 * @param {List} other - The list to append
19 * @return {List} A new list containing all items from both lists
1420 */
1521 append(other) {
16 throw new Error('Remove this line and implement the function');
22 // Edge Case: Handle when other is not a List instance
23 if (!(other instanceof List)) {
24 throw new Error('Argument must be a List');
25 }
26
27 return new List(this.values.concat(other.values));
28 // Handled Edge Cases: other not being a List instance
1729 }
1830
1931 /**
20 * @param {List} others
21 * @return {List}
32 * Concatenates a series of lists into one flattened list
33 * @param {List} others - A list containing other lists to concatenate
34 * @return {List} A new list with all items from all lists
2235 */
2336 concat(others) {
24 throw new Error('Remove this line and implement the function');
37 // Edge Case: Handle when others is not a List instance
38 if (!(others instanceof List)) {
39 throw new Error('Argument must be a List');
40 }
41
42 let result = this.values.slice(); // Copy current values
43
44 // Edge Case: Handle when items in others are not Lists themselves
45 for (const item of others.values) {
46 if (item instanceof List) {
47 result = result.concat(item.values);
48 } else {
49 result.push(item);
50 }
51 }
52
53 return new List(result);
54 // Handled Edge Cases: others not being a List instance, items in others not being Lists
2555 }
2656
2757 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
58 * Filters the list based on a predicate function
59 * @param {(any) => boolean} predicate - Function to test each item
60 * @return {List} A new list containing only items where predicate returns true
3061 */
3162 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
63 // Edge Case: Handle when predicate is not a function
64 if (typeof predicate !== 'function') {
65 throw new Error('Predicate must be a function');
66 }
67
68 const result = [];
69
70 // Edge Case: Handle empty list
71 for (let i = 0; i < this.values.length; i++) {
72 if (predicate(this.values[i])) {
73 result.push(this.values[i]);
74 }
75 }
76
77 return new List(result);
78 // Handled Edge Cases: predicate not being a function, empty list
3379 }
3480
3581 /**
36 * @param {(any) => any} expression
37 * @return {List}
82 * Maps each item in the list using a provided function
83 * @param {(any) => any} expression - Function to transform each item
84 * @return {List} A new list with transformed items
3885 */
3986 map(expression) {
40 throw new Error('Remove this line and implement the function');
87 // Edge Case: Handle when expression is not a function
88 if (typeof expression !== 'function') {
89 throw new Error('Expression must be a function');
90 }
91
92 const result = [];
93
94 // Edge Case: Handle empty list
95 for (let i = 0; i < this.values.length; i++) {
96 result.push(expression(this.values[i]));
97 }
98
99 return new List(result);
100 // Handled Edge Cases: expression not being a function, empty list
41101 }
42102
43103 /**
44 * @return {number}
104 * Returns the length of the list
105 * @return {number} The number of items in the list
45106 */
46107 length() {
47 throw new Error('Remove this line and implement the function');
108 let count = 0;
109
110 // Edge Case: Handle empty list
111 for (let i = 0; i < this.values.length; i++) {
112 count++;
113 }
114
115 return count;
116 // Handled Edge Cases: empty list
48117 }
49118
50119 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
120 * Folds the list from the left using a callback function and initial accumulator
121 * @param {(acc: any, el: any) => any} callback - Function to apply to each item
122 * @param {any} initial - Initial accumulator value
123 * @return {any} The final accumulator value
54124 */
55125 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
126 // Edge Case: Handle when callback is not a function
127 if (typeof callback !== 'function') {
128 throw new Error('Callback must be a function');
129 }
130
131 let accumulator = initial;
132
133 // Edge Case: Handle empty list
134 for (let i = 0; i < this.values.length; i++) {
135 accumulator = callback(accumulator, this.values[i]);
136 }
137
138 return accumulator;
139 // Handled Edge Cases: callback not being a function, empty list
57140 }
58141
59142 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
143 * Folds the list from the right using a callback function and initial accumulator
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
63147 */
64148 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
149 // Edge Case: Handle when callback is not a function
150 if (typeof callback !== 'function') {
151 throw new Error('Callback must be a function');
152 }
153
154 let accumulator = initial;
155
156 // Edge Case: Handle empty list
157 for (let i = this.values.length - 1; i >= 0; i--) {
158 accumulator = callback(accumulator, this.values[i]);
159 }
160
161 return accumulator;
162 // Handled Edge Cases: callback not being a function, empty list
66163 }
67164
68165 /**
69 * @return {List}
166 * Returns a new list with items in reverse order
167 * @return {List} A new list with reversed items
70168 */
71169 reverse() {
72 throw new Error('Remove this line and implement the function');
170 const result = [];
171
172 // Edge Case: Handle empty list
173 for (let i = this.values.length - 1; i >= 0; i--) {
174 result.push(this.values[i]);
175 }
176
177 return new List(result);
178 // Handled Edge Cases: empty list
73179 }
74180}
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.