-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfree.ts
More file actions
355 lines (336 loc) · 7.13 KB
/
Copy pathfree.ts
File metadata and controls
355 lines (336 loc) · 7.13 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/**
* 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";
/**
* A Node represents a single value in the Free structure.
*
* @since 2.0.0
*/
export type Node<A> = {
readonly tag: "Node";
readonly value: A;
};
/**
* A Link represents a combination of two Free structures.
*
* @since 2.0.0
*/
export type Link<A> = {
readonly tag: "Link";
readonly first: Free<A>;
readonly second: Free<A>;
};
/**
* The Free type represents either a Node (single value) or a Link (combination of two Free structures).
*
* @since 2.0.0
*/
export type Free<A> = Node<A> | Link<A>;
/**
* Specifies Free as a Higher Kinded Type, with covariant
* parameter A corresponding to the 0th index of any substitutions.
*
* @since 2.0.0
*/
export interface KindFree extends Kind {
readonly kind: Free<Out<this, 0>>;
}
/**
* Create a Node containing a single value.
*
* @example
* ```ts
* import { node } from "./free.ts";
*
* const singleValue = node(42);
* console.log(singleValue); // { tag: "Node", value: 42 }
* ```
*
* @since 2.0.0
*/
export function node<A>(value: A): Free<A> {
return { tag: "Node", value };
}
/**
* Create a Link combining two Free structures.
*
* @example
* ```ts
* import { link, node } from "./free.ts";
*
* const first = node(1);
* const second = node(2);
* const combined = link(first, second);
* console.log(combined); // { tag: "Link", first: { tag: "Node", value: 1 }, second: { tag: "Node", value: 2 } }
* ```
*
* @since 2.0.0
*/
export function link<A>(
first: Free<A>,
second: Free<A>,
): Free<A> {
return { tag: "Link", first, second };
}
/**
* Check if a Free structure is a Node.
*
* @example
* ```ts
* import { isNode, node, link } from "./free.ts";
*
* const single = node(1);
* const combined = link(node(1), node(2));
*
* console.log(isNode(single)); // true
* console.log(isNode(combined)); // false
* ```
*
* @since 2.0.0
*/
export function isNode<A>(ua: Free<A>): ua is Node<A> {
return ua.tag === "Node";
}
/**
* Check if a Free structure is a Link.
*
* @example
* ```ts
* import { isLink, node, link } from "./free.ts";
*
* const single = node(1);
* const combined = link(node(1), node(2));
*
* console.log(isLink(single)); // false
* console.log(isLink(combined)); // true
* ```
*
* @since 2.0.0
*/
export function isLink<A>(ua: Free<A>): ua is Link<A> {
return ua.tag === "Link";
}
/**
* Pattern match on a Free structure to extract values.
*
* @example
* ```ts
* import { match, node, link } from "./free.ts";
*
* const matcher = match(
* (value) => `Single value: ${value}`,
* (first, second) => `Combined: ${first} and ${second}`
* );
*
* const single = node(42);
* const combined = link(node(1), node(2));
*
* console.log(matcher(single)); // "Single value: 42"
* console.log(matcher(combined)); // "Combined: [object Object] and [object Object]"
* ```
*
* @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);
}
};
}
/**
* Combine two Free structures.
*
* @example
* ```ts
* import { combine, node } from "./free.ts";
* import { pipe } from "./fn.ts";
*
* const first = node(1);
* const second = node(2);
* const combined = pipe(first, combine(second));
*
* console.log(combined.tag); // "Link"
* ```
*
* @since 2.0.0
*/
export function combine<A>(
second: Free<A>,
): (first: Free<A>) => Free<A> {
return (first) => ({ tag: "Link", first, second });
}
/**
* Wrap a value in a Free structure.
*
* @example
* ```ts
* import { wrap } from "./free.ts";
*
* const wrapped = wrap("Hello");
* console.log(wrapped); // { tag: "Node", value: "Hello" }
* ```
*
* @since 2.0.0
*/
export function wrap<A>(a: A): Free<A> {
return node(a);
}
/**
* Apply a function to the value in a Free structure.
*
* @example
* ```ts
* import { map, node } from "./free.ts";
* import { pipe } from "./fn.ts";
*
* const free = node(5);
* const doubled = pipe(
* free,
* map(n => n * 2)
* );
*
* console.log(doubled); // { tag: "Node", value: 10 }
* ```
*
* @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;
}
/**
* Chain Free computations together.
*
* @example
* ```ts
* import { flatmap, node } from "./free.ts";
* import { pipe } from "./fn.ts";
*
* const free = node(5);
* const chained = pipe(
* free,
* flatmap(n => node(n * 2))
* );
*
* console.log(chained); // { tag: "Node", value: 10 }
* ```
*
* @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;
}
/**
* Apply a function wrapped in a Free to a value wrapped in a Free.
*
* @example
* ```ts
* import { apply, node } from "./free.ts";
* import { pipe } from "./fn.ts";
*
* const freeFn = node((n: number) => n * 2);
* const freeValue = node(5);
* const result = pipe(
* freeFn,
* apply(freeValue)
* );
*
* console.log(result); // { tag: "Node", value: 10 }
* ```
*
* @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))));
}
/**
* Fold over a Free structure to produce a single value.
*
* @example
* ```ts
* import { fold, node, link } from "./free.ts";
* import { pipe } from "./fn.ts";
*
* const free = link(node(1), node(2));
* const sum = pipe(
* free,
* fold(
* (value, acc) => value + acc,
* 0
* )
* );
*
* console.log(sum); // 3
* ```
*
* @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;
}
/**
* Create a Combinable instance for Free.
*
* @example
* ```ts
* import { getCombinable, node } from "./free.ts";
* import { pipe } from "./fn.ts";
*
* const combinable = getCombinable<number>();
* const first = node(1);
* const second = node(2);
* const combined = pipe(first, combinable.combine(second));
*
* console.log(combined.tag); // "Link"
* ```
*
* @since 2.0.0
*/
export function getCombinable<A>(): Combinable<Free<A>> {
return { combine };
}
// TODO: Showable, Sortable, Traversable
//