-
Notifications
You must be signed in to change notification settings - Fork 7
/
foldable.ts
38 lines (35 loc) · 949 Bytes
/
foldable.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Foldable is a structure that allows a function to all values inside of a
* structure. The reduction is accumulative and ordered.
*
* @module Foldable
* @since 2.0.0
*/
import type { $, Hold, Kind } from "./kind.ts";
import type { Initializable } from "./initializable.ts";
/**
* A Foldable structure has the method fold.
*
* @since 2.0.0
*/
export interface Foldable<U extends Kind> extends Hold<U> {
readonly fold: <A, O>(
reducer: (accumulator: O, value: A) => O,
accumulator: O,
) => <B = never, C = never, D = unknown, E = unknown>(
ua: $<U, [A, B, C], [D], [E]>,
) => O;
}
/**
* @experimental
* @since 2.0.0
*/
export function collect<U extends Kind, A>(
{ fold }: Foldable<U>,
{ combine, init }: Initializable<A>,
): <B = never, C = never, D = unknown, E = unknown>(
ua: $<U, [A, B, C], [D], [E]>,
) => A {
return (ua) =>
fold<A, A>((first, second) => combine(second)(first), init())(ua);
}