forked from baetheus/fun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconst.ts
More file actions
90 lines (71 loc) · 2.26 KB
/
const.ts
File metadata and controls
90 lines (71 loc) · 2.26 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
87
88
89
90
import type { Applicative } from "./applicative.ts";
import type { Apply } from "./apply.ts";
import type { Bifunctor } from "./bifunctor.ts";
import type { Contravariant } from "./contravariant.ts";
import type { Functor } from "./functor.ts";
import type { Kind, Out } from "./kind.ts";
import type { Monoid } from "./monoid.ts";
import type { Ord } from "./ord.ts";
import type { Semigroup } from "./semigroup.ts";
import type { Eq } from "./eq.ts";
import type { Show } from "./show.ts";
import { identity } from "./fn.ts";
export type Const<E, _ = never> = E;
export interface KindConst extends Kind {
readonly kind: Const<Out<this, 1>, Out<this, 0>>;
}
export interface KindRightConst<B> extends Kind {
readonly kind: Const<B, Out<this, 0>>;
}
export function make<E, A = never>(e: E): Const<E, A> {
return e;
}
export function map<A, I>(
_fai: (a: A) => I,
): <B = never>(ta: Const<B, A>) => Const<B, I> {
return identity;
}
export function contramap<A, I>(
_fai: (a: A) => I,
): <B = never>(ta: Const<B, I>) => Const<B, A> {
return identity;
}
export function bimap<A, B, I, J>(
fbj: (b: B) => J,
_fai: (a: A) => I,
): (tab: Const<B, A>) => Const<J, I> {
return (tab) => make(fbj(tab));
}
export function mapLeft<B, J>(
fbj: (b: B) => J,
): <A = never>(tab: Const<B, A>) => Const<J, A> {
return bimap(fbj, identity);
}
export const getShow = <E, A>(S: Show<E>): Show<Const<E, A>> => ({
show: (c) => `Const(${S.show(c)})`,
});
export const getEq: <E, A>(
E: Eq<E>,
) => Eq<Const<E, A>> = identity;
export const getOrd: <E, A>(O: Ord<E>) => Ord<Const<E, A>> = identity;
export const getSemigroup: <E, A>(
S: Semigroup<E>,
) => Semigroup<Const<E, A>> = identity;
export const getMonoid: <E, A>(
M: Monoid<E>,
) => Monoid<Const<E, A>> = identity;
export const getApply = <E>(
S: Semigroup<E>,
): Apply<KindRightConst<E>> => ({
map: (_) => (ta) => ta,
ap: (tfai) => (ta) => make(S.concat(ta)(tfai)),
});
export const getApplicative = <E>(
M: Monoid<E>,
): Applicative<KindRightConst<E>> => ({
of: () => make(M.empty()),
...getApply(M),
});
export const FunctorConst: Functor<KindConst> = { map };
export const ContravariantConst: Contravariant<KindConst> = { contramap };
export const BifunctorConst: Bifunctor<KindConst> = { bimap, mapLeft };