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 all items from another list to the end of this list
17 * @param {List} other - The list to append
18 * @return {List} A new list containing all items from both lists
1419 */
1520 append(other) {
16 throw new Error('Remove this line and implement the function');
21 // Edge Case: Appending an empty list should return a copy of the original list
22 if (!other || !other.values || other.length() === 0) {
23 return new List(this.values);
24 }
25
26 const result = [];
27 for (let i = 0; i < this.values.length; i++) {
28 result.push(this.values[i]);
29 }
30 for (let i = 0; i < other.values.length; i++) {
31 result.push(other.values[i]);
32 }
33 return new List(result);
34 // Handled Edge Cases: Appending empty list
1735 }
1836
1937 /**
20 * @param {List} others
21 * @return {List}
38 * Concatenates this list with a list of lists
39 * @param {List} others - A list containing other lists to concatenate
40 * @return {List} A new flattened list containing all items
2241 */
2342 concat(others) {
24 throw new Error('Remove this line and implement the function');
43 // Edge Case: Concatenating with an empty list of lists
44 if (!others || !others.values || others.length() === 0) {
45 return new List(this.values);
46 }
47
48 let result = [];
49
50 // Add items from the current list
51 for (let i = 0; i < this.values.length; i++) {
52 result.push(this.values[i]);
53 }
54
55 // Add items from each list in others
56 for (let i = 0; i < others.values.length; i++) {
57 const list = others.values[i];
58 // Edge Case: Handling null or undefined elements in the list of lists
59 if (list && list.values) {
60 for (let j = 0; j < list.values.length; j++) {
61 result.push(list.values[j]);
62 }
63 }
64 }
65
66 return new List(result);
67 // Handled Edge Cases: Concatenating with empty list, null/undefined elements in lists
2568 }
2669
2770 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
71 * Filters the list based on a predicate function
72 * @param {(any) => boolean} predicate - Function to test each element
73 * @return {List} A new list containing only elements that satisfy the predicate
3074 */
3175 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
76 // Edge Case: Empty list should return an empty list
77 if (this.values.length === 0) {
78 return new List([]);
79 }
80
81 // Edge Case: Invalid predicate function
82 if (typeof predicate !== 'function') {
83 throw new Error('Predicate must be a function');
84 }
85
86 const result = [];
87 for (let i = 0; i < this.values.length; i++) {
88 if (predicate(this.values[i])) {
89 result.push(this.values[i]);
90 }
91 }
92 return new List(result);
93 // Handled Edge Cases: Empty list, invalid predicate function
3394 }
3495
3596 /**
36 * @param {(any) => any} expression
37 * @return {List}
97 * Maps each element in the list using the provided expression function
98 * @param {(any) => any} expression - Function to transform each element
99 * @return {List} A new list with transformed elements
38100 */
39101 map(expression) {
40 throw new Error('Remove this line and implement the function');
102 // Edge Case: Empty list should return an empty list
103 if (this.values.length === 0) {
104 return new List([]);
105 }
106
107 // Edge Case: Invalid expression function
108 if (typeof expression !== 'function') {
109 throw new Error('Expression must be a function');
110 }
111
112 const result = [];
113 for (let i = 0; i < this.values.length; i++) {
114 result.push(expression(this.values[i]));
115 }
116 return new List(result);
117 // Handled Edge Cases: Empty list, invalid expression function
41118 }
42119
43120 /**
44 * @return {number}
121 * Returns the number of elements in the list
122 * @return {number} The length of the list
45123 */
46124 length() {
47 throw new Error('Remove this line and implement the function');
125 // Edge Case: Count elements properly even when list has no elements
126 let count = 0;
127 for (let i = 0; i < this.values.length; i++) {
128 count++;
129 }
130 return count;
131 // Handled Edge Cases: Empty list
48132 }
49133
50134 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
135 * Folds the list from the left using a callback function and initial accumulator
136 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
137 * @param {any} initial - Initial accumulator value
138 * @return {any} The final accumulator value
54139 */
55140 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
141 // Edge Case: Invalid callback function
142 if (typeof callback !== 'function') {
143 throw new Error('Callback must be a function');
144 }
145
146 // Edge Case: Empty list should return the initial value
147 if (this.values.length === 0) {
148 return initial;
149 }
150
151 let accumulator = initial;
152 for (let i = 0; i < this.values.length; i++) {
153 accumulator = callback(accumulator, this.values[i]);
154 }
155 return accumulator;
156 // Handled Edge Cases: Empty list, invalid callback function
57157 }
58158
59159 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
160 * Folds the list from the right using a callback function and initial accumulator
161 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
162 * @param {any} initial - Initial accumulator value
163 * @return {any} The final accumulator value
63164 */
64165 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
166 // Edge Case: Invalid callback function
167 if (typeof callback !== 'function') {
168 throw new Error('Callback must be a function');
169 }
170
171 // Edge Case: Empty list should return the initial value
172 if (this.values.length === 0) {
173 return initial;
174 }
175
176 let accumulator = initial;
177 for (let i = this.values.length - 1; i >= 0; i--) {
178 accumulator = callback(accumulator, this.values[i]);
179 }
180 return accumulator;
181 // Handled Edge Cases: Empty list, invalid callback function
66182 }
67183
68184 /**
69 * @return {List}
185 * Returns a new list with elements in reverse order
186 * @return {List} A new list with reversed elements
70187 */
71188 reverse() {
72 throw new Error('Remove this line and implement the function');
189 // Edge Case: Reversing an empty list should return an empty list
190 if (this.values.length === 0) {
191 return new List([]);
192 }
193
194 const result = [];
195 for (let i = this.values.length - 1; i >= 0; i--) {
196 result.push(this.values[i]);
197 }
198 return new List(result);
199 // Handled Edge Cases: Empty list
73200 }
74201}
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.