This repository was 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 pathio.ts
110 lines (82 loc) · 3.1 KB
/
io.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
import type * as HKT from "./hkt.ts";
import type * as TC from "./type_classes.ts";
import { createSequenceStruct, createSequenceTuple } from "./sequence.ts";
import { apply, constant, flow, pipe } from "./fns.ts";
import { createApplySemigroup, createDo } from "./derivations.ts";
/*******************************************************************************
* Types
******************************************************************************/
export type IO<A> = () => A;
/*******************************************************************************
* Kind Registration
******************************************************************************/
export const URI = "IO";
export type URI = typeof URI;
declare module "./hkt.ts" {
// deno-lint-ignore no-explicit-any
export interface Kinds<_ extends any[]> {
[URI]: IO<_[0]>;
}
}
/*******************************************************************************
* Modules
******************************************************************************/
export const Functor: TC.Functor<URI> = {
map: (fab) => (ta) => flow(ta, fab),
};
export const Apply: TC.Apply<URI> = {
ap: (tfab) => (ta) => () => tfab()(ta()),
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) => flow(ta, fatb, apply()),
};
export const Monad: TC.Monad<URI> = {
of: Applicative.of,
ap: Apply.ap,
map: Functor.map,
join: apply(),
chain: Chain.chain,
};
export const Extends: TC.Extend<URI> = {
map: Monad.map,
extend: (ftab) => (ta) => () => ftab(ta),
};
export const Foldable: TC.Foldable<URI> = {
reduce: (faba, a) => (tb) => faba(a, tb()),
};
export const Traversable: TC.Traversable<URI> = {
map: Monad.map,
reduce: Foldable.reduce,
traverse: (A) => (faub) => (ta) => pipe(faub(ta()), A.map(of)),
};
/*******************************************************************************
* Module Getters
******************************************************************************/
export const getSemigroup = createApplySemigroup(Apply);
export const getMonoid = <A>(M: TC.Monoid<A>): TC.Monoid<IO<A>> => ({
...getSemigroup(M),
empty: constant(M.empty),
});
/*******************************************************************************
* Pipeables
******************************************************************************/
export const { of, ap, map, join, chain } = Monad;
export const { reduce, traverse } = Traversable;
export const { extend } = Extends;
/*******************************************************************************
* Sequenec
******************************************************************************/
export const sequenceTuple = createSequenceTuple(Apply);
export const sequenceStruct = createSequenceStruct(Apply);
/*******************************************************************************
* Do Notation
******************************************************************************/
export const { Do, bind, bindTo } = createDo(Monad);