Currying with bind()

We can find a solution to currying by using the bind() method. This allows us to fix one argument (or more, if need be; we won't be needing to do that here, but later on we will use it) and provide a function with that fixed argument. Of course, many libraries (such as Lodash, Underscore, Ramda, and others) provide this functionality, but we want to see how to implement that by ourselves.

Read more on .bind() at https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind—it will be useful since we'll take advantage of this method at other points in this chapter.

Our implementation is quite short but will require some explanation:

const curryByBind = fn =>  fn.length === 0 ? fn() : p => curryByBind(fn.bind(null, ...

Get Mastering JavaScript Functional Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.