-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlist.cpp
More file actions
159 lines (132 loc) · 3.73 KB
/
list.cpp
File metadata and controls
159 lines (132 loc) · 3.73 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
#include <iostream>
#include <utility>
#include <vector>
#include <cassert>
auto List = [](auto ...xs) {
return [=](auto access) { return access(xs...); };
};
auto fmap = [](auto func) {
return [func] (auto alist) {
return alist([func](auto... xs) { return List(func(xs)...); });
};
};
auto concat = [](auto l1, auto l2) {
auto access1 = [=](auto... p) {
auto access2 = [=](auto... q) {
return List(p..., q...);
};
return l2(access2);
};
return l1(access1);
};
template <class Func>
auto flatten(Func)
{
return List();
}
template <class Func, class A, class... B>
auto flatten(Func f, A a, B... b)
{
return concat(f(a), flatten(f, b...));
}
auto flatmap = [](auto func) {
return [func](auto alist) {
return alist([func](auto... xs) { return flatten(func, xs...); });
};
};
auto where_unpleasant = [](auto func) {
return [=](auto i) {
return std::make_pair(func(i), i);
};
};
template <class A, class... B>
struct overload : overload<A>, overload<B...> {
overload(A a, B... b)
: overload<A>(a), overload<B...>(b...) {}
using overload<A>::operator ();
using overload<B...>::operator ();
};
template <class A>
struct overload<A> : A{
overload(A a)
: A(a) {}
using A::operator();
};
template <class... F>
auto make_overload(F... f) {
return overload<F...>(f...);
}
template <class LIST, class Func>
auto operator > (LIST l, Func f)
{
return fmap(f)(l);
}
template <class LIST, class Func>
auto operator >= (LIST l, Func f)
{
return flatmap(f)(l);
}
template <class M1, class M2>
void assert_equal(M1 m1, M2 m2)
{
auto to_vector = [](auto... a) { return std::vector<int> { a... }; };
assert(m1(to_vector) == m2(to_vector));
}
int main()
{
auto twice = [](int i) { return 2*i; };
auto print = [](auto i) { std::cout << i << " "; return i; };
auto triplet = [](int i) { return List(-i, 0, i); };
{
auto l1 = List(1, 2, 3, 4);
auto l2 = fmap(twice)(l1);
fmap(print)(l2);
}
std::cout << "\nMonad-preserving length = ";
auto pair = [](auto i) { return List(-i, i); };
auto count = [](auto... a) { return List(sizeof...(a)); };
{
auto l1 = List(10, 20, 30);
auto l2 = flatmap(pair)(l1);
auto l3 = l2(count);
fmap(print)(l3);
}
std::cout << std::endl;
auto len = [](auto ...z) { return sizeof...(z); };
{
auto l1 = List(10, 20, 30);
auto l2 = flatmap(pair)(l1);
std::cout << "Monad-breaking length = "
<< l2(len)
<< std::endl;
}
std::cout << "Overloaded operators\n";
List(10,20,30) >= pair > print;
std::cout << std::endl;
auto positive = [](auto i) { return i >= 0; };
auto pair_print = [](auto pair) { if(pair.first) std::cout << pair.second << " "; return pair.second; };
List(10, 20) >= pair > where_unpleasant(positive) > pair_print; // prints 10, 20
std::cout << std::endl;
auto test =
make_overload([](int i) { std::cout << "int = " << i << std::endl; },
[](double d) { std::cout << "double = " << d << std::endl; });
test(10); // int
test(9.99); // double
auto int_or_string =
make_overload([](int i) { return 5*i; },
[](std::string s) { return s+s; });
List(10, "ab") > int_or_string > print;
std::cout << std::endl;
auto M = List(11);
std::cout << "Monad law (left identity)\n";
assert_equal(flatmap(pair)(M), pair(11));
assert_equal(M >= pair, pair(11));
std::cout << "Monad law (right identity)\n";
assert_equal(flatmap(List)(M), M);
assert_equal(M >= List, M);
std::cout << "Monad law (associativity)\n";
assert_equal(flatmap(triplet)(flatmap(pair)(M)),
flatmap([=](auto x) { return flatmap(triplet)(pair(x)); })(M));
assert_equal(M >= pair >= triplet,
M >= [=](auto x) { return pair(x) >= triplet; });
}