Stats

437 Contributors: 19
2017-07-25
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook

Getting started with Elixir Language

Download elixir eBook

Remarks

Elixir is a dynamic, functional language designed for building scalable and maintainable applications.

Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, while also being successfully used in web development and the embedded software domain.

Versions

VersionRelease Date
0.92013-05-23
1.02014-09-18
1.12015-09-28
1.22016-01-03
1.32016-06-21
1.42017-01-05

Hello World

For installation instructions on elixir check here, it describes instructions related to different platforms.

Elixir is a programming language that is created using erlang, and uses erlang's BEAM runtime (like JVM for java).

We can use elixir in two modes: interactive shell iex or directly running using elixir command.

Place the following in a file named hello.exs:

IO.puts "Hello world!"

From the command line, type the following command to execute the Elixir source file:

$ elixir hello.exs

This should output:

Hello world!

This is known as the scripted mode of Elixir. In fact, Elixir programs can also be compiled (and generally, they are) into bytecode for the BEAM virtual machine.

You can also use iex for interactive elixir shell (recommended), run the command you will get a prompt like this:

Interactive Elixir (1.3.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>

Here you can try your elixir hello world examples:

iex(1)> IO.puts "hello, world"
hello, world
:ok
iex(2)> 

You can also compile and run your modules through iex. For example, if you have a helloworld.ex that contains:

defmodule Hello do
   def sample do
       IO.puts "Hello World!"
   end
end

Through iex, do:

iex(1)> c("helloworld.ex")
[Hello]
iex(2)> Hello.sample
Hello World!

Hello World from IEx

You can also use the IEx (Interactive Elixir) shell to evaluate expressions and execute code.

If you are on Linux or Mac, just type iex on your bash and press enter:

$ iex

If you are on a Windows machine, type:

C:\ iex.bat

Then you will enter into the IEx REPL (Read, Evaluate, Print, Loop), and you can just type something like:

iex(1)> "Hello World"
"Hello World"

If you want to load a script while opening an IEx REPL, you can do this:

$ iex script.exs

Given script.exs is your script. You can now call functions from the script in the console.