The Role of Functional Programming in Web Development (Intro to Elixir for Ruby Developers)

Why you should learn Elixir if you’re a Ruby developer

Richardson Dackam
8 min readMar 30, 2023

As a Senior Fullstack Ruby on Rails Developer, I have seen firsthand the power of functional programming in web development. Functional programming languages like Elixir and Clojure are designed for concurrency, which means they can handle multiple tasks at the same time without blocking. This can be especially useful in web development, where applications may need to handle many requests at once. Using functional programming principles can help you build applications that are more scalable and responsive.

Functional programming is a programming paradigm that emphasizes the use of pure functions, immutability, and higher-order functions.

In this blog post, I will discuss the role of functional programming in web development and why you should learn Elixir if you are a Ruby developer.

But First, quick definitions

What are Pure functions?

A pure function is a function that always produces the same output for a given input, and does not have any side effects. In other words, it doesn’t change anything outside of the function itself. For example, a function that takes two numbers and adds them together is a pure function. It will always return the same result for the same inputs.

What do I mean by Immutability?

In programming, immutability means that once a value is assigned to a variable, it cannot be changed. This can help reduce bugs and make code easier to reason about. For example, in a functional programming language, if you have a list of numbers, you cannot change any of the numbers in the list. Instead, you would create a new list with the updated values.

What are Higher-order functions?

A higher-order function is a function that takes another function as an argument or returns a function as its result. This allows you to write more generic code that can be reused in different contexts. For example, a function that takes a list of numbers and a comparison function, and returns a sorted list based on that comparison function, is a higher-order function. The comparison function can be different each time the function is called, allowing you to sort the list in different ways.

