A.2.1

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 no values are provided by initializing with an empty array
9 this.values = values;
910 }
1011
1112 /**
12 * @param {List} other
13 * @return {List}
13 * Appends all items from another list to the end of this list
14 * @param {List} other - The list to append
15 * @return {List} A new list containing all items from both lists
1416 */
1517 append(other) {
16 throw new Error('Remove this line and implement the function');
18 // Edge Case: Handle when other list is empty
19 if (!other || !other.values) {
20 return new List(this.values);
21 }
22
23 const result = [];
24 for (let i = 0; i < this.values.length; i++) {
25 result.push(this.values[i]);
26 }
27 for (let i = 0; i < other.values.length; i++) {
28 result.push(other.values[i]);
29 }
30 return new List(result);
31 // Handled Edge Cases: Empty other list
1732 }
1833
1934 /**
20 * @param {List} others
21 * @return {List}
35 * Concatenates a series of lists into one flattened list
36 * @param {List} others - A list containing other lists to concatenate
37 * @return {List} A new list with all items from all lists
2238 */
2339 concat(others) {
24 throw new Error('Remove this line and implement the function');
40 // Edge Case: Handle when others is null or undefined
41 if (!others) {
42 return new List(this.values);
43 }
44
45 let result = [];
46
47 // Add items from the current list
48 for (let i = 0; i < this.values.length; i++) {
49 result.push(this.values[i]);
50 }
51
52 // Add items from each list in others
53 for (let i = 0; i < others.values.length; i++) {
54 const item = others.values[i];
55 if (item instanceof List) {
56 // If item is a List, add its values
57 for (let j = 0; j < item.values.length; j++) {
58 result.push(item.values[j]);
59 }
60 } else {
61 // If item is not a List, add it directly
62 result.push(item);
63 }
64 }
65
66 return new List(result);
67 // Handled Edge Cases: Null/undefined others, non-List items in others
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 item
73 * @return {List} A new list containing only items that satisfy the predicate
3074 */
3175 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
76 // Edge Case: Handle when predicate is not a function
77 if (typeof predicate !== 'function') {
78 throw new Error('Predicate must be a function');
79 }
80
81 const result = [];
82 for (let i = 0; i < this.values.length; i++) {
83 if (predicate(this.values[i])) {
84 result.push(this.values[i]);
85 }
86 }
87 return new List(result);
88 // Handled Edge Cases: Non-function predicate
3389 }
3490
3591 /**
36 * @param {(any) => any} expression
37 * @return {List}
92 * Maps each item in the list using a provided function
93 * @param {(any) => any} expression - Function to transform each item
94 * @return {List} A new list with transformed items
3895 */
3996 map(expression) {
40 throw new Error('Remove this line and implement the function');
97 // Edge Case: Handle when expression is not a function
98 if (typeof expression !== 'function') {
99 throw new Error('Expression must be a function');
100 }
101
102 const result = [];
103 for (let i = 0; i < this.values.length; i++) {
104 result.push(expression(this.values[i]));
105 }
106 return new List(result);
107 // Handled Edge Cases: Non-function expression
41108 }
42109
43110 /**
44 * @return {number}
111 * Returns the number of items in the list
112 * @return {number} The length of the list
45113 */
46114 length() {
47 throw new Error('Remove this line and implement the function');
115 let count = 0;
116 for (let i = 0; i < this.values.length; i++) {
117 count++;
118 }
119 return count;
120 // Handled Edge Cases: None (empty list is naturally handled)
48121 }
49122
50123 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
124 * Folds the list from the left using a callback function and initial accumulator
125 * @param {(acc: any, el: any) => any} callback - Function to apply to each item
126 * @param {any} initial - Initial accumulator value
127 * @return {any} The final accumulator value
54128 */
55129 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
130 // Edge Case: Handle when callback is not a function
131 if (typeof callback !== 'function') {
132 throw new Error('Callback must be a function');
133 }
134
135 let accumulator = initial;
136 for (let i = 0; i < this.values.length; i++) {
137 accumulator = callback(accumulator, this.values[i]);
138 }
139 return accumulator;
140 // Handled Edge Cases: Non-function callback
57141 }
58142
59143 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
144 * Folds the list from the right using a callback function and initial accumulator
145 * @param {(acc: any, el: any) => any} callback - Function to apply to each item
146 * @param {any} initial - Initial accumulator value
147 * @return {any} The final accumulator value
63148 */
64149 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
150 // Edge Case: Handle when callback is not a function
151 if (typeof callback !== 'function') {
152 throw new Error('Callback must be a function');
153 }
154
155 let accumulator = initial;
156 // Process from right to left
157 for (let i = this.values.length - 1; i >= 0; i--) {
158 accumulator = callback(accumulator, this.values[i]);
159 }
160 return accumulator;
161 // Handled Edge Cases: Non-function callback, empty list
66162 }
67163
68164 /**
69 * @return {List}
165 * Returns a new list with items in reverse order
166 * @return {List} A new list with reversed items
70167 */
71168 reverse() {
72 throw new Error('Remove this line and implement the function');
169 const result = [];
170 // Build result array in reverse order
171 for (let i = this.values.length - 1; i >= 0; i--) {
172 result.push(this.values[i]);
173 }
174 return new List(result);
175 // Handled Edge Cases: Empty list
73176 }
74177}
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.