by lrichardson on 4/29/14, 4:01 PM with 23 comments
by chriswarbo on 4/29/14, 5:13 PM
I've been using my own JS currying implementation in a few projects[1], which pretty much matches the implementation in the article: count the function's parameter number and build up argument lists in closures until we have enough to call the function, then pass them all in.
However, when I was implementing the same thing in PHP[2] I discovered a nice alteration we can make: instead of passing all arguments to the curried function, we should only pass the minimum number. If we have any left over, we should pass them to the return value. This lets us chain even more things together[3]. For example:
// "c" is our currying function
var id = c(function(x) { return x; });
var triple = c(function(x, y, z) { return x + y + z; });
// These work with both currying functions
id(triple)('Hell', 'o Wor', 'ld');
id(triple)('Hell')('o Wor', 'ld');
id(triple)('Hell', 'o Wor')('ld');
id(triple)('Hell')('o Wor')('ld');
// These only work with the altered version
id(triple, 'Hell', 'o Wor', 'ld');
id(triple, 'Hell')('o Wor', 'ld');
id(triple, 'Hell', 'o Wor')('ld');
id(triple, 'Hell')('o Wor')('ld');
[1] http://chriswarbo.net/index.php?page=news&type=view&id=curry...[2] http://chriswarbo.net/index.php?page=news&type=view&id=admin...
[3] http://chriswarbo.net/index.php?page=news&type=view&id=admin...
by platz on 4/29/14, 8:53 PM
Otherwise it will be hard to make the types line up.
Also there are functions in javascript which do different things depending on what the type of the argument is (or do different things depending on how many arguments are passed in; those kinds of functions always have irked me), so it would be hard to document exactly what the "type" of those functions are.
by ceedan on 4/29/14, 7:07 PM
uhhh, 2 wrongs don't make it right?
"It is very unlikely that you will notice a performance hit at all."
Until you do..
--
I could never justify using this stuff at work. It's inefficient, unfamiliar to most JS developers and even if it did pass a peer review, nobody else would actually want to maintain it.
by taylodl on 4/30/14, 2:00 AM
I never provided a currying function, thinking there were so many implementations easily available - but I've never seen one as good as what's presented here!
by ripter on 4/29/14, 10:02 PM
What is the difference between curry and partial? Is a partial just a curry that hasn't filled in all the arguments yet?
by rpwverheij on 4/29/14, 4:23 PM
by batmansbelt on 4/29/14, 4:21 PM