Search code examples
javascriptecmascript-6bindcurryingpartial-application

Partial functions with bind


So recently I discovered that you can do partial functions/currying with js using bind. For example:

const foo = (a, b, c) => (a + (b / c))
foo.bind(null, 1, 2) //gives me (c) => (1 + (2 / c))

However this only works if the parts you want to curry are in order. What if I wanted to achieve the following using bind?

(b) => (1 + (b / 2))

Tried various solutions such as:

foo.bind(null, 1, null, 2)

Any ideas? Is it possible to accomplish this with vanilla es6?


Solution

  • You could use a wrapper for a reordering of arguments.

    const
        foo = (a, b, c) => a + b / c,
        acb = (a, c, b) => foo(a, b, c);
    
    console.log(acb.bind(null, 1, 2)(5));