Skip to content

Instantly share code, notes, and snippets.

@Superstar64
Last active May 17, 2018 13:26
Show Gist options
  • Save Superstar64/9ed01cd2aa69b6613765ca51ebd9f0a8 to your computer and use it in GitHub Desktop.
Save Superstar64/9ed01cd2aa69b6613765ca51ebd9f0a8 to your computer and use it in GitHub Desktop.
IO Monads in C++
#include <iostream>
#include <string>
#include <tuple>
auto getString = [] {
return []() {
std::string string;
getline(std::cin, string);
return string;
};
};
auto putString = [](std::string string) {
return [string]() {
std::cout << string << "\n";
return std::make_tuple();
};
};
template <typename Monad, typename Functor>
constexpr auto operator>>(Monad monad, Functor functor)
{
return [monad, functor] {
return functor(monad())();
};
}
auto result = getString() >> [](std::string str) {
return getString() >> [str](std::string str2) {
return putString(str + str2);
};
};
int main()
{
result();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment