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
/
task_either.ts
198 lines (159 loc) · 5.43 KB
/
task_either.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import type * as HKT from "./hkt.ts";
import type * as TC from "./type_classes.ts";
import type { Lazy } from "./types.ts";
import * as E from "./either.ts";
import * as T from "./task.ts";
import { createDo } from "./derivations.ts";
import { constant, flow, identity, pipe, wait } from "./fns.ts";
/*******************************************************************************
* Types
******************************************************************************/
export type TaskEither<L, R> = T.Task<E.Either<L, R>>;
/*******************************************************************************
* Kind Registration
******************************************************************************/
export const URI = "TaskEither";
export type URI = typeof URI;
declare module "./hkt.ts" {
// deno-lint-ignore no-explicit-any
export interface Kinds<_ extends any[]> {
[URI]: TaskEither<_[1], _[0]>;
}
}
/*******************************************************************************
* Constructors
******************************************************************************/
export const left = <E = never, A = never>(left: E): TaskEither<E, A> =>
T.of(E.left(left));
export const right = <E = never, A = never>(right: A): TaskEither<E, A> =>
T.of(E.right(right));
export const tryCatch = <E, A>(
f: Lazy<A>,
onError: (e: unknown) => E,
): TaskEither<E, A> =>
() => {
try {
return Promise.resolve(E.right(f()));
} catch (e) {
return Promise.resolve(E.left(onError(e)));
}
};
export const fromFailableTask = <E, A>(onError: (e: unknown) => E) =>
(ta: T.Task<A>): TaskEither<E, A> =>
() => ta().then(E.right).catch((e) => E.left(onError(e)));
export const fromEither = <E, A>(ta: E.Either<E, A>): TaskEither<E, A> =>
pipe(ta, E.fold((e) => left(e), right));
/*******************************************************************************
* Utilities
******************************************************************************/
export const then = <A, B>(fab: (a: A) => B) =>
(p: Promise<A>): Promise<B> => p.then(fab);
/*******************************************************************************
* Modules (Parallel)
******************************************************************************/
export const Functor: TC.Functor<URI> = {
map: (fai) => (ta) => flow(ta, then(E.map(fai))),
};
export const Bifunctor: TC.Bifunctor<URI> = {
bimap: (fab, fcd) => (tac) => () => tac().then(E.bimap(fab, fcd)),
mapLeft: (fef) => (ta) => pipe(ta, Bifunctor.bimap(fef, identity)),
};
export const Apply: TC.Apply<URI> = {
ap: flow(T.map(E.ap), T.ap),
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: (fatb) =>
(ta) =>
async () => {
const ea = await ta();
if (E.isLeft(ea)) {
return ea;
}
return await fatb(ea.right)();
},
};
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> = {
of: Applicative.of,
ap: Apply.ap,
map: Functor.map,
join: Monad.join,
chain: Chain.chain,
throwError: left,
};
export const Alt: TC.Alt<URI> = ({
map: Functor.map,
alt: (tb) => (ta) => () => ta().then((te) => E.isLeft(te) ? tb() : te),
});
/*******************************************************************************
* Modules (Sequential)
******************************************************************************/
// TODO Refactor in terms of Task.ap (Sequential)
export const ApplySeq: TC.Apply<URI> = {
ap: (tfab) =>
(ta) =>
async () => {
const efab = await tfab();
const ea = await ta();
return pipe(ea, E.ap(efab));
},
map: Functor.map,
};
export const ApplicativeSeq: TC.Applicative<URI> = {
of: right,
ap: ApplySeq.ap,
map: Functor.map,
};
export const ChainSeq: TC.Chain<URI> = {
ap: ApplySeq.ap,
map: Functor.map,
chain: Chain.chain,
};
export const MonadSeq: TC.Monad<URI> = {
of: ApplicativeSeq.of,
ap: ApplySeq.ap,
map: Functor.map,
join: ChainSeq.chain(identity),
chain: ChainSeq.chain,
};
export const MonadThrowSeq: TC.MonadThrow<URI> = {
of: ApplicativeSeq.of,
ap: ApplySeq.ap,
map: Functor.map,
join: MonadSeq.join,
chain: ChainSeq.chain,
throwError: left,
};
/*******************************************************************************
* Pipeables
******************************************************************************/
export const { of, ap, map, join, chain } = Monad;
export const { bimap, mapLeft } = Bifunctor;
export const { ap: apSeq } = ApplySeq;
export const chainLeft = <E, A>(onLeft: (e: E) => TaskEither<E, A>) =>
T.chain(E.fold<E, A, TaskEither<E, A>>(onLeft, right));
export const widen: <F>() => <E, A>(
ta: TaskEither<E, A>,
) => TaskEither<E | F, A> = constant(identity);
// This leaks async ops so we cut it for now.
//export const timeout = <E, A>(ms: number, onTimeout: () => E) =>
// (ta: TaskEither<E, A>): TaskEither<E, A> =>
// () => Promise.race([ta(), wait(ms).then(flow(onTimeout, E.left))]);
/*******************************************************************************
* Do Notation
******************************************************************************/
export const { Do, bind, bindTo } = createDo(Monad);