Created
October 14, 2016 06:02
-
-
Save vladris/03aa03f6798ebf5fab84a948d6998d26 to your computer and use it in GitHub Desktop.
Simple curry recipe
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
template <typename Func, typename Arg> | |
struct curry_t | |
{ | |
template <typename ...Args> | |
auto operator()(Args&& ...args) | |
{ | |
return _func(_arg, std::forward<Args>(args)...); | |
} | |
Func _func; | |
Arg _arg; | |
}; | |
template <typename Func, typename Arg> | |
auto curry(Func func, Arg&& arg) | |
{ | |
return curry_t<Func, Arg> { func, std::move(arg) }; | |
} | |
int add(int x, int y) | |
{ | |
return x + y; | |
} | |
int main() | |
{ | |
auto add_one = curry(add, 1); | |
cout << add_one(41) << endl; | |
auto moar_curry = curry(add_one, 41); | |
cout << moar_curry() << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment