sourdough

Finished
44//
55
66export class List {
7 constructor() {
8 throw new Error('Remove this line and implement the function');
7 /**
8 * Creates a new List
9 * @param {Array} [values=[]] - Initial values for the list
10 */
11 constructor(values = []) {
12 // Edge Case: Handle when values is not an array by defaulting to empty array
13 this.values = Array.isArray(values) ? values : [];
914 }
1015
1116 /**
12 * @param {List} other
13 * @return {List}
17 * Appends all items from another list to the end of this list
18 * @param {List} other - The list to append
19 * @return {List} A new List containing all items from both lists
1420 */
1521 append(other) {
16 throw new Error('Remove this line and implement the function');
22 // Edge Case: Handle when other is not a List instance
23 if (!(other instanceof List)) {
24 throw new Error('Argument must be a List instance');
25 }
26
27 return new List(this.values.concat(other.values));
1728 }
1829
1930 /**
20 * @param {List} others
21 * @return {List}
31 * Concatenates a series of lists into one flattened list
32 * @param {List} others - A list containing other lists to concatenate
33 * @return {List} A new List with all items from all lists flattened
2234 */
2335 concat(others) {
24 throw new Error('Remove this line and implement the function');
36 // Edge Case: Handle when others is not a List instance
37 if (!(others instanceof List)) {
38 throw new Error('Argument must be a List instance');
39 }
40
41 let result = this.values.slice(); // Copy current values
42
43 // Edge Case: Handle when others contains non-List elements
44 for (const item of others.values) {
45 if (item instanceof List) {
46 result = result.concat(item.values);
47 } else {
48 result.push(item);
49 }
50 }
51
52 return new List(result);
2553 }
2654
2755 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
56 * Filters the list based on a predicate function
57 * @param {(any) => boolean} predicate - Function to test each element
58 * @return {List} A new List containing only elements that satisfy the predicate
3059 */
3160 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
61 // Edge Case: Handle when predicate is not a function
62 if (typeof predicate !== 'function') {
63 throw new Error('Predicate must be a function');
64 }
65
66 const result = [];
67
68 // Edge Case: Handle empty list
69 if (this.values.length === 0) {
70 return new List([]);
71 }
72
73 for (const item of this.values) {
74 if (predicate(item)) {
75 result.push(item);
76 }
77 }
78
79 return new List(result);
3380 }
3481
3582 /**
36 * @param {(any) => any} expression
37 * @return {List}
83 * Maps each element in the list using the provided expression function
84 * @param {(any) => any} expression - Function to transform each element
85 * @return {List} A new List containing transformed elements
3886 */
3987 map(expression) {
40 throw new Error('Remove this line and implement the function');
88 // Edge Case: Handle when expression is not a function
89 if (typeof expression !== 'function') {
90 throw new Error('Expression must be a function');
91 }
92
93 const result = [];
94
95 // Edge Case: Handle empty list
96 if (this.values.length === 0) {
97 return new List([]);
98 }
99
100 for (const item of this.values) {
101 result.push(expression(item));
102 }
103
104 return new List(result);
41105 }
42106
43107 /**
44 * @return {number}
108 * Returns the length of the list
109 * @return {number} The number of items in the list
45110 */
46111 length() {
47 throw new Error('Remove this line and implement the function');
112 // Edge Case: Handle when values is not properly initialized
113 if (!Array.isArray(this.values)) {
114 return 0;
115 }
116
117 let count = 0;
118 for (const _ of this.values) {
119 count++;
120 }
121 return count;
48122 }
49123
50124 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
125 * Folds (reduces) the list from the left using a callback function
126 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
127 * @param {any} initial - Initial accumulator value
128 * @return {any} The final accumulator value
54129 */
55130 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
131 // Edge Case: Handle when callback is not a function
132 if (typeof callback !== 'function') {
133 throw new Error('Callback must be a function');
134 }
135
136 let accumulator = initial;
137
138 // Edge Case: Handle empty list
139 if (this.values.length === 0) {
140 return accumulator;
141 }
142
143 for (const item of this.values) {
144 accumulator = callback(accumulator, item);
145 }
146
147 return accumulator;
57148 }
58149
59150 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
151 * Folds (reduces) the list from the right using a callback function
152 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
153 * @param {any} initial - Initial accumulator value
154 * @return {any} The final accumulator value
63155 */
64156 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
157 // Edge Case: Handle when callback is not a function
158 if (typeof callback !== 'function') {
159 throw new Error('Callback must be a function');
160 }
161
162 let accumulator = initial;
163
164 // Edge Case: Handle empty list
165 if (this.values.length === 0) {
166 return accumulator;
167 }
168
169 // Process from right to left
170 for (let i = this.values.length - 1; i >= 0; i--) {
171 accumulator = callback(accumulator, this.values[i]);
172 }
173
174 return accumulator;
66175 }
67176
68177 /**
69 * @return {List}
178 * Returns a new list with elements in reversed order
179 * @return {List} A new List with elements in reversed order
70180 */
71181 reverse() {
72 throw new Error('Remove this line and implement the function');
182 const result = [];
183
184 // Edge Case: Handle empty list
185 if (this.values.length === 0) {
186 return new List([]);
187 }
188
189 // Build reversed array
190 for (let i = this.values.length - 1; i >= 0; i--) {
191 result.push(this.values[i]);
192 }
193
194 return new List(result);
73195 }
196
197 // Handled Edge Cases: Empty list, non-function callbacks/predicates/expressions, non-List arguments, non-array values
74198}
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.