We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 415cf47 commit 51a0530Copy full SHA for 51a0530
βarray_to_list.ts
@@ -3,7 +3,24 @@ export interface List<T> {
3
rest: List<T> | null;
4
}
5
6
-// Recursivity FTW πππ
7
export function arrayToList<T>([value, ...values]: T[]): List<T> | null {
8
return value ? { value, rest: arrayToList(values) } : null;
9
+
10
+export function arrayToListReduce<T>(values: T[]) {
11
+ // it would enter on one line if this were plain JS π₯²
12
+ return values.reduceRight<List<T> | null>(
13
+ (rest, value) => ({ value, rest }),
14
+ null,
15
+ );
16
+}
17
18
+export function arrayToListFor<T>(values: T[]) {
19
+ let rest = {};
20
21
+ for (let i = values.length - 1; i >= 0; i--) {
22
+ rest = { value: values[i], rest };
23
+ }
24
25
+ return rest as List<T>;
26
0 commit comments