@ngel

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 this.values = values;
913 }
1014
1115 /**
12 * @param {List} other
13 * @return {List}
16 * Appends another list to this list
17 * @param {List} other - The list to append
18 * @return {List} A new list containing all elements from both lists
19 *
20 * Handled Edge Cases: empty lists, null/undefined other list
1421 */
1522 append(other) {
16 throw new Error('Remove this line and implement the function');
23 // Edge Case: Handle null or undefined other parameter
24 if (!other) {
25 return new List(this.values);
26 }
27
28 // Edge Case: Handle when other is not a List instance
29 if (!(other instanceof List)) {
30 throw new Error('Parameter must be a List instance');
31 }
32
33 return new List([...this.values, ...other.values]);
1734 }
1835
1936 /**
20 * @param {List} others
21 * @return {List}
37 * Concatenates multiple lists into one
38 * @param {List} others - A list of lists to concatenate
39 * @return {List} A new list with all elements from all lists flattened
40 *
41 * Handled Edge Cases: empty lists, null/undefined others list
2242 */
2343 concat(others) {
24 throw new Error('Remove this line and implement the function');
44 // Edge Case: Handle null or undefined others parameter
45 if (!others) {
46 return new List(this.values);
47 }
48
49 // Edge Case: Handle when others is not a List instance
50 if (!(others instanceof List)) {
51 throw new Error('Parameter must be a List instance');
52 }
53
54 let result = [...this.values];
55 for (const list of others.values) {
56 // Edge Case: Handle when an element in others is not a List instance
57 if (!(list instanceof List)) {
58 throw new Error('All elements in others must be List instances');
59 }
60 result = [...result, ...list.values];
61 }
62
63 return new List(result);
2564 }
2665
2766 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
67 * Filters the list based on a predicate function
68 * @param {(any) => boolean} predicate - Function to test each element
69 * @return {List} A new list containing only elements that pass the predicate
70 *
71 * Handled Edge Cases: empty list, null/undefined predicate
3072 */
3173 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
74 // Edge Case: Handle null or undefined predicate
75 if (typeof predicate !== 'function') {
76 throw new Error('Predicate must be a function');
77 }
78
79 const result = [];
80 for (const value of this.values) {
81 if (predicate(value)) {
82 result.push(value);
83 }
84 }
85
86 return new List(result);
3387 }
3488
3589 /**
36 * @param {(any) => any} expression
37 * @return {List}
90 * Maps each element using a provided function
91 * @param {(any) => any} expression - Function to transform each element
92 * @return {List} A new list with transformed elements
93 *
94 * Handled Edge Cases: empty list, null/undefined expression
3895 */
3996 map(expression) {
40 throw new Error('Remove this line and implement the function');
97 // Edge Case: Handle null or undefined expression
98 if (typeof expression !== 'function') {
99 throw new Error('Expression must be a function');
100 }
101
102 const result = [];
103 for (const value of this.values) {
104 result.push(expression(value));
105 }
106
107 return new List(result);
41108 }
42109
43110 /**
44 * @return {number}
111 * Returns the length of the list
112 * @return {number} The number of elements in the list
113 *
114 * Handled Edge Cases: empty list
45115 */
46116 length() {
47 throw new Error('Remove this line and implement the function');
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 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
129 *
130 * Handled Edge Cases: empty list, null/undefined callback
54131 */
55132 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
133 // Edge Case: Handle null or undefined callback
134 if (typeof callback !== 'function') {
135 throw new Error('Callback must be a function');
136 }
137
138 let accumulator = initial;
139 for (const value of this.values) {
140 accumulator = callback(accumulator, value);
141 }
142
143 return accumulator;
57144 }
58145
59146 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
147 * Folds the list from the right using a callback function
148 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
149 * @param {any} initial - Initial accumulator value
150 * @return {any} The final accumulator value
151 *
152 * Handled Edge Cases: empty list, null/undefined callback
63153 */
64154 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
155 // Edge Case: Handle null or undefined callback
156 if (typeof callback !== 'function') {
157 throw new Error('Callback must be a function');
158 }
159
160 let accumulator = initial;
161 // Process elements from right to left
162 for (let i = this.values.length - 1; i >= 0; i--) {
163 accumulator = callback(accumulator, this.values[i]);
164 }
165
166 return accumulator;
66167 }
67168
68169 /**
69 * @return {List}
170 * Reverses the list
171 * @return {List} A new list with elements in reverse order
172 *
173 * Handled Edge Cases: empty list
70174 */
71175 reverse() {
72 throw new Error('Remove this line and implement the function');
176 const result = [];
177 for (let i = this.values.length - 1; i >= 0; i--) {
178 result.push(this.values[i]);
179 }
180
181 return new List(result);
73182 }
74183}
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.