forked from baetheus/fun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonad.ts
More file actions
86 lines (83 loc) · 2.48 KB
/
monad.ts
File metadata and controls
86 lines (83 loc) · 2.48 KB
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
import type { $, Kind, TypeClass } from "./kind.ts";
import type { Applicative } from "./applicative.ts";
import type { Chain } from "./chain.ts";
import { pipe } from "./fn.ts";
/**
* A Monad<T> is an algebra with a notion of join(aka flatten or flat) for
* a given algebraic data type T. A Monad<T> must also extend Functor<T>,
* Apply<T>, Applicative<T>, and Chain<T>. This means a Monad<T> has
* the following methods: of, ap, map, join, and chain. An intuition for
* Monad can be found by thinking about how Promise.then handles functions
* that return a new Promise.
*
* An instance of a Monad must obey the following laws:
*
* 1. Left identity: chain(f)(of(a)) === f(a)
* 2. Right identity: chain(of)(ua) === ua
*
* The original type came from [static-land](https://github.com/fantasyland/static-land/blob/master/docs/spec.md#monad)
*
* @since 2.0.0
*/
export interface Monad<U extends Kind>
extends Applicative<U>, Chain<U>, TypeClass<U> {
readonly join: <
A,
B = never,
C = never,
D = unknown,
E = unknown,
J = never,
K = never,
>(
tta: $<U, [$<U, [A, B, C], [D], [E]>, J, K], [D], [E]>,
) => $<U, [A, B | J, C | K], [D], [E]>;
}
/**
* Derive a Monad instance from of, chain, and a Kind. This is
* the simplest way to get a Monad instance when one has a
* creation function (of) and a chain function (aka flatMap or
* bind).
*
* @example
* ```ts
* import type { Kind, Out } from "./kind.ts";
* import { createMonad } from "./monad.ts";
* import { pipe } from "./fn.ts";
*
* // Create a Kind for Promise<A>
* interface KindPromise extends Kind {
* readonly kind: Promise<Out<this, 0>>;
* };
*
* // Create an of and chain function for Promise<A>
* const of = <A>(a: A): Promise<A> => Promise.resolve(a);
* const chain = <A, I>(faui: (a: A) => Promise<I>) =>
* (ua: Promise<A>): Promise<I> => ua.then(faui);
*
* // Derive a Monad for Promise
* const M = createMonad<KindPromise>({ of, chain });
*
* const result = await pipe(
* M.of((n: number) => (m: number) => n + m),
* M.ap(M.of(1)),
* M.ap(M.of(1)),
* ); // 2
* ```
*
* @experimental
*
* @since 2.0.0
*/
export function createMonad<U extends Kind>(
{ of, chain }: Pick<Monad<U>, "of" | "chain">,
): Monad<U> {
const Monad: Monad<U> = {
of,
ap: (ua) => (ufai) => pipe(ufai, chain((fab) => pipe(ua, Monad.map(fab)))),
map: (fai) => (ta) => pipe(ta, chain((a) => of(fai(a)))),
chain,
join: (uua) => pipe(uua, chain((ua) => ua)),
};
return Monad;
}