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 for (let j = 0; j < item.length(); j++) {
44 result.push(item.values[j]);
45 }
46 } else {
47 result.push(item);
48 }
49 }
50 return new List(result);
2551 }
2652
2753 /**
2955 * @return {List}
3056 */
3157 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
58 // Edge Case: Handle when predicate is not a function
59 if (typeof predicate !== 'function') {
60 throw new Error('Predicate must be a function');
61 }
62
63 const result = [];
64 for (let i = 0; i < this.values.length; i++) {
65 if (predicate(this.values[i])) {
66 result.push(this.values[i]);
67 }
68 }
69 return new List(result);
3370 }
3471
3572 /**
3774 * @return {List}
3875 */
3976 map(expression) {
40 throw new Error('Remove this line and implement the function');
77 // Edge Case: Handle when expression is not a function
78 if (typeof expression !== 'function') {
79 throw new Error('Expression must be a function');
80 }
81
82 const result = [];
83 for (let i = 0; i < this.values.length; i++) {
84 result.push(expression(this.values[i]));
85 }
86 return new List(result);
4187 }
4288
4389 /**
4490 * @return {number}
4591 */
4692 length() {
47 throw new Error('Remove this line and implement the function');
93 let count = 0;
94 for (let i = 0; i < this.values.length; i++) {
95 count++;
96 }
97 return count;
4898 }
4999
50100 /**
53103 * @return {any}
54104 */
55105 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
106 // Edge Case: Handle when callback is not a function
107 if (typeof callback !== 'function') {
108 throw new Error('Callback must be a function');
109 }
110
111 let accumulator = initial;
112 for (let i = 0; i < this.values.length; i++) {
113 accumulator = callback(accumulator, this.values[i]);
114 }
115 return accumulator;
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: Handle when callback is not a function
125 if (typeof callback !== 'function') {
126 throw new Error('Callback must be a function');
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;
66134 }
67135
68136 /**
69137 * @return {List}
70138 */
71139 reverse() {
72 throw new Error('Remove this line and implement the function');
140 const result = [];
141 for (let i = this.values.length - 1; i >= 0; i--) {
142 result.push(this.values[i]);
143 }
144 return new List(result);
73145 }
74146}
147
148// Handled Edge Cases:
149// - Empty lists
150// - Lists with single element
151// - Null/undefined inputs
152// - Type mismatches between function and list elements
153// - Non-function callbacks/predicates
154// - Non-List instances passed to append/concat
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.