-
Notifications
You must be signed in to change notification settings - Fork 7
/
free.ts
173 lines (154 loc) · 3.03 KB
/
free.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
/**
* This file contains the Free algebraic data type. Free is a data type that is
* used primarily to create a Combinable for any given data structure. It is
* useful when one wants to use combine things without deciding on a specific
* data structure to implement.
*
* @experimental
* @module Free
* @since 2.0.0
*/
import type { Kind, Out } from "./kind.ts";
import type { Combinable } from "./combinable.ts";
import { flow, pipe } from "./fn.ts";
/**
* @since 2.0.0
*/
export type Node<A> = {
readonly tag: "Node";
readonly value: A;
};
/**
* @since 2.0.0
*/
export type Link<A> = {
readonly tag: "Link";
readonly first: Free<A>;
readonly second: Free<A>;
};
/**
* @since 2.0.0
*/
export type Free<A> = Node<A> | Link<A>;
/**
* @since 2.0.0
*/
export interface KindFree extends Kind {
readonly kind: Free<Out<this, 0>>;
}
/**
* @since 2.0.0
*/
export function node<A>(value: A): Free<A> {
return { tag: "Node", value };
}
/**
* @since 2.0.0
*/
export function link<A>(
first: Free<A>,
second: Free<A>,
): Free<A> {
return { tag: "Link", first, second };
}
/**
* @since 2.0.0
*/
export function isNode<A>(ua: Free<A>): ua is Node<A> {
return ua.tag === "Node";
}
/**
* @since 2.0.0
*/
export function isLink<A>(ua: Free<A>): ua is Link<A> {
return ua.tag === "Link";
}
/**
* @since 2.0.0
*/
export function match<A, O>(
onNode: (value: A) => O,
onLink: (first: Free<A>, second: Free<A>) => O,
): (ua: Free<A>) => O {
return (ua) => {
switch (ua.tag) {
case "Node":
return onNode(ua.value);
case "Link":
return onLink(ua.first, ua.second);
}
};
}
/**
* @since 2.0.0
*/
export function combine<A>(
second: Free<A>,
): (first: Free<A>) => Free<A> {
return (first) => ({ tag: "Link", first, second });
}
/**
* @since 2.0.0
*/
export function wrap<A>(a: A): Free<A> {
return node(a);
}
/**
* @since 2.0.0
*/
export function map<A, I>(
fai: (a: A) => I,
): (ua: Free<A>) => Free<I> {
const go: (ua: Free<A>) => Free<I> = match(
flow(fai, node),
(first, second) => link(go(first), go(second)),
);
return go;
}
/**
* @since 2.0.0
*/
export function flatmap<A, I>(
faui: (a: A) => Free<I>,
): (ua: Free<A>) => Free<I> {
const go: (ua: Free<A>) => Free<I> = match(
faui,
(first, second): Free<I> => link(go(first), go(second)),
);
return go;
}
/**
* @since 2.0.0
*/
export function apply<A>(ua: Free<A>): <I>(ufai: Free<(a: A) => I>) => Free<I> {
return (ufai) => pipe(ufai, flatmap(flow(map, (fn) => fn(ua))));
}
/**
* @since 2.0.0
*/
export function fold<A, O>(
foldr: (value: A, accumulator: O) => O,
initial: O,
): (ua: Free<A>) => O {
// :(
let result = initial;
const go: (ua: Free<A>) => O = match(
(value) => {
result = foldr(value, result);
return result;
},
(first, second) => {
go(first);
return go(second);
},
);
return go;
}
/**
* @since 2.0.0
*/
export function getCombinable<A>(): Combinable<Free<A>> {
return { combine };
}
// TODO: Showable, Sortable, Traversable
//