Last active
May 17, 2018 13:26
-
-
Save Superstar64/9ed01cd2aa69b6613765ca51ebd9f0a8 to your computer and use it in GitHub Desktop.
IO Monads in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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