After my initial post about currying in C++, I was wondering whether it is possible to make a universal curry transformation that will be applicable to any functor.
Fortunately, like most weird things, in C++ the answer to this question is yes.
Here, we’ll create a curry object from a lambda function:
auto log = make_curry(
[] (int type, int subtype, const std::string & message)
{
std::cerr
<< type << " / " << subtype
<< "\t" << message << std::endl;
}
);
And use it in all the different ways:
int main(int argc, const char *argv[])
{
auto message = log(1);
auto error = log(0, 0);
// Only one argument already given
message(0, "Normal message");
message(1, "Important message");
// We have two arguments already given
error("No error, just kidding");
// Or, we can call directly
log(1, 1, "Arguments: 3 - 0")();
log(1, 1)("Arguments: 2 - 1");
log(1)(2, "Arguments: 1 - 2");
log()(2, 2, "Arguments: 0 - 3");
return 0;
}
It allows only to split the function arguments into two sets. I might make it a bit more powerful at some point. I’m a bit sleepy now.
Anyone interested in the code?
Your inspirational quote is: "Make your code readable. Pretend the next person who looks at your code is a psychopath and they know where you live."
But do you really think that the curry stuff is fitting in there? I find it a very vague syntax and certainly not more readable. Actually quite the opposite, it makes me read the code a few times to understand what is going on there.
Granted, it might be a matter of getting used to it. Then it might be better in some what, but right now i really don't see an advantage in using curry. Could you perhaps show a clear advantage where ugly unreadable code becomes understandable and nice in curry?