How to get started with elixir? (Part 1)

How to get started with elixir? (Part 1)

Elixir is an new awesome language based on the erlang ecosystem, an very robust system which prevents your critical systems from failing and is fast

·

3 min read

Today we are looking at Elixir a dynamic, functional programming language for building scalable and maintainable applications. But what does that mean?

First we have to understand that the elixir and erlang are both compiled to the same code which is than executed by BEAM an virtual machine which handles the cross platform execution. Imagine it like Java with some advantages in concurrency and fail tolerance.

Getting started

First we need the elixir vm installed on our local machine, here you can get the specific instruction for your operating system. To check weather the installation succeeded or not you can use elixirs version command.

> elixir -v

Next we need to understand some basic commands and tools before we can start working with the language itself.

You can start the elixir command line in interactive mode to execute your code right in it.

$ iex
Erlang/OTP 23 [erts-11.1.8] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]

Interactive Elixir (1.11.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> 2+3
5
iex(2)> String.length("Elixir is a great language")
26
iex(3)>

To start an method you can call the Module name and the function here as well

iex(1)> TestModule.method()
"1234test"

Basic Types

Now that we are ready with exploring the basic execution tools of the elixir language, we can now go on and learn the basic types of elixir.

Atoms

Atoms are feeling kinda like an single enum value, but they are a little different. They are used like declared and used enums in elixir. So one advantage but sometimes disadvantage are that atoms are staying in memory as long as the application runs, so they are not garbage collected, which is good for a few constants but very inefficient for dynamic generated values. So only use atoms for constant values.

iex> :apple
:apple
iex> :orange
:orange
iex> :watermelon
:watermelon

Atoms are equal if their names are equal.

iex> :apple == :apple
true
iex> :apple == :orange
false

Often they are used to express the state of an operation, by using values such as :ok and :error.

Integer

Integers is the data type for whole numbers of all types: regular (122), octal (0b0110), or hexadecimal (0x1F).

iex> 255
255

Floats

Floats is the data type for decimal digits.

iex> 3.14
3.14
iex> .14
** (SyntaxError) iex:2: syntax error before: '.'
iex> 1.0e-10
1.0e-10

Booleans

An boolean value is a classic true or false value.

iex> true
true
iex> true == false
false

Elixir provides a bunch of predicate functions to check for a value type. For example, the is_boolean/1 function can be used to check if a value is a boolean or not:

Strings

Strings are a type for multiple characters, they are encoded in UTF-8.

iex> "hellö"
"hellö"

Elixir also supports string interpolation:

iex> string = :world
iex> "hellö #{string}"
"hellö world"

Strings can have line breaks in them. You can introduce them using escape sequences:

iex> "hello
...> world"
"hello\nworld"
iex> "hello\nworld"
"hello\nworld"

Header Image by cenczi from Pixabay