DEV Community

Cover image for You should learn Elixir, here is why...
Adnan Babakan (he/him)
Adnan Babakan (he/him)

Posted on • Updated on

You should learn Elixir, here is why...

Hey there DEV.to community!

I'm not a professional functional programmer but recently I got involved in a personal project that I chose to write with Elixir. Elixir is super fast and a very fun programming language to learn and use.

Here in this article I will mention some points I liked about Elixir and the importance of learning a functional language as well.

What is Elixir?

Elixir is a functional programming language that runs on Erlang virtual machine and is influenced by Ruby. It is super fast and very capable when it comes to concurrency.

Here is a "Hello World" application written in Elixir:

defmodule App do
    def main do
        IO.puts "Hello World"
    end
end
Enter fullscreen mode Exit fullscreen mode

Looks pretty clean and easy to write and if you are familiar with Ruby you will recognize those "do" and "end" keywords.

An easy-going language about syntax with awesome features

Elixir is an easy-going language when it comes to syntax, it doesn't mean that you can write your code as you want, rather you can omit some rules that you should have obeyed in other languages perhaps. You can omit parenthesis for instance when defining or invoking a function:

defmodule App do
    def main do
        IO.puts sum 8, 12
    end

    def sum x, y do
        x + y
    end
end
Enter fullscreen mode Exit fullscreen mode

And if you pay attention to the sum function, you'll see that there is no return statement, Elixir assumes the last line of code as your return statement.

An anonymous function looks much cleaner if you need to create one:

defmodule App do
    def main do
        sum = fn x, y -> x + y end
        IO.puts sum.(7, 10)
    end
end
Enter fullscreen mode Exit fullscreen mode

And if you think it cannot be shorter see the code below which is equal to the anonymous function above:

defmodule App do
    def main do
        sum = &(&1 + &2)
        IO.puts sum.(7, 10)
    end
end
Enter fullscreen mode Exit fullscreen mode

Although it might look a bit bizarre if you are unfamiliar with this syntax, it can become your next huge coding style!

FP is the next big thing!

FP is making much more sound these days comparing to few years ago and that's because people are starting to realize how cool FP is.

Even if you don't use FP in your projects learning FP will make you think differently and thus improve your coding skills.

Looping in Elixir

Elixir variables are immutable meaning that you cannot change their value (it is like defining a totally new variable if you try to assign a new value to a previously defined variable). The question here is if variables are immutable how can you loop since you need a counter variable for your for loop or a boolean variable for your while loop?

Here is something strange! Elixir doesn't have any loop constructors like for or while! Instead you'll be working with recursive functions to loop through your data!

Here is sample of printing from number 20 to 1 in Elixir:

defmodule App do
    def main do
        my_numbers 20
    end

    def my_numbers n do
        if n > 0 do
            IO.puts n
            my_numbers n - 1
        end
    end
end
Enter fullscreen mode Exit fullscreen mode

This might seem hard to implement but believe me if you get used to it, it is easier than a for loop in some ways!

If you are wondering how list (array) looping works in Elixir here is the implementation:

defmodule App do
    def main do
        my_list = ["Say", "hi", "to", "Elixir"]
        loop_through_list my_list
    end

    def loop_through_list([]) do


    def loop_through_list [head | tail] do
        IO.puts head
        loop_through_list tail
    end
end
Enter fullscreen mode Exit fullscreen mode

And the explanation is pretty simple. There is a feature called pattern matching (almost like destructuring objects in JavaScript) which allows you to get your list's head and tail, head being the first element and tail the rest. So in the first call we print the head and pass tail to the same function recursively. But to stop our loop there is another function named exactly the same with an empty list as the only argument meaning that if the passed list it empty this function should be run.

Here is the procedure of how this works:

loop_through_list =>
head: "Say", tail: ["hi", "to", "Elixir"]
Prints "Say"
Invokes loop_through_list => ["hi", "to", "Elixir"]
---
loop_through_list =>
head: "hi" -> tail : ["to", "Elixir"]
Prints "hi"
Invokes loop_through_list => ["to", "Elixir"]
---
loop_through_list =>
head: "to" -> tail : ["Elixir"]
Prints "to"
Invokes loop_through_list => ["Elixir"]
---
loop_through_list =>
head: "Elixir" -> tail : []
Prints "Elixir"
Invokes loop_through_list => []
loop_through_list => [] (empty lid)
Nothing to do and returns nil
Enter fullscreen mode Exit fullscreen mode

Now that we know the procedure, the reality is Elixir provides some functions to loop through stuff without needing to write it over and over again if you need something rather simple:

defmodule App do
    def main do
        Enum.each(20..1, fn x -> IO.puts x end)
        Enum.each(["Say", "hi", "to", "Elixir"], fn item -> IO.puts item end)
    end
end
Enter fullscreen mode Exit fullscreen mode

Case matching

A function can be combined with a switch-case statement in Elixir (kinda xD)! Here is an example:

defmodule App do
    def main do
        math = fn
            :sum, x, y -> x + y
            :subtract, x, y -> x - y
            :multiply, x, y -> x * y
            :divide, x, y -> x / y
            :remainder, x, y -> rem x, y
        end

        IO.puts math.(:sum, 10, 2) # 12
        IO.puts math.(:subtract, 8, 3) # 5
        IO.puts math.(:multiply, 4, 6) # 24
        IO.puts math.(:divide, 18, 2) # 9
        IO.puts math.(:remainder, 15, 4) # 3
    end
end
Enter fullscreen mode Exit fullscreen mode

There is also guards! When defining a function you can use guards to ensure that your function does exactly what you want!

defmodule App do
    def main do
        IO.puts divide 8, 4 # 2
        IO.puts divide 8, 0 # undefined
    end

    def divide(x, y) when y !== 0, do: x / y
    def divide(_, y) when y === 0, do: nil
end
Enter fullscreen mode Exit fullscreen mode

Here we defined two functions which in one we are checking if y is not equal to 0 so we divide the numbers and one if y is equal to 0 so it returns nil. Putting _ means that we won't need this argument or variable later.

Elixir is beyond this

All the things I've mentioned here are just a brief explanation of Elixir's features. Elixir is fascinating and will make you a better programmer for sure.

Hope you enjoyed!

Tell me what you think about Elixir in the comments section below.

Top comments (0)