How to make functions partially applicable in Javascript

Jason Yu on March 07, 2018

TL; DR const enablePartialApplication = (fn) => (...args) => { if (args.length >= fn.length) return fn(...args); retu... [Read Full]
markdown guide

If you don't care for readability, you can write it as a tiny on-liner :

const enablePartialApplication = fn => (...args) => args.length >= fn.length ? fn(...args) : enablePartialApplication(fn.bind(null, ...args));

It nice to have such power in one line.


Call me stupid if you will (and this is clearly very clever), but what real world problem would I solve with this sort of thing?


It can be useful if you design a library and you are thinking on the proper api. It can be combined for example with fluent interface: x(a).y(b)(c) etc. So it is good only for sugar syntax.


If nothing else, it forces you to think in ways you wouldn't otherwise. I believe Larry Wall (creator of Perl) recommends Haskell as an "academic" language for this reason :)


I believe that this concept is called "Currying" : en.wikipedia.org/wiki/Currying


FP 101 - Currying vs. Partial application

Currying is converting one function of arity n into n functions of arity one.
There is no requirement that partial application be bound to a particular arity (one, in the case of currying), it simply states that if you call a function with only some of the arguments - you get back another function "binds" or "fixes" the arguments that you passed in, as demonstrated by the author here. It's a very prevalent concept in FP languages like Haskell.


Thanks for clarifying this! I did do my research before deciding what keyword to use. I also thought curry is the same as partial function application. It's great someone could verify my word choice! :D

Code of Conduct Report abuse