jackson

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);
29 // Handled Edge Cases: Appending empty list
1730 }
1831
1932 /**
2134 * @return {List}
2235 */
2336 concat(others) {
24 throw new Error('Remove this line and implement the function');
37 // Edge Case: Concatenating with an empty list of lists
38 let result = new List(this.values);
39
40 for (let i = 0; i < others.values.length; i++) {
41 result = result.append(others.values[i]);
42 }
43
44 return result;
45 // Handled Edge Cases: Concatenating with empty 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: Filtering 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);
65 // Handled Edge Cases: Filtering empty list, predicate matching none, predicate matching all
3366 }
3467
3568 /**
3770 * @return {List}
3871 */
3972 map(expression) {
40 throw new Error('Remove this line and implement the function');
73 // Edge Case: Mapping over an empty list
74 if (this.length() === 0) {
75 return new List([]);
76 }
77
78 const result = [];
79 for (let i = 0; i < this.values.length; i++) {
80 result.push(expression(this.values[i]));
81 }
82 return new List(result);
83 // Handled Edge Cases: Mapping over empty list
4184 }
4285
4386 /**
4487 * @return {number}
4588 */
4689 length() {
47 throw new Error('Remove this line and implement the function');
90 // Edge Case: Length of empty list
91 let count = 0;
92 for (let i = 0; i < this.values.length; i++) {
93 count++;
94 }
95 return count;
96 // Handled Edge Cases: Empty list
4897 }
4998
5099 /**
53102 * @return {any}
54103 */
55104 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
105 // Edge Case: Folding an empty list
106 if (this.length() === 0) {
107 return initial;
108 }
109
110 let accumulator = initial;
111 for (let i = 0; i < this.values.length; i++) {
112 accumulator = callback(accumulator, this.values[i]);
113 }
114 return accumulator;
115 // Handled Edge Cases: Folding empty list
57116 }
58117
59118 /**
62121 * @return {any}
63122 */
64123 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
124 // Edge Case: Folding an empty list
125 if (this.length() === 0) {
126 return initial;
127 }
128
129 let accumulator = initial;
130 for (let i = this.values.length - 1; i >= 0; i--) {
131 accumulator = callback(accumulator, this.values[i]);
132 }
133 return accumulator;
134 // Handled Edge Cases: Folding empty list
66135 }
67136
68137 /**
69138 * @return {List}
70139 */
71140 reverse() {
72 throw new Error('Remove this line and implement the function');
141 // Edge Case: Reversing an empty list
142 if (this.length() === 0) {
143 return new List([]);
144 }
145
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: Reversing 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.