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
/
tree.ts
176 lines (145 loc) · 5 KB
/
tree.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
import type * as HKT from "./hkt.ts";
import type * as TC from "./type_classes.ts";
import * as A from "./array.ts";
import { createDo } from "./derivations.ts";
import { apply, flow, identity, pipe } from "./fns.ts";
/*******************************************************************************
* Types
******************************************************************************/
export type Forest<A> = ReadonlyArray<Tree<A>>;
export type Tree<A> = {
readonly value: A;
readonly forest: Forest<A>;
};
/*******************************************************************************
* Kind Registration
******************************************************************************/
export const URI = "Tree";
export type URI = typeof URI;
declare module "./hkt.ts" {
// deno-lint-ignore no-explicit-any
export interface Kinds<_ extends any[]> {
[URI]: Tree<_[0]>;
}
}
/*******************************************************************************
* Optimizations
******************************************************************************/
// deno-lint-ignore no-explicit-any
const _concat = A.getMonoid<Tree<any>>().concat;
const _draw = (indentation: string, forest: Forest<string>): string => {
let r = "";
const len = forest.length;
let tree: Tree<string>;
for (let i = 0; i < len; i++) {
tree = forest[i];
const isLast = i === len - 1;
r += indentation + (isLast ? "└" : "├") + "─ " + tree.value;
r += _draw(indentation + (len > 1 && !isLast ? "│ " : " "), tree.forest);
}
return r;
};
const _make = <A>(value: A) =>
(forest: Forest<A>): Tree<A> => ({ value, forest });
/*******************************************************************************
* Constructors
******************************************************************************/
export const make = <A>(value: A, forest: Forest<A> = A.empty()): Tree<A> => ({
value,
forest,
});
/*******************************************************************************
* Modules
******************************************************************************/
export const Functor: TC.Functor<URI> = {
map: (fab) =>
(ta) => ({
value: fab(ta.value),
forest: ta.forest.map(Functor.map(fab)),
}),
};
export const Apply: TC.Apply<URI> = {
ap: (tfab) => (ta) => pipe(tfab, Monad.chain(flow(Functor.map, apply(ta)))),
map: Functor.map,
};
export const Applicative: TC.Applicative<URI> = {
of: make,
ap: Apply.ap,
map: Functor.map,
};
export const Chain: TC.Chain<URI> = {
ap: Apply.ap,
map: Functor.map,
chain: (fatb) =>
(ta) => {
const { value, forest } = fatb(ta.value);
return {
value,
forest: _concat(forest)(ta.forest.map(Monad.chain(fatb))),
};
},
};
export const Monad: TC.Monad<URI> = {
of: make,
ap: Apply.ap,
map: Functor.map,
join: Chain.chain(identity),
chain: Chain.chain,
};
export const Traversable: TC.Traversable<URI> = {
map: Functor.map,
reduce: (faba, b) =>
(ta) => {
let r = faba(b, ta.value);
const len = ta.forest.length;
for (let i = 0; i < len; i++) {
r = Traversable.reduce(faba, r)(ta.forest[i]);
}
return r;
},
// TODO Clean up this implementation
traverse: (AP) =>
(faub) =>
(ta) => {
const traverseF = A.traverse(AP);
// deno-lint-ignore no-explicit-any
const out = (f: any) =>
// deno-lint-ignore no-explicit-any
(ta: any): any =>
pipe(
ta.forest,
// deno-lint-ignore no-explicit-any
traverseF(out(f)) as any,
AP.ap(pipe(f(ta.value), AP.map(_make))),
);
return out(faub)(ta);
},
};
/*******************************************************************************
* Module Getters
******************************************************************************/
export const getShow = <A>(S: TC.Show<A>): TC.Show<Tree<A>> => {
const show = (ta: Tree<A>): string =>
ta.forest === A.zero || ta.forest.length === 0
? `Tree(${S.show(ta.value)})`
: `Tree(${S.show(ta.value)}, [${ta.forest.map(show).join(", ")}])`;
return ({ show });
};
/*******************************************************************************
* Pipeables
******************************************************************************/
export const { of, ap, map, join, chain } = Monad;
export const { reduce, traverse } = Traversable;
export const drawForest = (forest: Forest<string>): string =>
_draw("\n", forest);
export const drawTree = (tree: Tree<string>): string =>
tree.value + drawForest(tree.forest);
export const fold = <A, B>(f: (a: A, bs: Array<B>) => B) =>
(tree: Tree<A>): B => {
const go = (tree: Tree<A>): B => f(tree.value, tree.forest.map(go));
return go(tree);
};
/*******************************************************************************
* Do Notation
******************************************************************************/
export const { Do, bind, bindTo } = createDo(Monad);