1

So JavaScript's bind supports currying, but most people use some other library like lodash or ramda to do currying.

From first impression It seems like bind supports context changing, since that is it's intended role, e.g.

var replaceTest = replace.bind(this, 'test')

replaceTest('beta', environments)

The other curry methods don't have this.

var replace = _.curry(function (a, b, list) {
  // implementation
})
var replaceTest = replace('test')

replaceTest('beta', environments)

So are there any other difference between the two methods? Mainly I want to know if it makes sense to use one of those implementations when there is a native implementation present.

3

bind() doesn't actually do currying. What it does is partial application. lodash and rambda have a method called _.partial() that does partial application without setting the context object.

A function f is curried if calling f with one or more arguments is equivalent to calling f with one argument, then calling the return value with the next argument, and so on until you run out of arguments. Currying can be implemented with partial application, but it's a different thing.

If you genuinely want a curried function, it's probably best to use one of these libraries rather than roll your own. If all you want is partial application, bind() should be good enough, but if you're already using a library like lodash or rambda I'd consider using _.partial(). Also, it's worth noting that currying doesn't work so well with variadic functions.

P.S. This blog post does a much better job explaining it.

  • Glad I could help. – Ixrec Jul 21 '15 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.