Mathias Jean Johansen

Curry Your Procs

Recently, I discovered that Ruby provides a rather esoteric #curry method for Procs that I’d like to examine in this post.

Currying basically means taking one function with multiple arguments and converting it into a function that takes only one argument and returns another function. The concept was originally coined by Moses Schönfinkel, and later developed by Haskell Curry.

In Ruby, we might have a Proc taking multiple arguments:

f = { |a, b, c| a + b + c }

We can call f with all of its arguments by saying f[1,2,3] which would evaluate to 6 in our case.

Currying f by hand would look like this:

curried_f = do |a| do |b| do |c|
      a + b + c

We can now evaluate our Proc by running curried_f[1][2][3] which would evaluate to 6 exactly as in our previous example.

The ingenious reader have probably already guessed that #curry will take our original f and turn it into curried_f. We can curry f in the following way instead: curried_f = f.curry, and then finally call curried_f[1][2][3] which will unsurprisingly return 6 as before.