future

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 instance
9 * @param {Array} [values=[]] - Initial values for the list
10 */
11 constructor(values = []) {
12 // Edge Case: Handle non-array inputs 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
20 * Handled Edge Cases: undefined input list states
1421 */
1522 append(other) {
16 throw new Error('Remove this line and implement the function');
23 // Edge Case: Handle null or undefined other list
24 if (!other || !(other instanceof List)) {
25 return new List(this.values);
26 }
27
28 return new List([...this.values, ...other.values]);
1729 }
1830
1931 /**
20 * @param {List} others
21 * @return {List}
32 * Concatenates all items from a series of lists into one flattened list
33 * @param {List} others - A list of lists to concatenate
34 * @return {List} A new List with all items from all lists (one level flattened)
35 * Handled Edge Cases: undefined input list states, concatenate operation with nested lists
2236 */
2337 concat(others) {
24 throw new Error('Remove this line and implement the function');
38 // Edge Case: Handle null or undefined others list
39 if (!others || !(others instanceof List)) {
40 return new List(this.values);
41 }
42
43 let result = [...this.values];
44
45 // Edge Case: Handle non-list elements in others
46 for (const item of others.values) {
47 if (item instanceof List) {
48 result = [...result, ...item.values];
49 } else {
50 // For non-List items, add them directly (one level flattening)
51 result.push(item);
52 }
53 }
54
55 return new List(result);
2556 }
2657
2758 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
59 * Filters the list based on a predicate function
60 * @param {(any) => boolean} predicate - Function to test each item
61 * @return {List} A new List containing only items for which predicate returns true
62 * Handled Edge Cases: undefined input list states
3063 */
3164 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
65 // Edge Case: Handle null or undefined predicate
66 if (typeof predicate !== 'function') {
67 return new List([]);
68 }
69
70 const result = [];
71
72 for (const item of this.values) {
73 if (predicate(item)) {
74 result.push(item);
75 }
76 }
77
78 return new List(result);
3379 }
3480
3581 /**
36 * @param {(any) => any} expression
37 * @return {List}
82 * Transforms each item in the list using a mapping function
83 * @param {(any) => any} expression - Function to transform each item
84 * @return {List} A new List containing transformed items
85 * Handled Edge Cases: undefined input list states
3886 */
3987 map(expression) {
40 throw new Error('Remove this line and implement the function');
88 // Edge Case: Handle null or undefined expression
89 if (typeof expression !== 'function') {
90 return new List([]);
91 }
92
93 const result = [];
94
95 for (const item of this.values) {
96 result.push(expression(item));
97 }
98
99 return new List(result);
41100 }
42101
43102 /**
44 * @return {number}
103 * Returns the number of items in the list
104 * @return {number} The length of the list
105 * Handled Edge Cases: undefined input list states
45106 */
46107 length() {
47 throw new Error('Remove this line and implement the function');
108 // Edge Case: Handle undefined values array
109 if (!this.values) {
110 return 0;
111 }
112
113 let count = 0;
114
115 for (const _ of this.values) {
116 count++;
117 }
118
119 return count;
48120 }
49121
50122 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
123 * Folds (reduces) the list from the left using a callback function
124 * @param {(acc: any, el: any) => any} callback - Function to apply to each item
125 * @param {any} initial - Initial accumulator value
126 * @return {any} The final accumulator value
127 * Handled Edge Cases: undefined input list states, fold function argument ordering significance
54128 */
55129 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
130 // Edge Case: Handle null or undefined callback
131 if (typeof callback !== 'function') {
132 return initial;
133 }
134
135 let accumulator = initial;
136
137 for (const item of this.values) {
138 // Edge Case: Ensure correct argument ordering (accumulator first, then current item)
139 accumulator = callback(accumulator, item);
140 }
141
142 return accumulator;
57143 }
58144
59145 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
146 * Folds (reduces) the list from the right using a callback function
147 * @param {(acc: any, el: any) => any} callback - Function to apply to each item
148 * @param {any} initial - Initial accumulator value
149 * @return {any} The final accumulator value
150 * Handled Edge Cases: undefined input list states, fold function argument ordering significance
63151 */
64152 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
153 // Edge Case: Handle null or undefined callback
154 if (typeof callback !== 'function') {
155 return initial;
156 }
157
158 let accumulator = initial;
159
160 // Process from right to left
161 for (let i = this.values.length - 1; i >= 0; i--) {
162 // Edge Case: Ensure correct argument ordering (accumulator first, then current item)
163 accumulator = callback(accumulator, this.values[i]);
164 }
165
166 return accumulator;
66167 }
67168
68169 /**
69 * @return {List}
170 * Returns a new list with all items in reverse order
171 * @return {List} A new List with items in reverse order
172 * Handled Edge Cases: undefined input list states
70173 */
71174 reverse() {
72 throw new Error('Remove this line and implement the function');
175 const result = [];
176
177 // Edge Case: Handle undefined values array
178 if (!this.values) {
179 return new List(result);
180 }
181
182 for (let i = this.values.length - 1; i >= 0; i--) {
183 result.push(this.values[i]);
184 }
185
186 return new List(result);
73187 }
74188}
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.