-
Notifications
You must be signed in to change notification settings - Fork 7
/
tree.ts
303 lines (272 loc) · 6.98 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
* This file contains a collection of utilities and algebraic structure
* implementations for Tree.
*
* @module Tree
* @since 2.0.0
*/
import type { $, Kind, Out } from "./kind.ts";
import type { Applicable } from "./applicable.ts";
import type { Comparable, Compare } from "./comparable.ts";
import type { Bind, Flatmappable, Tap } from "./flatmappable.ts";
import type { Foldable } from "./foldable.ts";
import type { BindTo, Mappable } from "./mappable.ts";
import type { Showable } from "./showable.ts";
import type { Traversable } from "./traversable.ts";
import type { Wrappable } from "./wrappable.ts";
import { TraversableArray } from "./array.ts";
import { fromCompare } from "./comparable.ts";
import { createBind, createTap } from "./flatmappable.ts";
import { createBindTo } from "./mappable.ts";
import { flow, pipe } from "./fn.ts";
/**
* A Forest is an array of Trees.
*
* @since 2.0.0
*/
export type Forest<A> = ReadonlyArray<Tree<A>>;
/**
* A Tree is a node with a single value and a Forest of children.
*
* @since 2.0.0
*/
export type Tree<A> = {
readonly value: A;
readonly forest: Forest<A>;
};
/**
* AnyTree is useful as an extends constraint on a generic type.
*
* @since 2.0.0
*/
// deno-lint-ignore no-explicit-any
export type AnyTree = Tree<any>;
/**
* TypeOf is a type extractor that returns the inner type A of a Tree<A>.
*
* @since 2.0.0
*/
export type TypeOf<T> = T extends Tree<infer A> ? A : never;
/**
* KindTree is the Kind implementation for a Tree.
*
* @since 2.0.0
*/
export interface KindTree extends Kind {
readonly kind: Tree<Out<this, 0>>;
}
/**
* This is an internal draw function used to draw an ascii representation of a
* tree.
*
* @since 2.0.0
*/
function drawString(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 += drawString(
indentation + (len > 1 && !isLast ? "│ " : " "),
tree.forest,
);
}
return r;
}
/**
* This is a constructor function, taking a single value A and optionally an
* array of Tree<A> and returning a Tree<A>.
*
* @since 2.0.0
*/
export function tree<A>(value: A, forest: Forest<A> = []): Tree<A> {
return ({ value, forest });
}
/**
* The wrap function for Wrappable<KindTree>.
*
* @since 2.0.0
*/
export function wrap<A>(value: A, forest: Forest<A> = []): Tree<A> {
return tree(value, forest);
}
/**
* The map function for Mappable<KindTree>.
*
* @since 2.0.0
*/
export function map<A, I>(fai: (a: A) => I): (ta: Tree<A>) => Tree<I> {
return (ta) => wrap(fai(ta.value), ta.forest.map(map(fai)));
}
/**
* The flatmap function for Flatmappable<KindTree>.
*
* @since 2.0.0
*/
export function flatmap<A, I>(
fati: (a: A) => Tree<I>,
): (ta: Tree<A>) => Tree<I> {
return (ta) => {
const { value, forest } = fati(ta.value);
return wrap(value, [...ta.forest.map(flatmap(fati)), ...forest]);
};
}
/**
* The apply function for Applicable<KindTree>.
*
* @since 2.0.0
*/
export function apply<A>(ua: Tree<A>): <I>(tfai: Tree<(a: A) => I>) => Tree<I> {
return (ufai) => pipe(ufai, flatmap(flow(map, (fn) => fn(ua))));
}
/**
* The fold function for Foldable<KindTree>.
*
* @since 2.0.0
*/
export function fold<A, O>(
foao: (o: O, a: A) => O,
o: O,
): (ta: Tree<A>) => O {
const foldr = (result: O, tree: Tree<A>) => fold(foao, result)(tree);
return (ta) => TraversableArray.fold(foldr, foao(o, ta.value))(ta.forest);
}
/**
* The traverse function for Traversable<KindTree>.
*
* @since 2.0.0
*/
export function traverse<V extends Kind>(
V: Applicable<V>,
): <A, I, J = never, K = never, L = unknown, M = unknown>(
favi: (a: A) => $<V, [I, J, K], [L], [M]>,
) => (ta: Tree<A>) => $<V, [Tree<I>, J, K], [L], [M]> {
const traverseArray = TraversableArray.traverse(V);
return <A, I, J = never, K = never, L = unknown, M = unknown>(
favi: (a: A) => $<V, [I, J, K], [L], [M]>,
): (ua: Tree<A>) => $<V, [Tree<I>, J, K], [L], [M]> => {
const pusher: (i: I) => (is: Forest<I>) => Tree<I> = (i) => (fs) =>
tree(i, fs);
const wrappedPusher = V.wrap<typeof pusher, J, K, L, M>(pusher);
const traverseTree = (ua: Tree<A>): $<V, [Tree<I>, J, K], [L], [M]> =>
pipe(
wrappedPusher,
V.apply(favi(ua.value)),
V.apply(traverseForest(ua.forest)),
);
const traverseForest = traverseArray(traverseTree);
return traverseTree;
};
}
/**
* The unwrap function for Unwrappable<KindTree>
*
* @since 2.0.0
*/
export function unwrap<A>({ value }: Tree<A>): A {
return value;
}
/**
* Converts a Forest<string> into a tree representation.
*
* @since 2.0.0
*/
export function drawForest(forest: Forest<string>): string {
return drawString("\n", forest);
}
/**
* Converts a Tree<string> into a tree representation.
*
* @since 2.0.0
*/
export function drawTree(tree: Tree<string>): string {
return tree.value + drawForest(tree.forest);
}
/**
* The match function is a recursive fold that collapses a Tree<A> into a single
* value I. It does this from the head of the Tree first.
*
* @since 2.0.0
*/
export function match<A, I>(
fai: (a: A, is: Array<I>) => I,
): (ta: Tree<A>) => I {
const go = (tree: Tree<A>): I => fai(tree.value, tree.forest.map(go));
return go;
}
/**
* Create an instance of Comparable<Tree<A>> from an instance of Comparable<A>.
*
* @since 2.0.0
*/
export function getComparableTree<A>(
{ compare }: Comparable<A>,
): Comparable<Tree<A>> {
const go: Compare<Tree<A>> = (second) => (first) => {
if (first === second) {
return true;
} else if (
compare(second.value)(first.value) &&
first.forest.length === second.forest.length
) {
return first.forest.every((tree, index) =>
go(second.forest[index])(tree)
);
} else {
return false;
}
};
return fromCompare(go);
}
/**
* Get an instance of Showable<Tree<A>> from an instance of Showable<A>.
*
* @since 2.0.0
*/
export function getShowable<A>({ show }: Showable<A>): Showable<Tree<A>> {
return ({ show: flow(map(show), drawTree) });
}
/**
* @since 2.0.0
*/
export const ApplicableTree: Applicable<KindTree> = { apply, map, wrap };
/**
* @since 2.0.0
*/
export const FlatmappableTree: Flatmappable<KindTree> = {
apply,
map,
flatmap,
wrap,
};
/**
* @since 2.0.0
*/
export const FoldableTree: Foldable<KindTree> = { fold };
/**
* @since 2.0.0
*/
export const MappableTree: Mappable<KindTree> = { map };
/**
* @since 2.0.0
*/
export const TraversableTree: Traversable<KindTree> = { map, fold, traverse };
/**
* @since 2.0.0
*/
export const WrappableTree: Wrappable<KindTree> = { wrap };
/**
* @since 2.0.0
*/
export const tap: Tap<KindTree> = createTap(FlatmappableTree);
/**
* @since 2.0.0
*/
export const bind: Bind<KindTree> = createBind(FlatmappableTree);
/**
* @since 2.0.0
*/
export const bindTo: BindTo<KindTree> = createBindTo(MappableTree);