Functional Programming Aspects of the Ruby Language
Revised by Andy Holland
Lately, there has been a significant change in the industry towards functional programming, new languages have appeared: Elixir, Scala, Elm.
With all this new hype I decided to give a try to some of them, and I have to say that they are really cool, but wether I like it at work I mostly use ruby. Not that I complain or anything but is exciting to be able to play with the new kid on the block.
After some time playing with a new functional programming languages, I have noticed that Ruby has some similarities with them.
I’m not going to say that ruby is a FP language nor that Matz the creator is incorrect.
To quote Matz words from an interview with O’Reilly.
I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python.
So Ruby is an object-oriented language – that’s is for sure – but I wanted to share my idea that ruby is a multipurpose language.
According to Wikipedia for a programming language to be considered Functional it needs to have: – First-class and higher-order functions – Pure functions – Recursion – Strict versus non-strict evaluation – Type systems – Currying – Lazy Evaluation
We are going to explore some of the concepts with Ruby :gem:
- takes one or more functions as arguments
- returns a function as its result
In ruby, there are examples everywhere which show that ruby has support for Higher-order functions.
Let’s look at a well-known ruby method
We can consider the block as passing a function as argument
To make the case more clear, you can write the same example using lambdas.
Many of ruby-core methods take a block or function as argument
As for functions that return other functions as not that common in ruby but doesn’t mean we can’t do it.
1 2 3 4 5 6
First Class Functions and Support for Lambdas
Ruby has support for multiple types of functions; lambdas and procs, there are shuttle differences between them, and we will need a whole post about it, but instead, there are many significant resources out there explaining the differences articles. How I like to think about them is as functions that can you stored in variables:
One interesting pattern is the policy pattern that allows you to have a function execute the same tasks but depending on the business logic the callbacks for success or failure can be configured:
1 2 3 4 5 6 7 8 9 10 11
One thing that is not that common in ruby is storing method inside variables, using the method
method, we can store them and pass it to functions as if it were a proc or a lambda.
There is an excellent video from RubyTapas that cover this topic very well.
For now, I let you a small example:
1 2 3 4 5 6 7 8 9
Currying means to partially apply a function, here are a couple of definitions:
- Partial function application is calling a function with some number of arguments, to get a function back that will take that many fewer arguments.
- Currying is taking a function that takes n arguments, and splitting it into n functions that take one argument.
In ruby land; is not very common to use this technique, but this allows us to have small and reusable functions that can be easy to use and understood while you read your code.
Since Ruby 1.9, the Proc class have the method:
#curry, that allow us to implement both options.
Let’s see some examples.
1 2 3 4 5 6 7 8 9
A nice plus is that working with this small functions your code becomes much easier to test.
First of all, I encourage to check this blog post from Pat Shaughnessy
Lazy evaluation allows us to consume data on demand, instead of evaluating everything up front.
Lets look at some examples:
1 2 3 4 5 6 7
lazy it tries to consume all the elements and transform them, ending in an endless loop but the second example uses the lazy evaluation allowing to collect only the data that need.
This technique is advantageous when we are dealing with an enormous amount of data, and we do not want to transform all of them at once but instead only on demand.
We all know that ruby is a dynamic language with great introspection capabilities, and doesn’t have a type system.
Probably one of the reasons everyone loves ruby is due to these specifications, where we are allowed to do want we want. But having that freedom of choice, we tend to end with profoundly entangled and error-prone applications.
I’m not saying not to use ruby, but some type system that allows us to have warranties of want sort of data are we working with would be fantastic.
Dry-types to the rescue \o/.
Talking about dry-types would be out of scope for this post and probably requires one itself :wink: but just to let you know: if you are looking for a valuable option for your application, there is one.
A pure function is a function which:
- Given the same input, will always return the same output.
- Produces no side effects.
Having pure functions will give us a lot of benefits, the first one that comes to my mind is parallel processing.
Pure functions are also easy to understand, refactor and move around, making our application less error prone.
And going to show a straightforward example
1 2 3
No matter what if we introduce the same input it will return the same output.
Ruby allow us to have mutable state, so is up to us to change how we write our function in a way that is pure.
If you have any thoughts or questions, please share and I’ll be happy to answer in the comments.