Currying and partial function application are common concepts of the functional programming. They look similar, but have differences in realization and using. Ruby allows you to easily operate with both of them.
Partial function application
First we need to know what is really application is: it’s the process of applying
function to all of it’s arguments to return value.
Partial function application is the process of applying function to some of it’s arguments. This process creates a new function, based on the parent function, but with lower arity (with fewer arguments). So, if we have an abstract function with three arguments, then we can create a partial function with two arguments that both of this functions return the same result.
For example, we have a simple multiply function that multiplies two arguments:
But what if we want just double numbers ? Should we pass the 2 argument each time? Not really. We can use partial function application to create a new double function that takes just one argument:
Ruby has Proc#curry method that allows you to use partial function application even more simpler:
Currying is similar to partial function application concept.
Currying is the process of translating the evaluating of function with many arguments into evaluating a sequence of functions, each with exactly one parameter. So, if we have a function with two arguments: then we can translate it with transformation to return a new function with one parameter: .
For example, look back at previous multiply function. What if we want to multiply more than two arguments?
To prevent this, we can change multiply function and use Proc#curry method:
If we want to restrict arguments count, we can use arity optional argument in Proc#curry function:
You can also use curry method on plain methods, not only procs with Ruby 2.2:
Partial function application and currying are both great features of functional programming that allows you to write clean, simple and flexible functions based on anothers. You can use Ruby’s #curry method with procs or methods to write eloquent and powerful code.