-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09.monoid-examples.js
More file actions
67 lines (52 loc) · 931 Bytes
/
09.monoid-examples.js
File metadata and controls
67 lines (52 loc) · 931 Bytes
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
/**
* Monoid examples
*
*/
const Sum = x =>
({
x,
concat: ({ x: y }) => Sum(x + y)
});
Sum.empty = () => Sum(0);
const Product = x =>
({
x,
concat: ({ x: y }) => Produce(x * y)
});
Product.empty = () => Product(1);
const Any = x =>
({
x,
concat: ({ x: y }) => Any(x || y)
});
Any.empty = () => Any(false);
const All = x =>
({
x,
concat: ({ x: y }) => All(x && y)
});
All.empty = () => All(true);
const Max = x =>
({
x,
concat: ({ x: y }) => Max(x > y ? x : y)
});
Max.empty = () => Max(-Infinity);
const Min = x =>
({
x,
concat: ({ x: y }) => Min(x < y ? x : y)
});
Min.empty = () => Min(Infinity);
const Left = x => ({
map: f => Left(x),
fold: (f, g) => f(x),
concat: o => Left(x)
});
const Right = x => ({
map: f => Right(f(x)),
fold: (f, g) => g(x),
concat: o =>
o.fold(e => Left(e),
r => Right(x.concat(r)))
});