Last active
August 29, 2015 14:00
-
-
Save FrankHB/11103657 to your computer and use it in GitHub Desktop.
C++14 Currying test
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
// clang++ -std=c++1y a.cc -o a.exe | |
#include <cstdio> | |
void seq(){} | |
template<class T, class... P> void seq(T arg, P... args){ static_cast<void>(arg()); seq(args...); } | |
auto curry = [](auto f){return [f](auto... x){return [=](auto y){return f(x..., y); }; }; }; | |
auto make_seq = [](auto f){return [f](auto... x){seq([=]{return f(x);}...); }; }; | |
auto seq_curry = [](auto f){return curry(make_seq(f)); }; | |
template<size_t N> | |
struct seq_curry_n | |
{ | |
template<class F> | |
static auto ff(F f){ return seq_curry_n<N - 1>::ff(curry(f)); }; | |
}; | |
template<> | |
struct seq_curry_n<0> | |
{ | |
template<class F> | |
static auto ff(F f){ return make_seq(f); }; | |
}; | |
void fun(int x) | |
{ | |
printf("%d;", x); | |
} | |
int main() | |
{ | |
using namespace std; | |
fun(1); fun(2); fun(3); fun(4); putchar('\n'); | |
seq_curry(fun)(1)(2); putchar('\n'); | |
//seq_curry_n<2>::ff(fun)(1)(2);putchar('\n'); | |
curry(curry(make_seq(fun)))(1)(2)(3); | |
curry(curry(curry(make_seq(fun))))(1)(2)(3)(4); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment