kale

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 values is not an array by defaulting to empty array
9 this.values = Array.isArray(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 is not a List instance
18 if (!(other instanceof List)) {
19 throw new Error('Argument must be a List instance');
20 }
21
22 const result = [...this.values];
23 for (let i = 0; i < other.length(); i++) {
24 result.push(other.values[i]);
25 }
26 return new List(result);
1727 }
1828
1929 /**
2131 * @return {List}
2232 */
2333 concat(others) {
24 throw new Error('Remove this line and implement the function');
34 // Edge Case: Handle when others is not a List instance
35 if (!(others instanceof List)) {
36 throw new Error('Argument must be a List instance');
37 }
38
39 const result = [...this.values];
40 for (let i = 0; i < others.length(); i++) {
41 const item = others.values[i];
42 if (item instanceof List) {
43 // Edge Case: Handle nested lists by flattening them
44 for (let j = 0; j < item.length(); j++) {
45 result.push(item.values[j]);
46 }
47 } else {
48 result.push(item);
49 }
50 }
51 return new List(result);
2552 }
2653
2754 /**
2956 * @return {List}
3057 */
3158 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
59 // Edge Case: Handle when predicate is not a function
60 if (typeof predicate !== 'function') {
61 throw new Error('Predicate must be a function');
62 }
63
64 const result = [];
65 for (let i = 0; i < this.values.length; i++) {
66 if (predicate(this.values[i])) {
67 result.push(this.values[i]);
68 }
69 }
70 return new List(result);
3371 }
3472
3573 /**
3775 * @return {List}
3876 */
3977 map(expression) {
40 throw new Error('Remove this line and implement the function');
78 // Edge Case: Handle when expression is not a function
79 if (typeof expression !== 'function') {
80 throw new Error('Expression must be a function');
81 }
82
83 const result = [];
84 for (let i = 0; i < this.values.length; i++) {
85 result.push(expression(this.values[i]));
86 }
87 return new List(result);
4188 }
4289
4390 /**
4491 * @return {number}
4592 */
4693 length() {
47 throw new Error('Remove this line and implement the function');
94 let count = 0;
95 // Edge Case: Handle empty list
96 for (let i = 0; i < this.values.length; i++) {
97 count++;
98 }
99 return count;
48100 }
49101
50102 /**
53105 * @return {any}
54106 */
55107 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
108 // Edge Case: Handle when callback is not a function
109 if (typeof callback !== 'function') {
110 throw new Error('Callback must be a function');
111 }
112
113 let accumulator = initial;
114 // Edge Case: Handle empty list
115 for (let i = 0; i < this.values.length; i++) {
116 accumulator = callback(accumulator, this.values[i]);
117 }
118 return accumulator;
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 if (typeof callback !== 'function') {
129 throw new Error('Callback must be a function');
130 }
131
132 let accumulator = initial;
133 // Edge Case: Handle empty list
134 for (let i = this.values.length - 1; i >= 0; i--) {
135 accumulator = callback(accumulator, this.values[i]);
136 }
137 return accumulator;
66138 }
67139
68140 /**
69141 * @return {List}
70142 */
71143 reverse() {
72 throw new Error('Remove this line and implement the function');
144 const result = [];
145 // Edge Case: Handle empty list
146 for (let i = this.values.length - 1; i >= 0; i--) {
147 result.push(this.values[i]);
148 }
149 return new List(result);
73150 }
74151}
152
153// Handled Edge Cases:
154// - Handle when values is not an array by defaulting to empty array
155// - Handle when other is not a List instance
156// - Handle when others is not a List instance
157// - Handle nested lists by flattening them
158// - Handle when predicate is not a function
159// - Handle empty list
160// - Handle when expression is not a function
161// - Handle when callback is not a function
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.