unicode

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
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.values.length === 0) {
23 return new List(this.values);
24 }
25
26 const combined = [];
27 for (let i = 0; i < this.values.length; i++) {
28 combined.push(this.values[i]);
29 }
30 for (let i = 0; i < other.values.length; i++) {
31 combined.push(other.values[i]);
32 }
33 return new List(combined);
34 // Handled Edge Cases: Appending empty list
1735 }
1836
1937 /**
20 * @param {List} others
21 * @return {List}
38 * Concatenates multiple lists into one
39 * @param {List} others - A list of lists to concatenate
40 * @return {List} A new list with all elements from all lists flattened
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.values.length === 0) {
45 return new List(this.values);
46 }
47
48 let result = new List(this.values);
49 for (let i = 0; i < others.values.length; i++) {
50 result = result.append(others.values[i]);
51 }
52 return result;
53 // Handled Edge Cases: Empty list of lists
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 containing only elements that satisfy the predicate
3060 */
3161 filter(predicate) {
32 throw new Error('Remove this line and implement the function');
62 // Edge Case: Filtering an empty list should return an empty list
63 if (this.values.length === 0) {
64 return new List([]);
65 }
66
67 const filtered = [];
68 for (let i = 0; i < this.values.length; i++) {
69 if (predicate(this.values[i])) {
70 filtered.push(this.values[i]);
71 }
72 }
73 return new List(filtered);
74 // Handled Edge Cases: Empty list, predicate that matches no elements, predicate that matches all elements
3375 }
3476
3577 /**
36 * @param {(any) => any} expression
37 * @return {List}
78 * Transforms each element using the provided function
79 * @param {(any) => any} expression - Function to transform each element
80 * @return {List} A new list with transformed elements
3881 */
3982 map(expression) {
40 throw new Error('Remove this line and implement the function');
83 // Edge Case: Mapping over an empty list should return an empty list
84 if (this.values.length === 0) {
85 return new List([]);
86 }
87
88 const mapped = [];
89 for (let i = 0; i < this.values.length; i++) {
90 mapped.push(expression(this.values[i]));
91 }
92 return new List(mapped);
93 // Handled Edge Cases: Empty list
4194 }
4295
4396 /**
44 * @return {number}
97 * Returns the number of elements in the list
98 * @return {number} The length of the list
4599 */
46100 length() {
47 throw new Error('Remove this line and implement the function');
101 // Edge Case: Length of empty list should be 0
102 let count = 0;
103 for (let i = 0; i < this.values.length; i++) {
104 count++;
105 }
106 return count;
107 // Handled Edge Cases: Empty list
48108 }
49109
50110 /**
51 * @param {(acc: any, el: any) => any} callback
52 * @param {any} initial
53 * @return {any}
111 * Folds the list from the left using a callback function and initial accumulator
112 * @param {(acc: any, el: any) => any} callback - Function to apply to accumulator and each element
113 * @param {any} initial - Initial value for the accumulator
114 * @return {any} The final accumulated value
54115 */
55116 foldl(callback, initial) {
56 throw new Error('Remove this line and implement the function');
117 // Edge Case: Folding an empty list should return the initial value
118 if (this.values.length === 0) {
119 return initial;
120 }
121
122 let accumulator = initial;
123 for (let i = 0; i < this.values.length; i++) {
124 accumulator = callback(accumulator, this.values[i]);
125 }
126 return accumulator;
127 // Handled Edge Cases: Empty list
57128 }
58129
59130 /**
60 * @param {(acc: any, el: any) => any} callback
61 * @param {any} initial
62 * @return {any}
131 * Folds the list from the right using a callback function and initial accumulator
132 * @param {(acc: any, el: any) => any} callback - Function to apply to accumulator and each element
133 * @param {any} initial - Initial value for the accumulator
134 * @return {any} The final accumulated value
63135 */
64136 foldr(callback, initial) {
65 throw new Error('Remove this line and implement the function');
137 // Edge Case: Folding an empty list should return the initial value
138 if (this.values.length === 0) {
139 return initial;
140 }
141
142 let accumulator = initial;
143 for (let i = this.values.length - 1; i >= 0; i--) {
144 accumulator = callback(accumulator, this.values[i]);
145 }
146 return accumulator;
147 // Handled Edge Cases: Empty list
66148 }
67149
68150 /**
69 * @return {List}
151 * Reverses the order of elements in the list
152 * @return {List} A new list with elements in reverse order
70153 */
71154 reverse() {
72 throw new Error('Remove this line and implement the function');
155 // Edge Case: Reversing an empty list should return an empty list
156 if (this.values.length === 0) {
157 return new List([]);
158 }
159
160 const reversed = [];
161 for (let i = this.values.length - 1; i >= 0; i--) {
162 reversed.push(this.values[i]);
163 }
164 return new List(reversed);
165 // Handled Edge Cases: Empty list, single element list
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.