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 = [];
19 for (let i = 0; i < this.values.length; i++) {
20 result.push(this.values[i]);
21 }
22 for (let i = 0; i < other.values.length; i++) {
23 result.push(other.values[i]);
24 }
25 return new List(result);
26 // Handled Edge Cases: Appending empty lists
1727 }
1828
1929 /**
20 * @param {List} others
21 * @return {List}
30 * Concatenates multiple lists into one
31 * @param {List} others - A list of lists to concatenate
32 * @return {List} A new list with all elements flattened
2233 */
2334 concat(others) {
24 throw new Error('Remove this line and implement the function');
35 let result = [];
36
37 // Add elements from current list
38 for (let i = 0; i < this.values.length; i++) {
39 result.push(this.values[i]);
40 }
41
42 // Add elements from each list in others
43 for (let i = 0; i < others.values.length; i++) {
44 const list = others.values[i];
45 for (let j = 0; j < list.values.length; j++) {
46 result.push(list.values[j]);
47 }
48 }
49
50 return new List(result);
51 // Handled Edge Cases: Concatenating with empty lists
2552 }
2653
2754 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
55 * Filters the list based on a predicate function
56 * @param {(any) => boolean} predicate - Function to test each element
57 * @return {List} A new list with elements that pass the predicate test
3058 */
3159 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
60 const result = [];
61
62 // Edge Case: Empty list should return empty list
63 for (let i = 0; i < this.values.length; i++) {
64 if (predicate(this.values[i])) {
65 result.push(this.values[i]);
66 }
67 }
68
69 return new List(result);
70 // Handled Edge Cases: Empty list, all elements match, no elements match
3371 }
3472
3573 /**
36 * @param {(any) => any} expression
37 * @return {List}
74 * Maps each element using a provided function
75 * @param {(any) => any} expression - Function to transform each element
76 * @return {List} A new list with transformed elements
3877 */
3978 map(expression) {
40 throw new Error('Remove this line and implement the function');
79 const result = [];
80
81 // Edge Case: Empty list should return empty list
82 for (let i = 0; i < this.values.length; i++) {
83 result.push(expression(this.values[i]));
84 }
85
86 return new List(result);
87 // Handled Edge Cases: Empty list
4188 }
4289
4390 /**
44 * @return {number}
91 * Returns the length of the list
92 * @return {number} The number of elements in the list
4593 */
4694 length() {
47 throw new Error('Remove this line and implement the function');
95 // Edge Case: Empty list should return 0
96 let count = 0;
97 for (let i = 0; i < this.values.length; i++) {
98 count++;
99 }
100 return count;
101 // Handled Edge Cases: Empty list
48102 }
49103
50104 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
105 * Folds the list from the left using a callback function
106 * @param {(acc: any, el: any) => any} callback - Function to apply to accumulator and element
107 * @param {any} initial - Initial value for the accumulator
108 * @return {any} The final accumulated value
54109 */
55110 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
111 let accumulator = initial;
112
113 // Edge Case: Empty list should return initial value
114 for (let i = 0; i < this.values.length; i++) {
115 accumulator = callback(accumulator, this.values[i]);
116 }
117
118 return accumulator;
119 // Handled Edge Cases: Empty list
57120 }
58121
59122 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
123 * Folds the list from the right using a callback function
124 * @param {(acc: any, el: any) => any} callback - Function to apply to accumulator and element
125 * @param {any} initial - Initial value for the accumulator
126 * @return {any} The final accumulated value
63127 */
64128 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
129 let accumulator = initial;
130
131 // Edge Case: Empty list should return initial value
132 for (let i = this.values.length - 1; i >= 0; i--) {
133 accumulator = callback(accumulator, this.values[i]);
134 }
135
136 return accumulator;
137 // Handled Edge Cases: Empty list
66138 }
67139
68140 /**
69 * @return {List}
141 * Reverses the list
142 * @return {List} A new list with elements in reverse order
70143 */
71144 reverse() {
72 throw new Error('Remove this line and implement the function');
145 const result = [];
146
147 // Edge Case: Empty list should return empty list
148 for (let i = this.values.length - 1; i >= 0; i--) {
149 result.push(this.values[i]);
150 }
151
152 return new List(result);
153 // Handled Edge Cases: Empty list, single element list
73154 }
74155}
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.