Skip to content

Commit 51a0530

Browse files
committed
♻️ Update
1 parent 415cf47 commit 51a0530

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

β€Žarray_to_list.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,24 @@ export interface List<T> {
33
rest: List<T> | null;
44
}
55

6-
// Recursivity FTW πŸš€πŸš€πŸš€
76
export function arrayToList<T>([value, ...values]: T[]): List<T> | null {
87
return value ? { value, rest: arrayToList(values) } : null;
98
}
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

Comments
Β (0)