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
/
affect.ts
151 lines (116 loc) · 4.32 KB
/
affect.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import type * as TC from "./type_classes.ts";
import type * as HKT from "./hkt.ts";
import * as E from "./either.ts";
import { createDo } from "./derivations.ts";
import { flow, identity, pipe } from "./fns.ts";
import { createSequenceStruct, createSequenceTuple } from "./sequence.ts";
/*******************************************************************************
* Types
******************************************************************************/
export type Affect<R, E, A> = (r: R) => Promise<E.Either<E, A>>;
/*******************************************************************************
* Kind Registration
******************************************************************************/
export const URI = "Affect";
export type URI = typeof URI;
export type URIS = HKT.URIS;
declare module "./hkt.ts" {
// deno-lint-ignore no-explicit-any
export interface Kinds<_ extends any[]> {
[URI]: Affect<_[2], _[1], _[0]>;
}
}
/*******************************************************************************
* Utilites
******************************************************************************/
export const make = <A>(a: A): Promise<A> => Promise.resolve(a);
export const then = <A, B>(fab: (a: A) => B) =>
(p: Promise<A>): Promise<B> => p.then(fab);
/*******************************************************************************
* Constructors
******************************************************************************/
export const ask = <R, L = never>(): Affect<R, L, R> => flow(E.right, make);
export const askLeft = <L, R = never>(): Affect<L, L, R> => flow(E.left, make);
export const asks = <R, E = never, A = never>(
fra: (r: R) => Promise<A>,
): Affect<R, E, A> => flow(fra, then(E.right));
export const asksLeft = <R, E = never, A = never>(
fre: (r: R) => Promise<E>,
): Affect<R, E, A> => flow(fre, then(E.left));
export const right = <R = never, E = never, A = never>(
right: A,
): Affect<R, E, A> => () => make(E.right(right));
export const left = <R = never, E = never, A = never>(
left: E,
): Affect<R, E, A> => () => make(E.left(left));
/*******************************************************************************
* Modules
******************************************************************************/
export const Functor: TC.Functor<URI> = {
map: (fai) => (ta) => flow(ta, then(E.map(fai))),
};
export const Bifunctor: TC.Bifunctor<URI> = {
bimap: (fbj, fai) => (ta) => flow(ta, then(E.bimap(fbj, fai))),
mapLeft: (fbj) => (ta) => flow(ta, then(E.mapLeft(fbj))),
};
export const Apply: TC.Apply<URI> = {
ap: (tfab) =>
(ta) =>
async (r) => {
const efab = await tfab(r);
const ea = await ta(r);
return pipe(
ea,
E.ap(efab),
);
},
map: Functor.map,
};
export const Applicative: TC.Applicative<URI> = {
of: right,
ap: Apply.ap,
map: Functor.map,
};
export const Chain: TC.Chain<URI> = {
ap: Apply.ap,
map: Functor.map,
chain: (fati) =>
(ta) =>
async (r) => {
const ea = await ta(r);
return E.isLeft(ea) ? ea : fati(ea.right)(r);
},
};
export const Monad: TC.Monad<URI> = {
of: Applicative.of,
ap: Apply.ap,
map: Functor.map,
join: Chain.chain(identity),
chain: Chain.chain,
};
export const MonadThrow: TC.MonadThrow<URI> = {
...Monad,
throwError: left,
};
/*******************************************************************************
* Pipeables
******************************************************************************/
export const { of, ap, map, join, chain, throwError } = MonadThrow;
export const { bimap, mapLeft } = Bifunctor;
export const sequenceTuple = createSequenceTuple(Apply);
export const sequenceStruct = createSequenceStruct(Apply);
export const compose = <E = never, A = never, B = never>(
aeb: Affect<A, E, B>,
) =>
<R>(rea: Affect<R, E, A>): Affect<R, E, B> =>
async (r) => {
const ea = await rea(r);
return E.isLeft(ea) ? ea : await aeb(ea.right);
};
export const recover = <E, A>(fea: (e: E) => A) =>
<R>(ta: Affect<R, E, A>): Affect<R, E, A> =>
flow(ta, then(E.fold(flow(fea, E.right), E.right)));
/*******************************************************************************
* Do
******************************************************************************/
export const { Do, bind, bindTo } = createDo(Monad);