awesome

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 // Edge Case: Appending to an empty list should return the other list
22 if (this.length() === 0) {
23 return new List(other.values);
24 }
25
26 return new List([...this.values, ...other.values]);
1727 }
1828
1929 /**
2131 * @return {List}
2232 */
2333 concat(others) {
24 throw new Error('Remove this line and implement the function');
34 // Edge Case: Concatenating with an empty list of lists should return the original list
35 if (others.length() === 0) {
36 return new List(this.values);
37 }
38
39 let result = [...this.values];
40 for (let i = 0; i < others.length(); i++) {
41 result = [...result, ...others.values[i].values];
42 }
43 return new List(result);
2544 }
2645
2746 /**
2948 * @return {List}
3049 */
3150 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
51 // Edge Case: Filtering an empty list should return an empty list
52 if (this.length() === 0) {
53 return new List([]);
54 }
55
56 const result = [];
57 for (let i = 0; i < this.values.length; i++) {
58 if (predicate(this.values[i])) {
59 result.push(this.values[i]);
60 }
61 }
62 return new List(result);
3363 }
3464
3565 /**
3767 * @return {List}
3868 */
3969 map(expression) {
40 throw new Error('Remove this line and implement the function');
70 // Edge Case: Mapping over an empty list should return an empty list
71 if (this.length() === 0) {
72 return new List([]);
73 }
74
75 const result = [];
76 for (let i = 0; i < this.values.length; i++) {
77 result.push(expression(this.values[i]));
78 }
79 return new List(result);
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 should be 0
87 let count = 0;
88 for (let i = 0; i < this.values.length; i++) {
89 count++;
90 }
91 return count;
4892 }
4993
5094 /**
5397 * @return {any}
5498 */
5599 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
100 // Edge Case: Folding an empty list should return the initial value
101 if (this.length() === 0) {
102 return initial;
103 }
104
105 let accumulator = initial;
106 for (let i = 0; i < this.values.length; i++) {
107 accumulator = callback(accumulator, this.values[i]);
108 }
109 return accumulator;
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 should return the initial value
119 if (this.length() === 0) {
120 return initial;
121 }
122
123 let accumulator = initial;
124 for (let i = this.values.length - 1; i >= 0; i--) {
125 accumulator = callback(accumulator, this.values[i]);
126 }
127 return accumulator;
66128 }
67129
68130 /**
69131 * @return {List}
70132 */
71133 reverse() {
72 throw new Error('Remove this line and implement the function');
134 // Edge Case: Reversing an empty list should return an empty list
135 if (this.length() === 0) {
136 return new List([]);
137 }
138
139 const result = [];
140 for (let i = this.values.length - 1; i >= 0; i--) {
141 result.push(this.values[i]);
142 }
143 return new List(result);
73144 }
74145}
146
147// Handled Edge Cases: Appending an empty list, appending to an empty list, concatenating with an empty list of lists, filtering an empty list, mapping over an empty list, length of an empty list, folding an empty list, reversing an empty list
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.