relax

Finished
44//
55
66export class List {
7 constructor() {
8 throw new Error('Remove this line and implement the function');
7 constructor(values = []) {
8 this.values = values;
99 }
1010
1111 /**
12 * @param {List} other
13 * @return {List}
12 * Appends all items from another list to the end of this list
13 * @param {List} other - The list to append
14 * @return {List} A new list containing all items from both lists
1415 */
1516 append(other) {
16 throw new Error('Remove this line and implement the function');
17 // Edge Case: Appending an empty list should return a copy of the original list
18 return new List(this.values.concat(other.values));
1719 }
1820
1921 /**
20 * @param {List} others
21 * @return {List}
22 * Concatenates a series of lists into one flattened list
23 * @param {List} others - A list of lists to concatenate
24 * @return {List} A new list with all items from all lists
2225 */
2326 concat(others) {
24 throw new Error('Remove this line and implement the function');
27 let result = [...this.values];
28
29 // Edge Case: Concatenating with an empty list of lists should return the original list
30 for (let i = 0; i < others.length(); i++) {
31 result = result.concat(others.values[i].values);
32 }
33
34 return new List(result);
2535 }
2636
2737 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
38 * Filters the list based on a predicate function
39 * @param {(any) => boolean} predicate - Function to test each item
40 * @return {List} A new list containing only items that satisfy the predicate
3041 */
3142 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
43 const result = [];
44
45 // Edge Case: Filtering an empty list should return an empty list
46 for (let i = 0; i < this.values.length; i++) {
47 if (predicate(this.values[i])) {
48 result.push(this.values[i]);
49 }
50 }
51
52 return new List(result);
3353 }
3454
3555 /**
36 * @param {(any) => any} expression
37 * @return {List}
56 * Maps each item in the list using a provided function
57 * @param {(any) => any} expression - Function to apply to each item
58 * @return {List} A new list with the results of applying the function
3859 */
3960 map(expression) {
40 throw new Error('Remove this line and implement the function');
61 const result = [];
62
63 // Edge Case: Mapping over an empty list should return an empty list
64 for (let i = 0; i < this.values.length; i++) {
65 result.push(expression(this.values[i]));
66 }
67
68 return new List(result);
4169 }
4270
4371 /**
44 * @return {number}
72 * Returns the number of items in the list
73 * @return {number} The length of the list
4574 */
4675 length() {
47 throw new Error('Remove this line and implement the function');
76 // Edge Case: Length of an empty list should be 0
77 let count = 0;
78 for (let i = 0; i < this.values.length; i++) {
79 count++;
80 }
81 return count;
4882 }
4983
5084 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
85 * Folds (reduces) the list from the left using a callback function
86 * @param {(acc: any, el: any) => any} callback - Function to apply to accumulator and each element
87 * @param {any} initial - Initial value for the accumulator
88 * @return {any} The final accumulated value
5489 */
5590 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
91 let accumulator = initial;
92
93 // Edge Case: Folding an empty list should return the initial value
94 for (let i = 0; i < this.values.length; i++) {
95 accumulator = callback(accumulator, this.values[i]);
96 }
97
98 return accumulator;
5799 }
58100
59101 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
102 * Folds (reduces) the list from the right using a callback function
103 * @param {(acc: any, el: any) => any} callback - Function to apply to accumulator and each element
104 * @param {any} initial - Initial value for the accumulator
105 * @return {any} The final accumulated value
63106 */
64107 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
108 let accumulator = initial;
109
110 // Edge Case: Folding an empty list should return the initial value
111 for (let i = this.values.length - 1; i >= 0; i--) {
112 accumulator = callback(accumulator, this.values[i]);
113 }
114
115 return accumulator;
66116 }
67117
68118 /**
69 * @return {List}
119 * Reverses the order of items in the list
120 * @return {List} A new list with items in reverse order
70121 */
71122 reverse() {
72 throw new Error('Remove this line and implement the function');
123 const result = [];
124
125 // Edge Case: Reversing an empty list should return an empty list
126 for (let i = this.values.length - 1; i >= 0; i--) {
127 result.push(this.values[i]);
128 }
129
130 return new List(result);
73131 }
132
133 // Handled Edge Cases: Appending an empty list, concatenating with empty lists, filtering empty lists, mapping over empty lists, length of empty list, folding empty lists, reversing empty lists
74134}
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.