Sometimes it would make sense to invoke some functions partially. It could be that that function requires a few parameters, but we know just a couple of them. Later, we’d be able to provide the rest of the parameters. An example of that could be a function that requires some configuration. If the configuration never changes throughout the lifetime of the app, it makes sense to provide that configuration once. Later, we could use the function without providing the configuration every time. An example could be some function that retrieves data from a database. The connection string will probably not change during runtime.
The function above requires us to provide both parameters at once. We could change it to a HOF, like this:
"Hello" is captured in a closure. The function is now in a curried
There is a way to turn multi-parameter functions into partial functions, with
the help of an implementation of the
Apply function which relies on closures:
Partial function application allows us to create very generic functions that may be turned into more specific ones. The consuming code does not need to know that it’s invoking a partial function.
Currying is similar to partial application. The difference is:
- in partial application we provide some parameters of the function to get a new function that expects the rest of the parameters
- in currying we transform a function into a curried function (no need to provide any parametes while transforming)
Here’ an example of an implementation of a
It’s very similar to
Curry does not expect any
t1. It just
transforms the original function into a curried one.