tobe

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 const result = [];
55 for (let i = 0; i < this.values.length; i++) {
56 if (predicate(this.values[i])) {
57 result.push(this.values[i]);
58 }
59 }
60 return new List(result);
61 // Handled Edge Cases: predicate not being a function
3362 }
3463
3564 /**
3766 * @return {List}
3867 */
3968 map(expression) {
40 throw new Error('Remove this line and implement the function');
69 // Edge Case: Handle when expression is not a function
70 if (typeof expression !== 'function') {
71 throw new Error('Expression must be a function');
72 }
73 const result = [];
74 for (let i = 0; i < this.values.length; i++) {
75 result.push(expression(this.values[i]));
76 }
77 return new List(result);
78 // Handled Edge Cases: expression not being a function
4179 }
4280
4381 /**
4482 * @return {number}
4583 */
4684 length() {
47 throw new Error('Remove this line and implement the function');
85 let count = 0;
86 for (let i = 0; i < this.values.length; i++) {
87 count++;
88 }
89 return count;
90 // Handled Edge Cases: empty list
4891 }
4992
5093 /**
5396 * @return {any}
5497 */
5598 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
99 // Edge Case: Handle when callback is not a function
100 if (typeof callback !== 'function') {
101 throw new Error('Callback must be a function');
102 }
103 let accumulator = initial;
104 for (let i = 0; i < this.values.length; i++) {
105 accumulator = callback(accumulator, this.values[i]);
106 }
107 return accumulator;
108 // Handled Edge Cases: callback not being a function
57109 }
58110
59111 /**
62114 * @return {any}
63115 */
64116 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
117 // Edge Case: Handle when callback is not a function
118 if (typeof callback !== 'function') {
119 throw new Error('Callback must be a function');
120 }
121 let accumulator = initial;
122 for (let i = this.values.length - 1; i >= 0; i--) {
123 accumulator = callback(accumulator, this.values[i]);
124 }
125 return accumulator;
126 // Handled Edge Cases: callback not being a function
66127 }
67128
68129 /**
69130 * @return {List}
70131 */
71132 reverse() {
72 throw new Error('Remove this line and implement the function');
133 const result = [];
134 for (let i = this.values.length - 1; i >= 0; i--) {
135 result.push(this.values[i]);
136 }
137 return new List(result);
138 // Handled Edge Cases: empty list
73139 }
74140}
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.