No, this is not about a dish originating in the cuisine of the Indian Subcontinent. It’s about Groovy Closures.
Currying is the Groovy’s partial function application making clones of functions, fixing values for some of the parameters and producing new functions with smaller arity* – in other words: new function accepting less argument(s).
It’s possible to fix one or more parameters depending on the number of arguments you will use for the
curry(). The simple rules is that the parameters are bound from left to right.
“In Groovy, currying refers to the concept of partial application. It does not correspond to the real concept of currying in functional programming because of the different scoping rules that Groovy applies on closures” – http://www.groovy-lang.org/
A bit of code
product as a function (closure) accepting two parameters. Then I’ll use
product as the building block for two new closures:
octate. Groovy makes calling a closure easy: you can either explicitly execute the
call() or use the supplied language-level of placing a set of parentheses containing any parameters after the code-block name, as in multiplyBy8
(5), for example.
As I described before Groovy
curry() actually implements partial application by manipulating closure blocks underneath but you can simulate currying by using partial application to reduce a function to a series of partially applied single argument functions, like:
volume closure computes the cubic volume of a rectangular solid using the well-known formula. So I created an
area closure (which computes a rectangle’s area) by fixing volume’s first dimension at line 3.
lengthPartialApplication uses partial application by fixing each of the first two parameters at line 4.
lengthCurried at line 5 applies currying twice to yield the same result. The difference is subtle and at the end result is the same.
Composing closures with curry
Finishing, currying in Groovy will let you to fix the value of one or more parameters of a closure returning a new closure accepting one (or more) less argument. All examples were made by left currying – there are more 2 options: right currying and index based.
If you are interested check [here].
arity*: the number of arguments or operands taken by a function or operator.