SUSE Blog

Diving into Elixir

chabowski

By: chabowski

April 8, 2017 5:52 am

Reads:3,054

Comments:0

Score:5

Print/PDF

The following article has been contributed by Imobach González Sosa, Software Engineer at the YaST team, SUSE.

 

 

 

 

 

Apart from being fun, I truly believe that learning new programming languages helps you to become a better developer. And that’s specially true if the languages you learn push you out of our comfort zone.

Some months ago I discovered Elixir, a functional programming language that targets on the Erlang Virtual Machine (more on that later). Elixir was developed initially by José Valim, a prominent member of the Ruby community and a Ruby on Rails developer.

In this article I’ll go through some Elixir aspects and I’ll share my view and (limited) experiences about them. Maybe you get interested in the language after reading it.

Enters Functional Programming

I’ve been writing Ruby code for quite some years and I would say that it is my main programming language. As you may know, Ruby is a (highly dynamic) Object Oriented language, so approaching to Elixir has been an interesting journey.

When it comes to Functional Programming, I like to highlight two core concepts: avoiding (limiting) side-effects as much as possible and separating data and behavior. And those concepts challenged an Object Oriented mindset guy like me.

The Language

Syntax

Elixir’s syntax is somehow inspired in Ruby. Once you get used to, it is quite nice, although I must admit that it has some oddities, most of them related to anonymous functions.

But if I had to underline some aspect, I would talk about the cool pipe operator. Instead of:

func2(func1(some_data))

you can write:

some_data |> func1 |> func2

It’s not only about making the code more readable, which is just a matter of taste, but it also emphasizes the idea of data transformations.

Pattern Matching

Pattern matching is a key concept in Elixir. It’s used instead of assignments, conditions and even when calling functions. For instance, see the (typical) factorial example:

def fact(0), do: 1
def fact(n), do: n * fact(n-1)

Anyway, the Elixir documentation explains better than me. Of course, it’s nothing new for you if you come from languages like Haskell.

Recursion Everywhere

Being a functional language, recursion is at the heart of Elixir. As recursion will be used even for iterating, Elixir implements tail call optimization, so it will be quite efficient and you won’t have to worry, when done right, about the stack size.

Metaprogramming

Any Elixir program can be described by its own data structures, and mechanisms to compose them are available. On the other hand, the language can be extended through the use of macros. In fact, some of the language constructions are just macros.

Ecto, a database wrapper, offers a really nice macro-based API that worth a look if you’re interested in metaprogramming with Elixir.

Other Goodies

Polimorphism, sigils, doctests… there are a lot of things to learn about Elixir, but you have many good resources available for that. So let’s move on to a different topic.

The Erlang Ecosystem

Elixir runs on top of the Erlang Virtual Machine, which means that it has access to all the resources available in that platform (just like Scala or JRuby have access to the resources on the Java Virtual Machine).

Erlang has a mature ecosystem that was built with concurrency and fault tolerancy in mind and has proven its value in quite demanding scenarios.

A crucial component on the Erlang ecosystem is the popular Open Telecom Platform (OTP). Elixir embraces OTP adding its own abstraction on top of it. Usually, complex applications are composed by several (cheap and lightweight) Erlang Virtual Machine processes which communicate and cooperate.

Libraries and Tooling

Elixir is still a young language but is growing at a good pace. You can find lot of libraries in Hex, the package manager for the Erlang ecosystem.

But when it comes to tooling, I would highlight Mix, the build tool that comes with Elixir. Apart from managing your project’s dependencies, it includes several tasks to compile, build, run tests, etc. If you come from Ruby, you can think of it like a Rake+Bundler combo.

Finally, just because it’s cool, I like to bring your attention on the Observer. This Erlang module allows you to inspect almost every aspect of your running application: load, processes, memory content… sometimes I wished having something like this in Ruby. Just check the screenshot.

The Phoenix Framework

Before joining the YaST team at SUSE, I was writing Ruby on Rails applications for some years (I started with version 0.13 back in 2005). For that reason, the Phoenix Framework caught my attention when I approached Elixir.

Phoenix is a web development framework inspired by Ruby on Rails but with a functional approach. It’s composed by several projects, like Ecto (the database layer we already mentioned) or Plug (did anybody say Rack)?

Fortunately, inspiration works in both directions and Ruby on Rails got some ideas from Phoenix to build Action Cable. Lovely! That’s how Open Source works …

Phoenix creator, Chris McCord, wrote an interesting article summarizing similarities and differences between Phoenix and Rails two years ago.

To be honest, if I had to start now a web development project, I would consider Phoenix as a serious alternative. Maybe during next Hack Week …

Getting You Started

If you’re interested in learning more about Elixir, you should start checking Elixir’s website where you can find a nice Getting Started Guide. Check out also the learning section for more resources.

Personally, I really enjoyed Elixir in Action, by Saša Jurić, and I’m also a big fan of his blog.

Another interesting resource is Daily Drip, where you can find short videos about Elixir, among other topics. Finally, you can check the typical awesome list which includes a lot of resources.

And now: have a lot of fun!

2 votes, average: 5.00 out of 52 votes, average: 5.00 out of 52 votes, average: 5.00 out of 52 votes, average: 5.00 out of 52 votes, average: 5.00 out of 5 (2 votes, average: 5.00 out of 5)
You need to be a registered member to rate this post.
Loading...

Tags: , , , , , , ,
Categories: Expert Views, Technical Solutions

Disclaimer: As with everything else in the SUSE Blog, this content is definitely not supported by SUSE (so don't even think of calling Support if you try something and it blows up).  It was contributed by a community member and is published "as is." It seems to have worked for at least one person, and might work for you. But please be sure to test, test, test before you do anything drastic with it.

Comment

RSS