## Notes
Transforms a function from `f(a, b, c)` to `f(a)(b)(c)`
Create a helper function, `curry()`, that performs currying for two arguments, i.e. `f(a,b) = curry(f) = f(a)(b)`
```javascript
function curry(f) {
return function(a) {
return function(b) {
return f(a,b);
}
}
}
// usage
function sum(a, b) {
return a+b;
}
let curriedSum = curry(sum);
curriedSum(1)(2); // 3
```
## Why?
Currying helps extend functionality/function signatures.
```javascript
function log(date, importance, message) {
alert(`[${date.getHours(0)}:${date.getMinutes()}] [${importance}] ${message}`);
}
// currying with lodash
log = _.curry(log);
// works as normal
log(new Date(), "INFO", "some info"); // log(a, b, c)
// works in curried format
log(new Date(0))("INFO")("some info"); // log(a)(b)(c)
// make a convenience function
let logNow = log(new Date());
logNow("DEBUG", "debug info");
// logNow is a "partially applied function", aka "partial"
let debugNow = logNow("DEBUG"); // debugNow(...)
```
## Advanced Curry Implementation
```javascript
function curry(func) {
return function curried(...args) {
if (args.length >= func.length) {
return func.apply(this, args);
} else {
return function(...args2) {
return curried.apply(this, args.concat(args2));
}
}
}
}
```
In the conditional:
1. if provided number of `args` is same or greater than the original function, then just pass it to the function and call it
2. otherwise get a *partial*. Don't call `func` just yet
## References
- [[JavaScript Study MOC]]