Last active
December 20, 2015 14:49
-
-
Save plonk/6149963 to your computer and use it in GitHub Desktop.
Clojure の ->> の C++ によるイミテーション
This file contains hidden or 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 <functional> | |
| #include <vector> | |
| #include <string> | |
| #include <iostream> | |
| #include <cassert> | |
| using namespace std; | |
| vector<string> split(const string& sep, const string& in); | |
| string join(const string& sep, const vector<string>& in); | |
| template <class Out, class D, class F> | |
| Out chain(D data, F f) | |
| { | |
| return f(data); | |
| } | |
| template <class Out, class D, class F, class... G> | |
| Out chain(D data, F f, G... rest) | |
| { | |
| return chain<Out>(f(data), rest...); | |
| } | |
| int main() | |
| { | |
| using namespace std::placeholders; | |
| string out1 = join("_", split(",", "a,b,c")); | |
| assert(out1 == "a_b_c"); | |
| string out2 = chain<string>("a,b,c", bind(split, ",", _1), bind(join, "_", _1)); | |
| assert(out2 == out1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment