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
/
reader.ts
81 lines (60 loc) · 2.32 KB
/
reader.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
import type * as HKT from "./hkt.ts";
import type * as TC from "./type_classes.ts";
import { createDo } from "./derivations.ts";
import { constant, flow, identity, pipe } from "./fns.ts";
/*******************************************************************************
* Types
******************************************************************************/
export type Reader<R, A> = (r: R) => A;
/*******************************************************************************
* Kind Registration
******************************************************************************/
export const URI = "Reader";
export type URI = typeof URI;
declare module "./hkt.ts" {
// deno-lint-ignore no-explicit-any
export interface Kinds<_ extends any[]> {
[URI]: Reader<_[1], _[0]>;
}
}
/*******************************************************************************
* Constructors
******************************************************************************/
export const make: <R>(r: R) => Reader<R, R> = constant;
export const ask: <R>() => Reader<R, R> = () => identity;
export const asks: <R, A>(f: (r: R) => A) => Reader<R, A> = identity;
/*******************************************************************************
* Modules
******************************************************************************/
export const Functor: TC.Functor<URI> = {
map: (fab) => (ta) => flow(ta, fab),
};
export const Apply: TC.Apply<URI> = {
ap: (tfai) => (ta) => (r) => pipe(ta(r), tfai(r)),
map: Functor.map,
};
export const Applicative: TC.Applicative<URI> = {
of: constant,
ap: Apply.ap,
map: Functor.map,
};
export const Chain: TC.Chain<URI> = {
ap: Apply.ap,
map: Functor.map,
chain: (fatb) => (ta) => (r) => fatb(ta(r))(r),
};
export const Monad: TC.Monad<URI> = {
of: Applicative.of,
ap: Apply.ap,
map: Functor.map,
join: (tta) => (r) => tta(r)(r),
chain: Chain.chain,
};
/*******************************************************************************
* Pipeables
******************************************************************************/
export const { of, ap, map, join, chain } = Monad;
/*******************************************************************************
* Do
******************************************************************************/
export const { Do, bind, bindTo } = createDo(Monad);