Curry and Groovy

No, this is not about a dish originating in the cuisine of the Indian Subcontinent. It’s about Groovy Closures.


Yeah, science!

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” –

A bit of code

I’ll define product as a function (closure) accepting two parameters. Then I’ll use product as the building block for two new closures: quadrate and 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:

The 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.

To use volume as a building block for a code block that returns the length of a line segment, I can perform either partial application or currying.

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

I’ll create a composite code that composes two functions. Using that code block I’ll create a multiplyBy32 closure using partial application to compose the two methods together.

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.

[]’s Cassio.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s