Elixir is a functional programming language that extends the Erlang language, which is an older language that gained its popularity in the 80's, and is mixed with Ruby syntax. The language is fairly new, being created in 2011, and has excellent documentation. It is a functional programming language and has no classes, but instead modules with module functions. Elixir also has a mature web framework called Phoenix, which makes using it much easier when developing real world applications.
After you install Elixir you will have three new executables right out of the box, being iex
, elixir
and elixirc
. If you compiled Elixir from source or are using a packaged version, you can find these inside the bin directory. For these exmaples I'll use iex
(or iex.bat
if you are on Windows) which stands for Interactive Elixir. In interactive mode, you can type any Elixir expression and get its result immediately in the terminal.
Before getting started it is probably best to cover the basics on Elixir, and that starts with its simple data types. There are several different types of simple data types in Elixir, being integers, floats, strings, atoms, booleans, lists, and tuples. I'll touch on each of these data types with a brief explanation of what they are.
Integers and floats are both number data types. Integers can be thought of as whole numbers, and floats are integers with decimal values. While these data types might seem similar, integers and floats are two completely different values and can never be strictly equal to each other. With that being said, you can still use integers and floats together in operations, and sometimes operations on integers will automatically produce floats.
iex(1)> 2 + 2
4
iex(2)> 3 * 3
9
iex(3)> 3 / 3
1.0
iex(4)> 1 === 1.0
false
As seen from the code above, performing division on two integers will always produce a float. You can also see that even though mathematically 1 === 1.0 is true, since they are two different data types in Elixir they are not strictly equal values.
Next on the list of simple data types we have strings. All strings in Elixir must be declared using double quotes. Strings are essentially binaries converted to characters, so you can treat them the same. To append strings together, unlike using +
in JavaScript, you use the <>
operator. You can use simple concatenation to add strings together. You can also interpolate values but placing them into the #{//value}
interpolation syntax.
iex(5)> intro = "hello"
"hello"
iex(6)> "#{intro} " <> "world!"
"hello world!"
Atoms are simple data types that will always equal themselves. The can be defined by putting a colon in front of the value. In other languages, they are sometimes called symbols. A close reference would be assigning a value to const in JavaScript, but atoms also act similar to booleans.
iex(7)> :atom === :atom
true
iex(8)> :true === true
true
iex(9)> :true === "true"
false
Lists are complex data types that store references to any value types in a specific order. They are defined with brackets, and very comparable to arrays in JavaScript. Any data type can be stored in a list and data is accessed in reference to the head and the tail. The head is the first value in a list and the tail is a list of all values after the head. The values in a list can be accessed with the hd/1
and tl/1
functions. To concatenate two lists you would use ++
, and to subtract you would use '--'.
iex(10)> list = [7, 16.6, :atom, "bird"]
[7, 16.6, :atom, "bird"]
iex(11)> list ++ ["cat", false]
[7, 16.6, :atom, "bird", "cat", false]
iex(12)> list
[7, 16.6, :atom, "bird"]
iex(13)> hd list
7
iex(14)> tl list
[16.6, :atom, "bird"]
Tuples are also lists of values, and defined with curly brackets. They still act more like a JavaScript array rather than an object, as the don't have key value pairs. A tuple may contain elements of different types, which are stored contiguously in memory. Accessing any element takes constant time, but modifying a tuple, which produces a shallow copy, takes linear time. Tuples are good for reading data while lists are better for traversals. Tuples are typically used either when a function has multiple return values or for error handling.
Hopefully this was a helpful first glance into Elixir. While they have some similarities, Elixir and JavaScript also have many differences. The documentation for Elixir is extremely helpful, and their website also has beginner tutorials if you'd like to learn more.
Top comments (0)