-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrecursive.lambda.cpp
More file actions
59 lines (47 loc) · 948 Bytes
/
recursive.lambda.cpp
File metadata and controls
59 lines (47 loc) · 948 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
#include <iostream>
#include <functional>
#include <sstream>
using namespace std;
auto make_fibo()
{
return [](int n) {
function<int(int)> recurse;
recurse = [&](int n) {
return n<=2 ? 1 : recurse(n-1) + recurse(n-2);
};
return recurse(n);
};
}
auto from_to = [](auto start, auto finish) {
return [=]() mutable {
if (start < finish)
return start++;
else
throw runtime_error("complete");
};
};
auto unit = [](auto x) {
return [=] { return x; };
};
auto bind_ = [](auto u) {
return [=](auto callback) {
return callback(u());
};
};
auto stringify = [](auto x) {
stringstream ss;
ss << x;
return unit(ss.str());
};
int main(int argc, char *argv[])
{
auto fibo = make_fibo();
cout << fibo(10) << endl;
auto range = from_to(0, 10);
cout << range() << endl;
cout << stringify(5)()
<< "=="
<< bind_(stringify(5))(unit)()
<< endl;
return 0;
}