const pApply = (fn, ...cache) => (...args) => {
const all = cache.concat(args);
return all.length >= fn.length ? fn(...all) : pApply(fn, ...all);
};
Create a function like so:
const add = pApply((a, b, c) => a + b + c);
Use it as follows:
add(1);
// => function
add(1, 2);
// => function
add(1, 2, 3);
// => 6
add(1)(2)(3);
// => 6
Great approach! A tiny issue though:
according to this great article by Eric Elliot https://medium.com/javascript-scene/curry-or-partial-application-8150044c78b8 , this could be named as
ES6 partial applied function
rather thatcurried
. Curry is supposed to point to a function that is accepting it's collection of arguments in a series of calls, each accepting only 1 parameter!