tobe

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 /**
1313 * @return {List}
1414 */
1515 append(other) {
16 throw new Error('Remove this line and implement the function');
16 // Edge Case: Appending an empty list should return the original list
17 if (other.length() === 0) {
18 return new List(this.values);
19 }
20
21 const result = [];
22 for (let i = 0; i < this.values.length; i++) {
23 result.push(this.values[i]);
24 }
25 for (let i = 0; i < other.values.length; i++) {
26 result.push(other.values[i]);
27 }
28 return new List(result);
29 // Handled Edge Cases: Appending empty list
1730 }
1831
1932 /**
2134 * @return {List}
2235 */
2336 concat(others) {
24 throw new Error('Remove this line and implement the function');
37 // Edge Case: Concatenating with an empty list of lists
38 let result = new List(this.values);
39
40 for (let i = 0; i < others.values.length; i++) {
41 result = result.append(others.values[i]);
42 }
43
44 return result;
45 // Handled Edge Cases: Concatenating with empty lists
2546 }
2647
2748 /**
2950 * @return {List}
3051 */
3152 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
53 // Edge Case: Filtering an empty list
54 const result = [];
55
56 for (let i = 0; i < this.values.length; i++) {
57 if (predicate(this.values[i])) {
58 result.push(this.values[i]);
59 }
60 }
61
62 return new List(result);
63 // Handled Edge Cases: Filtering empty list
3364 }
3465
3566 /**
3768 * @return {List}
3869 */
3970 map(expression) {
40 throw new Error('Remove this line and implement the function');
71 // Edge Case: Mapping over 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: Mapping over empty list
4180 }
4281
4382 /**
4483 * @return {number}
4584 */
4685 length() {
47 throw new Error('Remove this line and implement the function');
86 // Edge Case: Length of an empty list
87 let count = 0;
88 for (let i = 0; i < this.values.length; i++) {
89 count++;
90 }
91 return count;
92 // Handled Edge Cases: Empty list length
4893 }
4994
5095 /**
5398 * @return {any}
5499 */
55100 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
101 // Edge Case: Folding an empty list
102 let accumulator = initial;
103
104 for (let i = 0; i < this.values.length; i++) {
105 accumulator = callback(accumulator, this.values[i]);
106 }
107
108 return accumulator;
109 // Handled Edge Cases: Folding empty list
57110 }
58111
59112 /**
62115 * @return {any}
63116 */
64117 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
118 // Edge Case: Folding an empty list from the right
119 let accumulator = initial;
120
121 for (let i = this.values.length - 1; i >= 0; i--) {
122 accumulator = callback(accumulator, this.values[i]);
123 }
124
125 return accumulator;
126 // Handled Edge Cases: Folding empty list from right
66127 }
67128
68129 /**
69130 * @return {List}
70131 */
71132 reverse() {
72 throw new Error('Remove this line and implement the function');
133 // Edge Case: Reversing an empty list
134 const result = [];
135
136 for (let i = this.values.length - 1; i >= 0; i--) {
137 result.push(this.values[i]);
138 }
139
140 return new List(result);
141 // Handled Edge Cases: Reversing empty list
73142 }
74143}
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.