Figure: Programming language paradigms (source: https://www.mitrais.com/news-updates/transition-to-functional-programming-for-java-developers/)

The Role of Functional Programming in Web Development

Functional programming has several benefits that make it well-suited for web development. Here are some of the main ways that functional programming can help you build better web applications:

1. Simplifies Code and Reduces Bugs

Functional programming emphasizes the use of pure functions, which are functions that always return the same output for a given input and have no side effects. This makes it easier to reason about code and reduces the likelihood of bugs.

For example, let’s say you have a function that takes an array of numbers and returns the sum. In a functional programming language like Elixir, you could write this function as follows:

def sum(numbers) do
Enum.reduce(numbers, 0, fn(number, acc) -> number + acc end)
end

This function written in Elixir is pure and has no side effects. It takes an array of numbers and returns the sum, without modifying any external state. This makes it easy to reason about and test.Now you may think. Why not just use Ruby to write functional programming code?

def sum(numbers)
numbers.reduce(0) { |acc, number| number + acc }
end

This is the same function as a Ruby method.

Using functional programming paradigms in Ruby can indeed be a viable option, especially if you are already proficient in the Ruby language, since Ruby is a flexible and versatile language that supports multiple programming paradigms, including object-oriented, procedural, and functional programming. However, there are certain trade-offs and benefits to consider when deciding between writing a functional app in Ruby versus using a purely functional programming language like Elixir.

While Ruby allows for functional programming, it is not a purely functional language. As a result, some functional programming constructs and features may not be fully supported or may require workarounds. It also allows for mutable state, which can lead to side effects and make it harder to reason about code in a functional programming context. Elixir is built on the Erlang Virtual Machine (BEAM), which is designed for highly concurrent, distributed, and fault-tolerant systems. This can be a significant advantage for certain types of applications, such as real-time web applications and distributed systems. Since Elixir is purely functional it promotes immutability and referential transparency. This can lead to code that is easier to reason about, test, and maintain.

2. Facilitates Concurrency and Parallelism

Functional programming emphasizes immutability, which means that once a data structure is created, it cannot be modified. This makes it easier to write code that can be executed in parallel or concurrently.

For example, let’s say you have a web application that needs to process a large amount of data. In a functional programming language like Elixir, you could use a library like GenStage to divide the data into smaller chunks and process them concurrently.

But, why not just implement Concurrency and parallelism with Ruby?

Achieving concurrency and parallelism in Ruby can be more challenging compared to a functional programming language like Elixir, which is inherently designed for concurrent and parallel execution.

Ruby allows for mutable states, which means that data structures can be modified after they are created. This can lead to race conditions and other synchronization issues in concurrent or parallel execution, as different threads or processes may try to modify the same data simultaneously.

Elixir provides built-in concurrency primitives like processes, message passing, and supervision trees, which facilitate the development of concurrent and fault-tolerant applications. In contrast, Ruby does not have such built-in primitives, which means that developers need to rely on external libraries or build custom solutions for concurrent and parallel processing.

Writing concurrent and parallel code in Ruby can involve dealing with low-level synchronization primitives (e.g., Mutex, ConditionVariable) and managing inter-process communication. This can introduce additional complexity and make the code harder to reason about and maintain.

Despite these challenges, Ruby does offer some tools and libraries for achieving concurrency and parallelism, such as the Thread class, the concurrent-ruby gem, and the Parallel gem. Additionally, Ruby 3 introduced Ractors (Ruby actors), an experimental feature that aims to provide a way to achieve parallel execution without GIL limitations. That’s Global Interpreter Lock which is the standard implementation of MRI Ruby. It prevents multiple threads from executing Ruby code in parallel.This can limit the ability to achieve true parallelism with threads, and as a result, developers may need to rely on multi-process approaches or alternative Ruby implementations like JRuby to achieve parallel execution.

I wrote extensively about JRuby in my post The obscure world of Jruby — JRuby with Rails Under a Microscope.

3. Promotes Code Reusability

Just like Ruby, which emphasizes the principle of code reusability through object-oriented programming, mixins, and modules, functional programming languages like Elixir also promote code reusability through different mechanisms.

Functional programming emphasizes the use of higher-order functions, which are functions that take other functions as arguments or return functions as results. This makes it easier to write reusable code.

For example, let’s say you have a web application that needs to sort a list of items in several different ways. In a functional programming language like Elixir, you could write a generic sort function that takes a comparison function as an argument:

def sort(items, compare_fun) do
Enum.sort(items, compare_fun)
end

You could then pass in different comparison functions to sort the list in different ways:

sort([3, 1, 4, 1, 5, 9, 2, 6, 5], fn a, b -> a < b end)
sort([3, 1, 4, 1, 5, 9, 2, 6, 5], fn a, b -> a > b end)

This promotes code reusability and makes it easier to write generic functions that can be used in multiple contexts.

source: https://www.monterail.com/blog/ruby-vs-elixir

Why You Should Learn Elixir if You’re a Ruby Developer

Elixir is a functional programming language that shares some similarities with Ruby in terms of syntax and philosophy. Here are some reasons why you should consider learning Elixir if you are a Ruby developer:

1. It’s Easy to Learn for Rubyist

Elixir is easy to learn if you already know Ruby. Elixir syntax is similar to Ruby, and many of the concepts in Elixir are also present in Ruby. For example, both Elixir and Ruby have functional programming features like closures and higher-order functions.

2. It’s Scalable

Elixir is designed for scalability. Its concurrency model is based on the actor model, which means that it can handle large amounts of concurrent requests without blocking or crashing. This makes it well-suited for building web applications that need to handle a high volume of traffic.

3. It’s Fault-Tolerant

Elixir is designed to be fault-tolerant. Its processes are isolated from each other, which means that if one process crashes, it does not affect the rest of the application. Additionally, Elixir has built-in error-handling mechanisms that make it easy to recover from errors.

4. It Integrates Well with Ruby on Rails

Elixir can be easily integrated with Ruby on Rails applications. For example, you can use Elixir to build a separate service that communicates with your Ruby on Rails application via an API. This allows you to take advantage of Elixir’s concurrency and fault-tolerance features while still using Ruby on Rails for other parts of your application.

5. It’s Fun!

Finally, learning Elixir can be a fun and rewarding experience. Elixir has a supportive community and many resources available for learning. Additionally, because Elixir is a relatively new language, there is a lot of opportunity for innovation and experimentation.

Want to Learn Elixir? Here are some resources to get you started

Hourandcode

Elixir for Rubyist ( Free )

Elixir School ( Free )

Elixir Succinctly ( Free )

ThinkingElixir.com’s Pattern Matching Course (Free)

Programming Elixir ≥ 1.6 by Dave Thomas (

Programming Phoenix 1.4 by Chris McCord, Bruce Tate, Jose Valim

If you’re looking for a developer to work on your Elixir application or build a Ruby on Rails application, I’m here to help. I’m new to Elixir but I’m very excited to work with this language. As a senior full-stack ruby on rails developer with expertise in building robust and scalable applications, I can assist you in creating a high-quality application that meets your needs. Whether you need help with project planning, development, or deployment, I’m available to support you. Contact me via LinkedIn or my company’s website to explore how we can work together to create a powerful Ruby on Rails application that exceeds your expectations.

--

--

Richardson Dackam
Richardson Dackam

Written by Richardson Dackam

Building and maintaining apps while teaching the process with systems you can steal. | Learn to build apps @ richdackam.com | Build one @ Richdx.com |

No responses yet