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 another list to this list
13 * @param {List} other - The list to append
14 * @return {List} A new list containing elements 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 const result = [...this.values];
19 for (let i = 0; i < other.values.length; i++) {
20 result.push(other.values[i]);
21 }
22 return new List(result);
23 // Handled Edge Cases: Appending empty lists
1724 }
1825
1926 /**
20 * @param {List} others
21 * @return {List}
27 * Concatenates multiple lists into one
28 * @param {List} others - A list of lists to concatenate
29 * @return {List} A new list with all elements flattened
2230 */
2331 concat(others) {
24 throw new Error('Remove this line and implement the function');
32 // Edge Case: Concatenating with an empty list of lists
33 let result = [...this.values];
34
35 for (let i = 0; i < others.values.length; i++) {
36 const list = others.values[i];
37 for (let j = 0; j < list.values.length; j++) {
38 result.push(list.values[j]);
39 }
40 }
41
42 return new List(result);
43 // Handled Edge Cases: Empty lists, nested empty lists
2544 }
2645
2746 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
47 * Filters the list based on a predicate function
48 * @param {(any) => boolean} predicate - Function to test each element
49 * @return {List} A new list with elements that pass the predicate test
3050 */
3151 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
52 // Edge Case: Filtering an empty list should return an empty list
53 const result = [];
54
55 for (let i = 0; i < this.values.length; i++) {
56 if (predicate(this.values[i])) {
57 result.push(this.values[i]);
58 }
59 }
60
61 return new List(result);
62 // Handled Edge Cases: Empty list, all elements match, no elements match
3363 }
3464
3565 /**
36 * @param {(any) => any} expression
37 * @return {List}
66 * Transforms each element using the provided function
67 * @param {(any) => any} expression - Function to transform each element
68 * @return {List} A new list with transformed elements
3869 */
3970 map(expression) {
40 throw new Error('Remove this line and implement the function');
71 // Edge Case: Mapping over an empty list should return an empty list
72 const result = [];
73
74 for (let i = 0; i < this.values.length; i++) {
75 result.push(expression(this.values[i]));
76 }
77
78 return new List(result);
79 // Handled Edge Cases: Empty list
4180 }
4281
4382 /**
44 * @return {number}
83 * Returns the length of the list
84 * @return {number} The number of elements in the list
4585 */
4686 length() {
47 throw new Error('Remove this line and implement the function');
87 // Edge Case: Length of an empty list should be 0
88 let count = 0;
89
90 for (let i = 0; i < this.values.length; i++) {
91 count++;
92 }
93
94 return count;
95 // Handled Edge Cases: Empty list
4896 }
4997
5098 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
99 * Folds the list from the left using a callback function
100 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
101 * @param {any} initial - Initial accumulator value
102 * @return {any} The final accumulator value
54103 */
55104 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
105 // Edge Case: Folding an empty list should return the initial value
106 let accumulator = initial;
107
108 for (let i = 0; i < this.values.length; i++) {
109 accumulator = callback(accumulator, this.values[i]);
110 }
111
112 return accumulator;
113 // Handled Edge Cases: Empty list
57114 }
58115
59116 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
117 * Folds the list from the right using a callback function
118 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
119 * @param {any} initial - Initial accumulator value
120 * @return {any} The final accumulator value
63121 */
64122 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
123 // Edge Case: Folding an empty list should return the initial value
124 let accumulator = initial;
125
126 for (let i = this.values.length - 1; i >= 0; i--) {
127 accumulator = callback(accumulator, this.values[i]);
128 }
129
130 return accumulator;
131 // Handled Edge Cases: Empty list
66132 }
67133
68134 /**
69 * @return {List}
135 * Reverses the order of elements in the list
136 * @return {List} A new list with elements in reverse order
70137 */
71138 reverse() {
72 throw new Error('Remove this line and implement the function');
139 // Edge Case: Reversing an empty list should return an empty list
140 const result = [];
141
142 for (let i = this.values.length - 1; i >= 0; i--) {
143 result.push(this.values[i]);
144 }
145
146 return new List(result);
147 // Handled Edge Cases: Empty list, single element list
73148 }
74149}
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.