Introduction to Elixir Programming

Introduction to Elixir Programming

Welcome to the Elixir programming language tutorial.

The Elixir programming language runs on prime of the Erlang VM. It’s successfully matched with Erlang, however has a apparent syntax and capabilities.

Programming is a discipline via which it seems like each single day a brand new language, library or framework is stumbled on.

Normally, it’s relatively not simple to take care of up with the enhancements, however when communities worldwide counsel a apparent novelty, it’s value checking and experimenting with it. That’s moreover the case with the Elixir programming language.

So, when it entails deciding on a purposeful programming language, there are available in a multitude from which you may likely have the ability to be in a location to make the most of searching on what you want.

Elixir Programming Language

Elixir is a purposeful programming language that positive aspects an growing collection of recognition in builders’ neighborhood. It’s far believed to be to be an immutable purposeful language, which normally signifies the undeniable fact that’s simple to learn and fabricate on, a dream got here correct for any pleasurable programmer.


The principle operate of Elixir programming is concurrency. Concurrency refers back to the effectivity with which a immense sequence of processes may even be flee on the identical time, with out being linked to 1 each different, whereas diminishing aspect results.

Erlang Digital Machine

Elixir has as a predominant inspire the undeniable fact that it’s far additionally aged with the Erlang Digital Machine or EVM. What’s approach additional thrilling is that Elixir processes steer particular of points linked with managing share knowledge by merely laying apart shared reminiscence and relying on asynchronous message passing.

Furthermore, the mannequin via which the processes can be a part of with one another throughout the identical group, recommends Elixir as a colossal instrument for horizontal scaling, a equal to that aged in distribute applications.

As talked about above, Elixir programming language had a hit in EVM, whereas the Erlang neighborhood overtly common Elixir on account of its scalable and maintainable purposes.

Thus, Elixir gained catch legitimate of entry to to the Erlang ecosystem, capabilities and benefits of its VM that results in low-latency, fault-tolerance and disbursed applications.

what is elixir


Elixir code is straightforward to implement, a light-weight decision for using successfully machine sources.

You may possibly possibly beginning the Elixir interactive shell with the voice iex.

Interactive Elixir (1.3.3) - press Ctrl+C to exit (sort h() ENTER for relieve)
iex(1)> IO.locations("Good day world")
hey world

To flee Elixir code from a file make the most of:

elixirc hey.ex

Why Elixir?

So, the quiz of that continues to be is why it’s best to unruffled try to make the most of Elixir programming language. Correctly, Elixir boards current an in depth degree of view with regard to this new language, whereas recommending it as a instrument you may likely have the ability to be in a location to depend on when it entails web-connected or disbursed actions.

Furthermore, the tendency this present day is to place up disbursed instrument, being understanding to be additional ambiance high-quality and pleasurable, for which a brand new programming language comparable to Elixir is most in mannequin on account of the undeniable fact that it permits builders to assign a distribute-model instrument from the inspiration up.

Bottom line, Elixir programming represents a dynamic, purposeful language constructed up for scalable and maintainable purposes.

Moreover, Elixir is approach better than Erlang VM, ceaselessly identified for its low-latency and fault-tolerant applications, which customers now generally tend to guide away from.

Elixir may even be efficiently aged in each net establishing and gear domains, on account of its scalability, extensibility and purposeful programming.

Set up Elixir

The set up of elixir is sort of easy, and this textual content focuses on study the formulation to place in and make the most of elixir on a linux blueprint.

Of path, the pleasurable net area has already launched the installation on the pleasurable web space intimately, so I acquired’t elaborate on it right here. Simply know that elixir is according to erlang so

Sooner than putting in elixir, ensure that that erlang is put in within the neighborhood and that elixir is just successfully matched with variations of erlang 18 and later.

Because of the linux blueprint tools administration devices are various, so I counsel the supply code compilation is additional general.

$ git clone

Executing the above voice within the terminal will catch the elixir supply code within the up to date listing, lastly, cautious of us can think about that after we examine the model, we acquire

$ cd elixir

‘-dev’ method that the up to date department is a establishing department and not a secure model, lastly everytime you happen to appropriate are searching out for to try it out you may likely have the ability to be in a location to skip a step. If we get now to make make the most of of it for an legitimate mission then we should make the most of this model of Precise. The perfect factor is that this is a git repository, we appropriate should know that the up to date secure model is switching over at that department

$ git department -a 
:: grasp
  remotes/origin/HEAD -> origin/grasp

Now the secure model of elixir is as a lot as 1.Four so (git is appropriate for model take care of watch over)

$ git checkout origin/v1.4

The next step is to assemble and set up, first traipse to the supply code listing after which attain the next voice

$ assign shifting check out
$ assign set up

This path of is a bit prolonged, traipse to assign a cup of tea and attain inspire on the whole thing OK most often attain throughout making an try out errors, you may likely have the ability to be in a location to find the direct suggestions google a bit, on the entire can treatment. Correctly, should you flee IEX within the terminal and be taught the next, you’re accomplished

Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.4.1) - press Ctrl+C to exit (sort h() ENTER for relieve)


Since elixir offers a terribly useful co-fabricate instrument, mix, after we are searching out for to fabricate a brand new mission we appropriate want

$ mix new my_project
$ cd my_project

Should you’re within the listing, you may likely have the ability to be in a location to obviously be taught the construction of the listing, and you then’ll likely have the ability to be in a location to start the elixir streak!

Good day world

Good day world program in Elixir. This program solely outputs hey world.

Hold as helloworld.ex, then flee it within the voice line.

defmodule Good day manufacture
   def pattern manufacture
       IO.locations "Good day World!"
   give up
give up

This system creates a module with one attribute. To flee it, load this machine after which identify the method using the module. As confirmed under:

Good day world

Bustle with this the elixir interactive shell:

