-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.cpp
More file actions
128 lines (100 loc) · 3.97 KB
/
io.cpp
File metadata and controls
128 lines (100 loc) · 3.97 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
#include <iostream>
#include <functional>
#include <string>
// This is a hack to create a "void type" that we can specify as a template
// value.
// Otherwise calling the default contructor with IO<void>() will attempt to set
// the type of IO.value as type "void"
// See functions greet_user() and show_better_number()
class MyVoid {
// Overloading << operator for ease of printing
friend std::ostream& operator<<(std::ostream &out, MyVoid const& v) {
out << "";
return out;
}
};
template <typename A> class IO {
private:
A value;
public:
// wrap/return/unit
IO() {}
IO(A value) : value(value) {}
// bind/flatmap
// As an operator
template <typename B>
IO<B> operator>>=(std::function<IO<B>(A)> f) const {
return f(value);
}
// Overloading << operator for ease of printing
friend std::ostream& operator<<(std::ostream &out, IO<A> const& io) {
out << "IO (" << io.value << ")";
return out;
}
};
int main(const int argc, char const *argv[]) {
std::function<IO<std::string>(void)> get_user_name = [] (void) {
std::string user_input;
std::cout << "Hello, what is your name?\n";
std::cin >> user_input;
return user_input;
};
std::function<IO<MyVoid>(std::string)> greet_user = [] (std::string user) {
std::cout << "Pleased to meet you, " << user << "!\n";
return IO<MyVoid>();
};
get_user_name() >>= greet_user;
// ----------------------- More intricate example ------------------------
// Obviously, this is a very unsafe function with no sanitisation!
std::function<IO<int>(void)> get_favourite_number = [] (void) {
int user_input;
std::cout << "What is your favourite number?\n";
std::cin >> user_input;
return IO<int>(user_input);
};
// *gasp* a pure function??
std::function<int(int)> better_number = [] (int n) {
return n + 1;
};
std::function<IO<MyVoid>(int, int)> show_better_number = [] (int n, int better) {
std::cout << "Personally, I think " << better << " is a better number than " << n << '\n';
return IO<MyVoid>();
};
/*
This is clunky because C++'s type-system was not designed with this in
mind :P
If we could remove the superfluous type information and lambda captures,
it would look like:
get_favourite_number() >>=
((int n) {
return IO<int>(better_number(n)) >>=
((int better) {
return show_better_number(n, better);
});
});
Functional languages like Haskell provide syntactic sugar called
"do" notation that is then transformed into the above bindings. If it
were implemented in C++ it, might look something similar to:
do {
IO<int> n = get_favourite_number();
IO<int> better = IO<int>(better_number(n));
show_better_number(n, better);
};
*/
// Have to provide the lambdas with explicit compile-time typing using
// std::function
const auto io = get_favourite_number() >>= std::function<IO<MyVoid>(int)> ([&]
(int n) {
// Because better_number() is a pure function
// i.e. not wrapped in a monad, it has no
// associated bind() function.
// It needs to be wrapped before sending it's
// result further down the pipeline:
return IO<int>(better_number(n)) >>= std::function<IO<MyVoid>(int)> ([&]
(int better) {
return show_better_number(n, better);
});
});
std::cout << "End value of computation is " << io << '\n';
return 0;
}