85 points Β· by kevincennis Β· 11y ago Β· 66 comments
Another problem with currying in Javascript is that it leads to more complicated stack traces, which is unfortunate for debugging.
The OP is already using Function.length (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...), so I suppose one could roll their own arity-checking. It'd be a little out of sync with the rest of the language, though.
If you have an uncurried function for which you have not supplied sufficient arguments, and it is eagerly evaluated, you more likely see below the call site an error in the function where an expected value is undefined. If the function is curried, the meat of the function remains unevaluated, and so is sure to give you back a value that isn't the one you want. So it does seem like moving to currying in JS is likely to sometimes increase error-to-symptom distance.
Does currying even make sense for variadic functions? I can't imagine what that would look like as an implementation.
This example here is a function that sums a variadic list of non-zero numbers. The zero signals the end of the list.
Its certainly very weird though. I wouldn't recommend doing this in real programs.
function reduce(nil, cons) {
return (ary) => {
...
}
}MDN documentation contains some very illustrative examples of its use:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Can't really do that with .bind without executing it.
That being said - well written, very informative, and I very much appreciated the simple and concise definition of currying.
Thanks.
It's been a while since the first time I ever tried to do this, so it's a bit difficult to remember exactly what my initial approach was. But I definitely remember the moment where I realized I needed the `resolver` function. Initially, I think I tried to just make `curry` return something that looked like the anonymous function inside of `resolver` β which of course didn't work, because I had no way to store all of the previous arguments.
Ultimately, I think there are probably a number of good ways to do this, and I would never claim that mine is the "right" one. It's really just a representation of the way I think about things, I guess.
If you do a bit of digging, there are some other great blog posts about currying, and each one has a different approach. It's actually pretty interesting to see how different people have implemented it. For a what basically amounts to a 10-line function, there's a pretty amazing amount of diversity in the way people approach it.
function curry(fn) {
return function accum(...args) {
if(args.length >= fn.length) {
return fn.apply(null, args)
}
return accum.bind(null, ...args)
}
}[1]:
http://www.mathscribe.com/author/jqmath.html
http://www.mathscribe.com/mathscribe/jqmath-0.4.3.js
[2]:
http://www.mathscribe.com/mathscribe/jscurry-0.4.0.js
http://www.mathscribe.com/mathscribe/jscurry-documentation.t...
Like if you had to call a method like this:
sendMessage(sender, receiver, data);
a bunch of times with the same receiver but different senders, you could define:
function sendMessageToBob(sender, data) { sendMessage(sender, bob, data); }
While that works for many cases it isn't generally true. For instance:
function wtf() { return arguments[0]; }
While that may seem like a ridiculous example, the following idiom is quite common in JS programming: function accessor(value) {
if (typeof value === 'undefined') {
return getValue();
}
else {
setValue(value):
}
}
In general it's not a good idea to expect any particular value for a function's length property.So we can debate how often youβd need to curry a function, but I feel safe suggesting that writing your own curry function (or reading along with an essay that does the same with lively interest) is valuable.
Same with things like classes and mixins, there is now syntactic sugar for such things, but itβs always a good thing to have written your own MakeClass function at least once.
That was kind of my intent (learning exercise rather than super practical real-world technique) -- but I'll admit I probably could have picked a better title.
https://web.archive.org/web/20140714014530/http://hughfdjack...
Ramdajs: http://ramdajs.com/0.16/index.html
objects.map(get('id'))
and poof there goes your type safety. Optional typing (flow and TS) is making inroads into JS. If you want to bet on them (or any other JS inspecting tools), make sure you use this: objects.map(function (x) { return x.id; });
Or, in ES6 or TypeScript: objects.map(x => x.id)
This helps computers understand your code.There are infinite examples for this sort of thing in real development scenarioes
Also (possibly related), what does 'arguments' in "Array.prototype.slice.call( arguments )" resolve to, and when? I would have thought it would be evaluated when 'resolver' in invoked, and would therefore evaluate to an empty array. If so, isn't there a simpler way to make an empty array?
The immediately invoked function is really just a slightly less verbose way to do this:
function resolver() {
}
return resolver();
The reason that I gave it a name instead of using an anonymous function (as is common with immediately invoked function expressions) is because I need to be able to call it from within itself. This function is all about creating a closure where we can store all of the arguments we've received so far.As for your second point: The first time that `resolver` is called, arguments is in fact empty. But if you read a bit further, you'll see that `resolver` calls itself recursively (indirectly, via the anonymous function it returns), and does in fact pass arguments. So you can't just start off with an empty array each time. You need to make sure you're taking into account any arguments that were passed in. Does that make sense?
That doesn't let you curry functions, but it does give you partial application, which I think is actually more useful on a day-to-day basis. If you're not familiar, here's a very quick and admittedly somewhat contrived example:
function clamp( lo, hi, val ) {
return Math.max( lo, Math.min( hi, val ) );
}
var tenToTwenty = clamp.bind( null, 10, 20 );
tenToTwenty( 5 ); // 10
tenToTwenty( 15 ); // 15
tenToTwenty( 25 ); // 20
For what it's worth though, I agree that having Function#curry in the ES spec would be pretty cool.If performance matters native bind() can be a pain.
If I'm writing some super perf-sensitive code like manipulating an array of PCM data for Web Audio stuff or doing a complex canvas animation, then the savings of not using Function#bind are probably kind of important.
For most everyday code, avoiding bind() kind of feels like premature optimization to me.