Currying is a transformation of functions that translates a function from callable as f(a, b, c)
into callable as f(a)(b)(c)
.
Currying doesn’t call a function. It just transforms it.
We’ll create a helper function curry(f)
that performs currying for a two-argument f
. In other words, curry(f)
for two-argument f(a, b)
translates it into a function that runs as f(a)(b)
:
1 | function curry(f) { // curry(f) does the currying transform |
Advanced curry implementation
In case you’d like to get in to the details, here’s the “advanced” curry implementation for multi-argument functions that we could use above.
It’s pretty short:
1 | function curry(func) { |
Usage examples:
1 | function sum(a, b, c) { |
Source: