Functional Programming - Elixir

I recently came across a Elixir and thought it's a nice time to talk about it. Elixir is a programming language created in 2012 and is designed to build scalable and maintainable applications. It has following features
  1. Functional - language is completely based on modules and functions. There are no classes
  2. Dynamic - it is dynamic language. There is no need to define type while creating a variable. They are automatically assigned during run time.
  3. Compiled - source code of a elixir file (.exs) is compiled to byte code
  4. Distributed and Concurrent - can run quickly and independently
  5. Runs on Erland Virtual Machine (BEAM) - highly stable and mature

What is it used for?

Elixir is a general purpose language especially suitable for
  • Data Processing
  • Network applications
  • High Availability System


All named functions in Elixir must be a part of an enclosing module. A sample function declaration is shown below

defmodule MyModule do
myFunction(first_param, second_param)
<> second_param

As you can see in above example we have defined a module named "MyModule" followed by a function ("myFunction") declaration. Since Elixir is a dynamic language we don't need to define data type for our function parameters. Also as you can notice there is no return statement. Last statement of every Elixir function is used as return value so no need to explicitly define it.

Note: "<>" is the string concatentation operator. In above function we are taking two parameters and concatenating them as string. You can learn more about operators here.

Pure Functions

Elixir follow the concept of pure functions. In computer programming a function can be considered as pure function if both of the below criterias are true
  1. Function always return the same value for a given set of parameters
  2. Evaluation of result doesn't cause any sematically observable side effects or output such as mutation of mutable objects.

Pipe Operator

While thinking purely in terms of functions, a common mistake is writing too many nested function calls. This can make code hard to read and in turn very difficult to maintain. You can have a look at some of the example below and decide for yourself

func(first_param) - a fairly simple function declaration
parent_func(func(first_param), second_param) - still ok but this is becoming complex
super_func(grand_func(parent_func(func(first_param), second_param)), third_param) - now this is something which will give nightmares to its developer.

To make things simpler we have Pipe operator (|>)
If I take the above example I can make it much simpler as shown below

func(first_param) |> parent_func(second_param) |> gran_func() |> super_func(third_param)

Pipe operator works very much similar to pipe operation in unix. It takes the output from the expression on its left side and passes it as the first argument to the function call on its right side.

After the above code change one can still argue that if there are more functions than the width of line will be too large making it again difficult to read. To avoid these kinds of issue it is recommended to break each pipe into new line as shown below

|> gran_func()
|> super_func(third_param)

This is all I have as an introduction. If you want to know more about this language I would highly recommend to visit and subsribe to Elixir Radar for all the latest news.


Popular posts from this blog

Google Chrome Extension - ReactJS

Angular JS, Require JS and Karma

Anonymous Functions - Elixir