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);
1729 }
1830
1931 /**
2133 * @return {List}
2234 */
2335 concat(others) {
24 throw new Error('Remove this line and implement the function');
36 // Edge Case: Concatenating with an empty list of lists
37 let result = new List(this.values);
38
39 for (let i = 0; i < others.values.length; i++) {
40 result = result.append(others.values[i]);
41 }
42
43 return 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 const result = [];
53
54 for (let i = 0; i < this.values.length; i++) {
55 if (predicate(this.values[i])) {
56 result.push(this.values[i]);
57 }
58 }
59
60 return new List(result);
3361 }
3462
3563 /**
3765 * @return {List}
3866 */
3967 map(expression) {
40 throw new Error('Remove this line and implement the function');
68 // Edge Case: Mapping over an empty list should return an empty list
69 const result = [];
70
71 for (let i = 0; i < this.values.length; i++) {
72 result.push(expression(this.values[i]));
73 }
74
75 return new List(result);
4176 }
4277
4378 /**
4479 * @return {number}
4580 */
4681 length() {
47 throw new Error('Remove this line and implement the function');
82 // Edge Case: Length of an empty list should be 0
83 let count = 0;
84
85 for (let i = 0; i < this.values.length; i++) {
86 count++;
87 }
88
89 return count;
4890 }
4991
5092 /**
5395 * @return {any}
5496 */
5597 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
98 // Edge Case: Folding an empty list should return the initial value
99 let accumulator = initial;
100
101 for (let i = 0; i < this.values.length; i++) {
102 accumulator = callback(accumulator, this.values[i]);
103 }
104
105 return accumulator;
57106 }
58107
59108 /**
62111 * @return {any}
63112 */
64113 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
114 // Edge Case: Folding an empty list should return the initial value
115 let accumulator = initial;
116
117 for (let i = this.values.length - 1; i >= 0; i--) {
118 accumulator = callback(accumulator, this.values[i]);
119 }
120
121 return accumulator;
66122 }
67123
68124 /**
69125 * @return {List}
70126 */
71127 reverse() {
72 throw new Error('Remove this line and implement the function');
128 // Edge Case: Reversing an empty list should return an empty list
129 const result = [];
130
131 for (let i = this.values.length - 1; i >= 0; i--) {
132 result.push(this.values[i]);
133 }
134
135 return new List(result);
73136 }
137
138 // Handled Edge Cases: Appending an empty list, concatenating with empty lists, filtering empty lists, mapping over empty lists, length of empty list, folding empty lists, reversing empty lists
74139}
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.