hope

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: Empty other list
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 // Edge Case: Handle when nested lists are empty
33 let result = [...this.values];
34 for (let i = 0; i < others.length(); i++) {
35 const item = others.values[i];
36 if (item instanceof List) {
37 for (let j = 0; j < item.length(); j++) {
38 result.push(item.values[j]);
39 }
40 } else {
41 result.push(item);
42 }
43 }
44 return new List(result);
45 // Handled Edge Cases: Empty others list, empty nested 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: Handle when predicate is not a function
54 // Edge Case: Handle when list is empty
55 if (typeof predicate !== 'function') {
56 throw new Error('Predicate must be a function');
57 }
58
59 const result = [];
60 for (let i = 0; i < this.values.length; i++) {
61 if (predicate(this.values[i])) {
62 result.push(this.values[i]);
63 }
64 }
65 return new List(result);
66 // Handled Edge Cases: Non-function predicate, empty list
3367 }
3468
3569 /**
3771 * @return {List}
3872 */
3973 map(expression) {
40 throw new Error('Remove this line and implement the function');
74 // Edge Case: Handle when expression is not a function
75 // Edge Case: Handle when list is empty
76 if (typeof expression !== 'function') {
77 throw new Error('Expression must be a function');
78 }
79
80 const result = [];
81 for (let i = 0; i < this.values.length; i++) {
82 result.push(expression(this.values[i]));
83 }
84 return new List(result);
85 // Handled Edge Cases: Non-function expression, empty list
4186 }
4287
4388 /**
4489 * @return {number}
4590 */
4691 length() {
47 throw new Error('Remove this line and implement the function');
92 // Edge Case: Handle when list is empty
93 let count = 0;
94 for (let i = 0; i < this.values.length; i++) {
95 count++;
96 }
97 return count;
98 // Handled Edge Cases: Empty list
4899 }
49100
50101 /**
53104 * @return {any}
54105 */
55106 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
107 // Edge Case: Handle when callback is not a function
108 // Edge Case: Handle when list is empty
109 if (typeof callback !== 'function') {
110 throw new Error('Callback must be a function');
111 }
112
113 let accumulator = initial;
114 for (let i = 0; i < this.values.length; i++) {
115 accumulator = callback(accumulator, this.values[i]);
116 }
117 return accumulator;
118 // Handled Edge Cases: Non-function callback, empty list
57119 }
58120
59121 /**
62124 * @return {any}
63125 */
64126 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
127 // Edge Case: Handle when callback is not a function
128 // Edge Case: Handle when list is empty
129 if (typeof callback !== 'function') {
130 throw new Error('Callback must be a function');
131 }
132
133 let accumulator = initial;
134 for (let i = this.values.length - 1; i >= 0; i--) {
135 accumulator = callback(accumulator, this.values[i]);
136 }
137 return accumulator;
138 // Handled Edge Cases: Non-function callback, empty list
66139 }
67140
68141 /**
69142 * @return {List}
70143 */
71144 reverse() {
72 throw new Error('Remove this line and implement the function');
145 // Edge Case: Handle when list is empty
146 const result = [];
147 for (let i = this.values.length - 1; i >= 0; i--) {
148 result.push(this.values[i]);
149 }
150 return new List(result);
151 // Handled Edge Cases: Empty list
73152 }
74153}
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.