This repository has been archived by the owner on May 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtask.ts
113 lines (87 loc) · 3.2 KB
/
task.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import type * as HKT from "./hkt.ts";
import type * as TC from "./type_classes.ts";
import { Lazy } from "./types.ts";
import { apply, flow, wait, identity } from "./fns.ts";
import { createDo } from "./derivations.ts";
/*******************************************************************************
* Types
******************************************************************************/
export type Task<A> = () => Promise<A>;
/*******************************************************************************
* Kind Registration
******************************************************************************/
export const URI = "Task";
export type URI = typeof URI;
declare module "./hkt.ts" {
// deno-lint-ignore no-explicit-any
export interface Kinds<_ extends any[]> {
[URI]: Task<_[0]>;
}
}
/*******************************************************************************
* Combinators
******************************************************************************/
export const make = <A>(a: A): Task<A> => () => Promise.resolve(a);
export const delay = (ms: number) =>
<A>(ma: Task<A>): Task<A> => () => wait(ms).then(ma);
export const fromThunk = <A>(fa: Lazy<A>): Task<A> =>
() => Promise.resolve(fa());
export const tryCatch = <A>(fa: Lazy<A>, onError: (e: unknown) => A): Task<A> =>
() => {
try {
return Promise.resolve(fa());
} catch (e) {
return Promise.resolve(onError(e));
}
};
/*******************************************************************************
* Modules (Parallel)
******************************************************************************/
export const Functor: TC.Functor<URI> = {
map: (fab) => (ta) => () => ta().then(fab),
};
export const Apply: TC.Apply<URI> = {
ap: (tfab) =>
(ta) => () => Promise.all([tfab(), ta()]).then(([f, a]) => f(a)),
map: Functor.map,
};
export const Applicative: TC.Applicative<URI> = {
of: (a) => () => Promise.resolve(a),
ap: Apply.ap,
map: Functor.map,
};
export const Chain: TC.Chain<URI> = {
ap: Apply.ap,
map: Functor.map,
chain: (fatb) => (ta) => () => ta().then(flow(fatb, apply())),
};
export const Monad: TC.Monad<URI> = {
of: Applicative.of,
ap: Apply.ap,
map: Functor.map,
join: Chain.chain(identity),
chain: Chain.chain,
};
/*******************************************************************************
* Modules (Sequential)
******************************************************************************/
export const ApplySeq: TC.Apply<URI> = {
ap: (tfab) => (ta) => async () => (await tfab())(await ta()),
map: Functor.map,
};
export const MonadSeq: TC.Monad<URI> = {
of: Applicative.of,
ap: ApplySeq.ap,
map: Functor.map,
join: Chain.chain(identity),
chain: Chain.chain,
};
/*******************************************************************************
* Pipeables
******************************************************************************/
export const { of, ap, map, join, chain } = Monad;
export const { ap: apSeq } = ApplySeq;
/*******************************************************************************
* Do Notation
******************************************************************************/
export const { Do, bind, bindTo } = createDo(Monad);