Finished
44//
55
66export class List {
7 constructor() {
8 throw new Error('Remove this line and implement the function');
7 constructor(values = []) {
8 // Edge Case: Handle when no values are provided, default to empty array
9 this.values = values;
910 }
1011
1112 /**
1314 * @return {List}
1415 */
1516 append(other) {
16 throw new Error('Remove this line and implement the function');
17 // Edge Case: Handle when other list is empty
18 const result = [...this.values];
19 for (let i = 0; i < other.length(); i++) {
20 result.push(other.values[i]);
21 }
22 return new List(result);
23 // Handled Edge Cases: other list being empty
1724 }
1825
1926 /**
2128 * @return {List}
2229 */
2330 concat(others) {
24 throw new Error('Remove this line and implement the function');
31 // Edge Case: Handle when others list is empty
32 let result = [...this.values];
33 for (let i = 0; i < others.length(); i++) {
34 const list = others.values[i];
35 if (list instanceof List) {
36 result = result.concat(list.values);
37 } else {
38 result.push(list);
39 }
40 }
41 return new List(result);
42 // Handled Edge Cases: others list being empty
2543 }
2644
2745 /**
2947 * @return {List}
3048 */
3149 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
50 // Edge Case: Handle when predicate is not a function
51 if (typeof predicate !== 'function') {
52 throw new Error('Predicate must be a function');
53 }
54
55 const result = [];
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 return new List(result);
62 // Handled Edge Cases: predicate not being a function
3363 }
3464
3565 /**
3767 * @return {List}
3868 */
3969 map(expression) {
40 throw new Error('Remove this line and implement the function');
70 // Edge Case: Handle when expression is not a function
71 if (typeof expression !== 'function') {
72 throw new Error('Expression must be a function');
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);
80 // Handled Edge Cases: expression not being a function
4181 }
4282
4383 /**
4484 * @return {number}
4585 */
4686 length() {
47 throw new Error('Remove this line and implement the function');
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
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: Handle when callback is not a function
102 if (typeof callback !== 'function') {
103 throw new Error('Callback must be a function');
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;
111 // Handled Edge Cases: callback not being a function
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: Handle when callback is not a function
121 if (typeof callback !== 'function') {
122 throw new Error('Callback must be a function');
123 }
124
125 let accumulator = initial;
126 for (let i = this.values.length - 1; i >= 0; i--) {
127 accumulator = callback(accumulator, this.values[i]);
128 }
129 return accumulator;
130 // Handled Edge Cases: callback not being a function
66131 }
67132
68133 /**
69134 * @return {List}
70135 */
71136 reverse() {
72 throw new Error('Remove this line and implement the function');
137 const result = [];
138 for (let i = this.values.length - 1; i >= 0; i--) {
139 result.push(this.values[i]);
140 }
141 return new List(result);
142 // Handled Edge Cases: empty list
73143 }
74144}
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.