Erlang/OTP 20 [erts-9.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [kernel-poll:false]

Interactive Elixir (1.3.3) - press Ctrl+C to exit (sort h() ENTER for relieve)
iex(1)> c("helloworld.ex")
iex(2)> Good day.pattern
Good day World!
:good satisfactory

hello world in elixir


Variables in Elixir may even be outlined with out fear. A variable may even be of a collection of varieties entails numbers and phrases.

You may possibly possibly assign that within the Elixir interactive shell, iex:

iex(1)> x = 3

iex(2)> identify = "Bob"

Or in bid so that you can output it as textual content:

iex> identify = "Alice"
iex> "Your identify is, #{identify}"
"Your identify is, Alice"

Knowledge varieties

A variable may even be of a apparent sort, like strings (phrases), integers (full numbers), booleans (correct/fraudulent values)

Elixir has a great deal of kinds of variables which could possibly possibly be supported, particularly:

  • integers
  • float
  • boolean
  • atom/image
  • string
  • guidelines
  • tuple

Within the Elixir interactive shell:

iex> 1         # integer
iex> 0x1F      # integer (hexadecimal)
iex> 1.0       # float, for the reason that dot
iex> correct      # boolean
iex> :atom     # aom
iex> "elixir"  # string
iex> [1,2,3]   # guidelines
iex> {1,2,3}   # tuple


Within the Elixir shell (iex) you may likely have the ability to be in a location to sort any expression

iex> 4+4
iex> 3*3
iex> 10 / 2

Within the tip two examples an integer is returned (8,9) however within the remaining occasion

a floating degree amount (5.0). Why?

On story of in Elixir the division operator (/) repeatedly returns a float. However you may likely have the ability to be in a location to spherical the output to the closest amount using:


Elixir Strings


Elixir strings are encoded in UTF-Eight construction. That method your complete explicit characters of UTF-Eight are supported out of the field.

iex> "hellö wörld 由力驱动"
"hellö wörld 由力驱动"

This may possibly be accomplished with variables to:

iex> x = "hey"
iex> x

You may possibly possibly print variables like this

iex> x = 3
iex> IO.locations("x = #{x}")


Line breaks are moreover supported by using the n character.

iex> IO.locations("heynworldnhellonyou")

Whenever you try it with out IO.locations, this may seemingly additionally current off you the string definition with out newlines:


String size

To catch the string size

iex> String.size("hey world")

The size may even be assigned to a variable:

iex> sl = String.size("hey world")
iex> sl

Positive aspects

Positive aspects are on the middle of programming in Elixir. How manufacture you invent capabilities and the way manufacture you make the most of them?

On this textual content you’ll study study the formulation to make make the most of of named capabilities. That is by no means any longer the one kind of attribute, as chances are high you may possibly study within the subsequent lesson.

Attribute occasion

Attribute identify

A attribute is created in a module. You may possibly possibly manufacture this without delay from Elixir shell (iex) or invent a file (.ex) and flee it with elixirc.

The pattern under defines a module (Good day) and a named attribute (hey) that doesn’t take any arguments.

defmodule Good day manufacture
  def hey manufacture
    IO.locations "hey world"
  give up
give up

A attribute is printed with the def key phrase, it wishes to be internal a module outlined with the defmodule key phrase.

Don’t fail to remember the manufacture give up mixture as this defines the code block (scope).

Then identify it (the attribute identify) using:

iex> Good day.hey
hey world
:good satisfactory

On story of the named attribute solely has one line, you may likely have the ability to be in a location to shorten it to:

defmodule Good day manufacture
  def helo manufacture IO.locations "hey world" give up
give up

Which does the correct similar, however most often a one liner is way easier to learn.

Attribute parameters

Positive aspects can take parameters. Let’s beginning by organising a attribute that takes parameters a and b.

defmodule Maths manufacture
  def sum(a,b) manufacture
    a + b
  give up
give up

Then identify it with

iex> Maths.sum(3,4)

If statements

If statements attain an expression if it’s scenario is appropriate. The expression is executed if and provided that the placement (most often referred to as predicate) is appropriate.

The if assertion is a key thought in programming that exists in loads of completely different languages. However, not like venerable languages elixir variables are immutable.

elixir if

In elixir the placement is written between the phrases if and manufacture. The expression isn’t any longer executed if the placement is fraudulent.

You may possibly possibly make the most of if statements within the Elixir interactive shell or from elixir code.

x = 3

if x > 2 manufacture
 " x better than 2"
give up

elixir if else

If the placement isn’t any longer correct, it’s far additionally fraudulent. You may possibly possibly make the most of if else to take care of each circumstances.

if fraudulent manufacture
  "This could not ever be considered"
  "This may possibly"
give up

In elixir interactive shell:

~ iex
Erlang/OTP 20 [erts-9.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [kernel-poll:false]

Interactive Elixir (1.3.3) - press Ctrl+C to exit (sort h() ENTER for relieve)
iex(1)> if fraudulent manufacture
...(1)>   "can not be right here"
...(1)> give up

if statement in elixir

elixir if else one line

You may possibly possibly make the most of a one liner for if statements like this:

if correct, manufacture: :good satisfactory, else: :error

In elixir interactive shell:

iex(4)> if correct, manufacture: :good satisfactory, else: :error
:good satisfactory

Anonymous capabilities

Anonymous capabilities are outlined on a single line. In Elixir, an anonymous attribute is a in mannequin sort. You will additionally get guessed, anonymous capabilities don’t get a repute.

In purposeful programming, you’re looking out out for to rework knowledge and that’s what capabilities are for. Elixir has two kinds of capabilities: anonymous capabilities and named capabilities.

Describe Anonymous Attribute

To outline one, write fn (ArgumentList) -> physique give up.

It has these machine:

  • fn the declaration

  • (ArgumentList) zero or additional arguments (we’ll make the most of a and b).

  • -> attribute implementation begins right here

  • give up declaration ends

You may possibly possibly examine examine it’s certainly a attribute with

iex> is_function(double)

Make Anonymous attribute

Delivery the Elixir shell (iex) to try the examples under. The occasion under creates an anonymous attribute that takes parameters a and b.

iex> sumit= fn(a,b) -> a+b give up
iex> sumit.(2,3)

To identify anonymous attribute, the expression sumit.(2,3) is aged.

The dot in sumit.(2,3) is required for anonymous capabilities. That’s how Elixir distincts anonymous capabilities from named capabilities.

iex> double= fn a -> add.(a,a) give up
iex> double.(2)

The anonymous attribute is saved in double. Then it’s referred to as with double.(2).

Anonymous attribute identify

The attribute assigned to the variable can trade, take a be taught at this example:

iex> func = fn(a,b) -> a+b give up
iex> func.(5,3)
iex> func = fn(a,b) -> a-b give up
iex> func.(5,3)

On this example func is a variable, not a attribute identify. The anonymous attribute assigned to it’s reassigned, that’s why the output modifications when calling it.

You may possibly possibly trade them anytime you’d like, whoa!


Case is aged to guage a designate in opposition to many patterns until an identical one is stumbled on. That’s comparable to the change statements in different programming languages.

Given an enter variable, this may seemingly additionally copmare it to every pattern. There are some examples under.

elixir case pattern matching

Elixir checks for an identical pattern. You may possibly possibly assign that in iex:

iex(1)> case {1,2,3} manufacture
...(1)>   {1,5,6} -> "156"
...(1)>   {1,2,4} -> "124"
...(1)>   {1,2,3} -> "123"
...(1)> give up

elixir case occasion

Delivery the Elixir interactive shell iex within the terminal.

Then try the elixir case occasion under:

Interactive Elixir (1.3.3) - press Ctrl+C to exit (sort h() ENTER for relieve)
iex(1)> x = 3
iex(2)> case {x} manufacture
...(2)>   {1,2} -> "No match"
...(2)>   {3} -> "Match"
...(2)> give up

As you be taught, solely expression the place x is analogous as three is executed. You may possibly possibly specify a pattern of possibilites with case.

If the worth isn’t any longer within the pattern, this may seemingly additionally give a CaseClauseError. Worship so:

iex(3)> case {x} manufacture
...(3)>   {4} -> "4"
...(3)> give up
(CaseClauseError) no case clause matching: {3}


Cond lets you match many stipulations on the identical time.

If a scenario within the block is appropriate, its code will seemingly be executed. If the placement isn’t any longer correct, this may seemingly additionally merely traipse to the following scenario.

That’s repeated until both the give up of the block or until a codition is appropriate.

cond occasion

You may possibly possibly assign that within the elixir interactive shell iex:

iex(1)> cond manufacture
...(1)>   1 + 1 == 4 -> "By no method considered"
...(1)>   2 3 == 9 -> "Nope" 
...(1)>   1 + 2 == 3 -> "Proven"
...(1)> give up

or everytime you happen to need make the most of elixirc to flee the (occasion.ex) program under:

x = 3
cond manufacture
   x == 1 -> IO.locations "It's 1!"
   x == 2 -> IO.locations "It's 2!"
   x == 3 -> IO.locations "It's 3!"
   correct   -> IO.locations "I surrender."
give up


If no scenario matches, CondClauseError is raised.

iex(4)> cond manufacture
...(4)>   1 + 3 == 9 -> "By no method"
...(4)> give up
(CondClauseError) no cond clause evaluated to an real designate

Because of that you just are going to are also searching out for therefore that you just can add correct to the as a remaining scenario, which is able to repeatedly match.

iex(4)> cond manufacture
...(4)>   3 > 1 -> "By no method"
...(4)>   correct -> "No maches stumbled on"
...(4)> give up
"By no method"

You will additionally make the most of else if clauses in loads of essential languages. You may possibly possibly make the most of this as each different of nesting many if statements.

Attempt make the most of

Attempt-utilize is aged to make the most of values which could possibly possibly be thrown. The try make the most of clause first tries to achieve

the codeblock. Then if a message is thrown, it goes into the make the most of clause.

The concept of strive-utilize isn’t any longer irregular to Elixir, it’s in the marketplace in loads of programming languages.

Attempt make the most of occasion

You may possibly possibly assign that within the elixir interactive shell iex. Or everytime you happen to need, add it to an elixir program.

Then we throw a designate with throw(:hey).

try manufacture
make the most of
  message -> "Purchased #{message}."
give up

This program outputs

"Purchased hey."

After clause

Elixir has the after clause. If a designate isn’t any longer caught, this may seemingly additionally attain that clause.

Within the occasion under a designate is thrown, so it goes into the make the most of clause.

try manufacture
make the most of
  message -> "Purchased #{message}."
  IO.locations("I am the after clause.")
give up

Whenever you manufacture not throw a designate, it jumps straight into the after clause:

try manufacture
make the most of
  message -> "Purchased #{message}."
  IO.locations("After clause.")
give up

In different programming languages that’s the lastly case;


Please enter your comment!
Please enter your name here