Stack Builders logo
Arrow icon Insights

Ruby - Now With Better Currying

Improved syntax for currying in Ruby makes it more pleasant for functional programmers. We describe how currying has changed to make it less verbose to partially apply functions.

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!

Published on: Jun. 20, 2014
Last updated: Dec. 20, 2024

Written by:

Subscribe to our blog

Join our community and get the latest articles, tips, and insights delivered straight to your inbox. Don’t miss it – subscribe now and be part of the conversation!
We care about your data. Check out our Privacy Policy.