Introduction to Elixir
I have been trying Functional Languages for a little while now. F# is in the .Net environment so why not give it a go. Haskell seems like a different monster, so let’s give it a try, Erlang and its EVM looked amazing.
I don’t really know why but even though I tried, they were not very engaging for me.
Then I learnt Elixir, a new language running in the EVM. I must be honest, I am a C-guy, so I like my curly braces and my semicolons. Elixir had some Ruby-like syntax, so no more semicolons to me. At the end I have come to enjoy programming in Elixir so much. In this post I want to share the basics with you so that maybe you give it a chance, as I did myself a few months ago.
Why Functional Programming?
We have all read why FP is awesome, so I don’t want to annoy you with yet another post around FP awesomeness. I’d like though, to give you some context, about why this thing matters.
Computers are getting wider, more CPUs, many languages don’t embrace parallel processing from their inception. What we are seeing these days is OOP embracing some FP principles, as systems become more distributed.
Functional programming makes a lot easier to work with Distributed systems because of…
You can’t change things.
We are exposing functions to mutate the internal state of the shopping cart, in this case items property. If you then want to add an item to the cart:
In Functional Programming we don’t mutate the state of the application but we transform it. This means that in our previous example, you would get a new cart when adding an item to it:
No Side Effects
In OOP we are really focus on hiding and protecting things. To me it looks like we try not to be honest with ourselves, we hide our code so I can’t touch it later. Then side effects arise its ugly face. Because when you hide things, those things can do other unexpected things, like saving things in database or calling a service that deletes some data.
At the end we are making our lives more complicated because things are harder to follow and maintain. With FP one function does one thing.
So if we mix Immutability and No side effects we get the following benefits:
- Pushes you to write smaller more predictable functions.
- Tests are clearer, actually you don’t really need mocks.
- Not much debugging.
Let’s meet Elixir
I am assuming that you can run IEX (interactive elixir) in a command line. If you can’t just download it from Elixir web site or use any sort of Vagrant or Chocoltery.
Let’s open IEX and…
No semicolons, no braces.
Elixir is like Ruby, really clean languages.
Atoms are labels in Elixir:
Tuples, lists, keyword list, map, struct
Head tail lists decomposition
We have a post around Pattern matching. There are many ways to define pattern matching but I like to look at it as a Maths equation. Let’s look today at the following. In OOP we usually control the flow of the application like this:
In Elixir we hardly ever write if statements, we use pattern matching to control the flow:
When we do overloading in OOP we can define the same function but with different input parametes. If you have the same input parameters, then you have to change the name. In Elixir the name of the function is the same and so is the input parameter, a tuple in this case. The appropriate function will be called using Pattern matching.
In Elixir we can pass the result of a function to the next function:
By the way you may be wondering how the hell you return something in a function: it is really easy, it will be the last statement of the function
Recursion, Pattern matching, and Head tail: head tail recursion
I will leave you with some mind blowing stuff. I have the list of my expenses the last month. I want to sum them all:
In OOP we usually loop through the list and accumulate the sum using some sort of foreach. In Elixir it is actually much better to recurse through the list, and operate it.
How do I learn more stuff?
Here you have a list of resources I found useful when learning Elixir:
Elixir docs, I recommend looking into the Enum module, the most comprenhensive set of functions to manipulate lists I’ve ever seen: http://elixir-lang.org/docs/stable/elixir/Kernel.html
Introduction to Elixir: https://github.com/elixir-lang/plug
Some really nice tutorial done by Rob Connery: http://www.redfour.io/
Jose Valim, creator of Elixir guiding you to create a really nice example: https://howistart.org/posts/elixir/1
This page has tone of resources for Elixir: https://github.com/h4cc/awesome-elixir
And the slack group is really active, you can ask question they are really helpful people: https://elixir-lang.slack.com
Use a lot IEX to try out code, at the end we are only writing functions.
Finally I recomend that you challenge yourself by doing some practical work. My friend Richard Kotze has contributed to Open Source (Whitebread and Eyedrops) and Leigh Shepperson is creating an amazing Algebra package.
In my case I have implemented a TicTacToe in Elixir and ReactJS.
Hope this was useful, thanks for reading!