Stack Builders News

A collection of thoughts and notes by our team


Justin Leitgeb

Ruby - Now With Better Currying


For aficionados of functional programming, currying is a must. According to Wikipedia, "currying is the technique of transforming a function that takes multiple arguments ... in such a way that it can be called as a chain of functions, each with a single argument." 1

Currying is considered such a fundamental part of functional programming that languages like Haskell curry by default. After doing more development in Haskell, I've lamented the verbosity of using currying in Ruby. To curry a method, you had to first grab a reference to that method, then turn that method into a proc, and finally tell it to work with partial application:

> def add a, b ; a + b; end
=> :add
> method(:add).to_proc.curry
=> #<Proc:0x007fa9e2140af8 (lambda)>

This just changed yesterday in Ruby head, and you can now call curry directly on an instance of the Method class!

> def add a, b ; a + b; end
=> :add
> method(:add).curry
=> #<Proc:0x007fa9e2140af8 (lambda)>

Although this is a relatively minor change, it's a step in the right direction for functional programming in Ruby. Kudos to Arne Brasseur for making this change!

Do You Have What it Takes To Be a Stack Builder?