fistinz

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 another list to this list
18 * @param {List} other - The list to append
19 * @return {List} A new list containing all elements 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');
25 }
26
27 return new List(this.values.concat(other.values));
28 // Handled Edge Cases: other not being a List instance
1729 }
1830
1931 /**
20 * @param {List} others
21 * @return {List}
32 * Concatenates multiple lists into one
33 * @param {List} others - A list of lists to concatenate
34 * @return {List} A new list with all elements flattened
2235 */
2336 concat(others) {
24 throw new Error('Remove this line and implement the function');
37 // Edge Case: Handle when others is not a List instance
38 if (!(others instanceof List)) {
39 throw new Error('Argument must be a List');
40 }
41
42 let result = this.values.slice(); // Copy current values
43
44 // Edge Case: Handle when elements in others are not List instances
45 for (const list of others.values) {
46 if (!(list instanceof List)) {
47 throw new Error('All elements in others must be List instances');
48 }
49 result = result.concat(list.values);
50 }
51
52 return new List(result);
53 // Handled Edge Cases: others not being a List instance, elements in others not being List instances
2554 }
2655
2756 /**
28 * @param {(any) => boolean} predicate
29 * @return {List}
57 * Filters the list based on a predicate function
58 * @param {(any) => boolean} predicate - Function to test each element
59 * @return {List} A new list with elements that pass the predicate test
3060 */
3161 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
62 // Edge Case: Handle when predicate is not a function
63 if (typeof predicate !== 'function') {
64 throw new Error('Predicate must be a function');
65 }
66
67 const result = [];
68 for (const value of this.values) {
69 if (predicate(value)) {
70 result.push(value);
71 }
72 }
73
74 return new List(result);
75 // Handled Edge Cases: predicate not being a function
3376 }
3477
3578 /**
36 * @param {(any) => any} expression
37 * @return {List}
79 * Maps each element using a provided function
80 * @param {(any) => any} expression - Function to transform each element
81 * @return {List} A new list with transformed elements
3882 */
3983 map(expression) {
40 throw new Error('Remove this line and implement the function');
84 // Edge Case: Handle when expression is not a function
85 if (typeof expression !== 'function') {
86 throw new Error('Expression must be a function');
87 }
88
89 const result = [];
90 for (const value of this.values) {
91 result.push(expression(value));
92 }
93
94 return new List(result);
95 // Handled Edge Cases: expression not being a function
4196 }
4297
4398 /**
44 * @return {number}
99 * Returns the length of the list
100 * @return {number} The number of elements in the list
45101 */
46102 length() {
47 throw new Error('Remove this line and implement the function');
103 let count = 0;
104 for (const _ of this.values) {
105 count++;
106 }
107 return count;
108 // Handled Edge Cases: None (this is a simple counting operation)
48109 }
49110
50111 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
112 * Folds the list from the left using a callback function
113 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
114 * @param {any} initial - Initial accumulator value
115 * @return {any} The final accumulator value
54116 */
55117 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
118 // Edge Case: Handle when callback is not a function
119 if (typeof callback !== 'function') {
120 throw new Error('Callback must be a function');
121 }
122
123 let accumulator = initial;
124 for (const value of this.values) {
125 accumulator = callback(accumulator, value);
126 }
127
128 return accumulator;
129 // Handled Edge Cases: callback not being a function
57130 }
58131
59132 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
133 * Folds the list from the right using a callback function
134 * @param {(acc: any, el: any) => any} callback - Function to apply to each element
135 * @param {any} initial - Initial accumulator value
136 * @return {any} The final accumulator value
63137 */
64138 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
139 // Edge Case: Handle when callback is not a function
140 if (typeof callback !== 'function') {
141 throw new Error('Callback must be a function');
142 }
143
144 let accumulator = initial;
145 // Process elements from right to left
146 for (let i = this.values.length - 1; i >= 0; i--) {
147 accumulator = callback(accumulator, this.values[i]);
148 }
149
150 return accumulator;
151 // Handled Edge Cases: callback not being a function
66152 }
67153
68154 /**
69 * @return {List}
155 * Reverses the list
156 * @return {List} A new list with elements in reverse order
70157 */
71158 reverse() {
72 throw new Error('Remove this line and implement the function');
159 const result = [];
160 for (let i = this.values.length - 1; i >= 0; i--) {
161 result.push(this.values[i]);
162 }
163
164 return new List(result);
165 // Handled Edge Cases: None (works with empty lists naturally)
73166 }
74167}
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.