Curry is cool!

Every time I learn a new language, the joy of learning the new language makes me feel so good. More important, the  new thinking style and new patterns behind the language teaches me a lot.

Recently I’ve involved in a project written in Groovy. Learning it was fun. Here, I want to share some points of this language which I like.

Groovy is dynamic

Groovy is dynamic language over the JVM. It’s heavily based on dynamic features. So managing the risk of dynamic languages and its error-proneness was a good experience while learning Groovy.

Groovy is functional

One of its design goals was o fill the gap of functional languages in JVM stack. In addition to being a functional language, it has lots of syntactic sugars to make it more fun.

Curry: a new pattern of calculus

This one surprises me a lot, as it revealed a new thinking style and pattern in my mind.

While learning Groovy, I’ve got familiar with Curry Calculus which I haven’t heard about. Maybe I used some kinds of Curries in JavaScript before, but I didn’t see it as something clear and powerful like curry concept.

Describing it in brief, it’s a way to reduce the parameters of a function and generating a new function which is simpler to work with.

Consider f(x,y) = x+y as a function. g(y) = f(5,y) is a reduced version of f. It always adds 5 to y. But why always 5? A curry is a function to make this process more flexible. In fact a curry itself, is a function which accepts a function as input and return our desired function as output.

Consider h(5), as a function which returns our famous function g, just like above: g(y) = f(5,y). Let’s give g a new name: FiveAdder. So FiveAdder(y)  is equivalent to h(5). It’s interesting that h can produce more functions for us:

h(10)     returns      TenAdder(y) = f(10,y)

h(z)       returns      ZAdder(z) = f(5,y)

In this example we say that h is a curried version of f.

So curry is a function that returns a new function, based on its input (which is a function). In this type of calculus functions are being evaluated by sequential currying. For example to evaluate f(2,3), first replace x with 2 and create a new function which accepts one argument, then replace its argument with 3 to create a new function that just works.

There’s a great article about Currying at Wikipedia, check it if you want to know more about this type of calculus.

About the author

Mehran Davoudi

A former technology geek! Currently a mentor and software architect, working as a consultant for companies with large scale software development.
Why dvd? Read about me (uɒɹɥəɯ)!

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *