hope

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 should return the original list
37 if (others.length() === 0) {
38 return new List(this.values);
39 }
40
41 let result = new List(this.values);
42 for (let i = 0; i < others.values.length; i++) {
43 result = result.append(others.values[i]);
44 }
45 return result;
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 if (this.length() === 0) {
55 return new List([]);
56 }
57
58 const result = [];
59 for (let i = 0; i < this.values.length; i++) {
60 if (predicate(this.values[i])) {
61 result.push(this.values[i]);
62 }
63 }
64 return new List(result);
3365 }
3466
3567 /**
3769 * @return {List}
3870 */
3971 map(expression) {
40 throw new Error('Remove this line and implement the function');
72 // Edge Case: Mapping over an empty list should return an empty list
73 if (this.length() === 0) {
74 return new List([]);
75 }
76
77 const result = [];
78 for (let i = 0; i < this.values.length; i++) {
79 result.push(expression(this.values[i]));
80 }
81 return new List(result);
4182 }
4283
4384 /**
4485 * @return {number}
4586 */
4687 length() {
47 throw new Error('Remove this line and implement the function');
88 let count = 0;
89 for (let i = 0; i < this.values.length; i++) {
90 count++;
91 }
92 return count;
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 should return the initial value
102 if (this.length() === 0) {
103 return initial;
104 }
105
106 let accumulator = initial;
107 for (let i = 0; i < this.values.length; i++) {
108 accumulator = callback(accumulator, this.values[i]);
109 }
110 return accumulator;
57111 }
58112
59113 /**
62116 * @return {any}
63117 */
64118 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
119 // Edge Case: Folding an empty list should return the initial value
120 if (this.length() === 0) {
121 return initial;
122 }
123
124 let accumulator = initial;
125 for (let i = this.values.length - 1; i >= 0; i--) {
126 accumulator = callback(accumulator, this.values[i]);
127 }
128 return accumulator;
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 if (this.length() === 0) {
137 return new List([]);
138 }
139
140 const result = [];
141 for (let i = this.values.length - 1; i >= 0; i--) {
142 result.push(this.values[i]);
143 }
144 return new List(result);
73145 }
146
147 // Handled Edge Cases: Appending empty list, concatenating empty list, filtering empty list, mapping over empty list, folding empty list, reversing empty list
74148}
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.