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
17 if (other.length() === 0) {
18 return new List(this.values);
19 }
20
21 // Edge Case: Appending to an empty list
22 if (this.length() === 0) {
23 return new List(other.values);
24 }
25
26 // Manual implementation without spread operator
27 const result = [];
28 for (const item of this.values) {
29 result.push(item);
30 }
31 for (const item of other.values) {
32 result.push(item);
33 }
34 return new List(result);
1735 }
1836
1937 /**
2139 * @return {List}
2240 */
2341 concat(others) {
24 throw new Error('Remove this line and implement the function');
42 // Start with current list values
43 const result = [];
44 for (const item of this.values) {
45 result.push(item);
46 }
47
48 // Edge Case: Concatenating with an empty list
49 if (others.length() === 0) {
50 return new List(result);
51 }
52
53 // Process each item in the others list
54 for (const item of others.values) {
55 if (item instanceof List) {
56 // If item is a List, add all its elements
57 for (const subItem of item.values) {
58 result.push(subItem);
59 }
60 } else {
61 // If item is not a List, add it directly
62 result.push(item);
63 }
64 }
65
66 return new List(result);
2567 }
2668
2769 /**
2971 * @return {List}
3072 */
3173 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
74 const result = [];
75
76 // Edge Case: Filtering an empty list
77 if (this.length() === 0) {
78 return new List(result);
79 }
80
81 for (const item of this.values) {
82 if (predicate(item)) {
83 result.push(item);
84 }
85 }
86
87 return new List(result);
3388 }
3489
3590 /**
3792 * @return {List}
3893 */
3994 map(expression) {
40 throw new Error('Remove this line and implement the function');
95 const result = [];
96
97 // Edge Case: Mapping over an empty list
98 if (this.length() === 0) {
99 return new List(result);
100 }
101
102 for (const item of this.values) {
103 result.push(expression(item));
104 }
105
106 return new List(result);
41107 }
42108
43109 /**
44110 * @return {number}
45111 */
46112 length() {
47 throw new Error('Remove this line and implement the function');
113 // Edge Case: Length of an empty list
114 if (!this.values) {
115 return 0;
116 }
117
118 let count = 0;
119 for (const _ of this.values) {
120 count++;
121 }
122 return count;
48123 }
49124
50125 /**
53128 * @return {any}
54129 */
55130 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
131 let accumulator = initial;
132
133 // Edge Case: Folding an empty list
134 if (this.length() === 0) {
135 return accumulator;
136 }
137
138 for (const item of this.values) {
139 accumulator = callback(accumulator, item);
140 }
141
142 return accumulator;
57143 }
58144
59145 /**
60 * @param {(acc: any, el: any) => any} callback
146 * @param {(el: any, acc: any) => any} callback
61147 * @param {any} initial
62148 * @return {any}
63149 */
64150 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
151 let accumulator = initial;
152
153 // Edge Case: Folding an empty list from the right
154 if (this.length() === 0) {
155 return accumulator;
156 }
157
158 // Process from right to left
159 // Note: callback is called as callback(item, accumulator)
160 for (let i = this.values.length - 1; i >= 0; i--) {
161 accumulator = callback(this.values[i], accumulator);
162 }
163
164 return accumulator;
66165 }
67166
68167 /**
69168 * @return {List}
70169 */
71170 reverse() {
72 throw new Error('Remove this line and implement the function');
171 const result = [];
172
173 // Edge Case: Reversing an empty list
174 if (this.length() === 0) {
175 return new List(result);
176 }
177
178 // Edge Case: Reversing a single element list
179 if (this.length() === 1) {
180 return new List([this.values[0]]);
181 }
182
183 for (let i = this.values.length - 1; i >= 0; i--) {
184 result.push(this.values[i]);
185 }
186
187 return new List(result);
73188 }
74189}
190
191// Handled Edge Cases: Appending an empty list, appending to an empty list, concatenating with an empty list, concatenating an empty list with others, filtering an empty list, mapping over an empty list, length of an empty list, folding an empty list, folding an empty list from the right, reversing an empty list, reversing a single element list
Test NameStatus
direction dependent function applied to non-empty list
Fail
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
empty list
Pass
non-empty list
Pass
list of lists is not flattened
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.