Currying is a transformation of functions that translates a function from callable as f(a, b, c) into callable as f(a)(b)(c)
process of taking a function with multiple arguments and turning it into a sequence of functions each with only a single argument
Currying doesn’t call a function. It just transforms it
function curry(f) { // curry(f) does the currying transform return function(a) { return function(b) { return f(a, b); }; };}// usagefunction sum(a, b) { return a + b;}let curriedSum = curry(sum); // currying transforms to another function but does not call sum functionalert( curriedSum(1)(2) ); // 3
Uses
We can create partial functions using currying which are useful
// assumefunction log(date, importance, message) { console.log(`[${date.getHours()}:${date.getMinutes()} [${importance}] ${message}`);}log = _.curry(log); // using lodash curry function// Now log is available in both the following forms:log(new Date(), "DEBUG", "some debug");log(new Date())("DEBUG")("some debug");// In the beginning we will create logNow which will be the partial of log with fixed first argumentlet logNow = log(new Date());// Now we will use it in the codlogNow("INFO", "message"); // [HH:mm] INFO message