joseph

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 should return 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 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: 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 empty list should be 0
87 let count = 0;
88
89 for (let i = 0; i < this.values.length; i++) {
90 count++;
91 }
92
93 return count;
94 // Handled Edge Cases: Empty list length
4895 }
4996
5097 /**
53100 * @return {any}
54101 */
55102 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
103 // Edge Case: Folding an empty list should return the initial value
104 let accumulator = initial;
105
106 for (let i = 0; i < this.values.length; i++) {
107 accumulator = callback(accumulator, this.values[i]);
108 }
109
110 return accumulator;
111 // Handled Edge Cases: Folding empty list
57112 }
58113
59114 /**
62117 * @return {any}
63118 */
64119 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
120 // Edge Case: Folding an empty list should return the initial value
121 let accumulator = initial;
122
123 for (let i = this.values.length - 1; i >= 0; i--) {
124 accumulator = callback(accumulator, this.values[i]);
125 }
126
127 return accumulator;
128 // Handled Edge Cases: Folding empty list
66129 }
67130
68131 /**
69132 * @return {List}
70133 */
71134 reverse() {
72 throw new Error('Remove this line and implement the function');
135 // Edge Case: Reversing an empty list should return an empty list
136 const result = [];
137
138 for (let i = this.values.length - 1; i >= 0; i--) {
139 result.push(this.values[i]);
140 }
141
142 return new List(result);
143 // Handled Edge Cases: Reversing empty list
73144 }
74145}
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.