DEV Community

Cover image for Elixir Basics
Samuel Littell
Samuel Littell

Posted on • Updated on

Elixir Basics

Before I begin this blog, I think it would be wise to preface it with a brief statement. As someone who is in the early stages of their JavaScript learning journey, it may seem a bit premature to venture out into unknown territory. That territory specifically being another programming language, of which I probably have no business exploring. But I do believe this will be beneficial. Seeing another language's inner workings and understanding its syntax and data types (even on a basic level) will maybe shed some light on how and where to address my weaknesses with JavaScript, and with coding in general.

Image description


Elixir is a functional programming language released by José Valim in the year 2012. It has gained a pretty substantial fan base since then, and is utilized in companies by the likes of Motorola, Pinterest, and Discord (among many, many others). It runs on top of the Erlang Beam virtual machine, which was actually developed in the 80s to scale the growing demands of phone switching. Elixir was essentially created to make the Erlang technology accessible to the every day developer. Elixir has a minimal and dynamically typed syntax inspired by Ruby, which makes it more approachable than other languages. Also, it's Phoenix web framework is responsible for the creation of thousands of full stack web applications.

Image description


Installing Elixir on your machine is a breeze. Simply visit the Elixir installation page and find your operating system in the installation list. For MacOS you simply run a command in your terminal, or for Windows follow a quick series of installation prompts, and voilà, you have Elixir installed. No need for path configuration or anything like that, just a straight install. To double check that the installation process was a success, head back over to your terminal and do a version check with the command: elixir -v. (At the time of writing this blog, Elixir is currently on version 1.14.0.) To get started with a new project, set your terminal to the desired directory, and use the command mix new "project name", and the necessary files will be loaded into your project folder, including a README, gitignore, library, and other basics.

Now that we have a project set up, let's start taking a look at the datatypes that make up Elixir.

Image description


The first two datatypes we will be taking a look at are integers and floats. These are both number data types, but they can never be strictly equal to each other. Any whole number is considered an integer, while floats are numbers with any given number of decimal places following that number. Here is a quick demonstration of how these two datatypes can be used together in operations, but in the end, are still different.

iex(1)> 1 + 1
2       
iex(2)> 1.5 + 1.5
3.0     
iex(3)> 1 + 1.5
2.5     
iex(4)> 4 * 4
16      
iex(5)> 3 === 3.0
false
Enter fullscreen mode Exit fullscreen mode

Image description


The next datatype is boolean. This is going to be your standard boolean datatype giving you true and false values.

iex(6)> true
true    
iex(7)> true === false
false 
Enter fullscreen mode Exit fullscreen mode

We can even wrap our value in an is_boolean function to assess whether or not the value is in fact a boolean datatype.

iex(8)> is_boolean(true)
true
iex(9)> is_boolean("a")
false
Enter fullscreen mode Exit fullscreen mode

Image description


Strings in Elixir must be delimited by using double quotes. (See yuh later single quotes.) Other than that, strings in Elixir behave pretty similarly to those in JavaScript. They can be concatenated together with the + or <> operators and Elixir also supports string interpolation by inserting your string value into an interpolation syntax: #{string value}. To print a string value simply wrap your value in the IO.puts(string value) function. Other string functions include String.length(string value) and String.upcase(string value).

iex(10)> string = "dog"
"dog"
iex(11)> "The #{string} ran fast"
"The dog ran fast"
iex(12)> "The " <> "#{string} " <> "ran fast" 
"The dog ran fast"
iex(13)> IO.puts("Hello World!")
Hello World!
iex(14)> String.length("hello")
5
iex(15)> String.upcase("hello")
"HELLO"
Enter fullscreen mode Exit fullscreen mode

Image description


Lists in Elixir are similar to arrays in other languages, but they're not exactly the same. Aesthetically, they're identical, but lists in Elixir behave more similarly to linked list data structures. The first element of the list is considered the "head" and the remaining elements are considered the "tail". The first and remaining values in a list can be accessed by utilizing the hd(list) and tl(list) functions. Lists are also immutable, meaning the original list can not be edited or changed.

iex(16)> list = [2, 4, 6, 8]
[2, 4, 6, 8]
iex(17)> hd(list)
2
iex(18)> tl(list)
[4, 6, 8]
Enter fullscreen mode Exit fullscreen mode

Image description


Tuples are somewhat similar to lists. They are represented though, with curly brackets instead of square. They are however, also immutable. The real difference though lies in that lists are sorted in memory as linked lists. This means accessing the end of the list is a linear operation, so we need to travers the entire list to determine its length, or size. Tuples on the other hand are stored contiguously in memory. So accessing an element can be a very quick operation. However adding or updating an element to tuples can be expensive in memory, because it requires creating a new tuple.

iex(19)> {"hello", "world!"}
{"hello", "world!"}
iex(20)> tuple = {"hello", "world!"}
{"hello", "world!"}
iex(21)> tuple_size(tuple)
2
iex(22)> elem(tuple, 0)
"hello"
iex(23)> elem(tuple, 1)
"world!"
Enter fullscreen mode Exit fullscreen mode

Image description


Finally we arrive at atoms! Atoms are similar to symbols in Ruby, and somewhat similar to defining a variable using the const keyword in JavaScript. Atoms are constants who's name is also their value. In other words, they will always represent what they stand for. Atoms can also represent boolean values as seen below.

iex(24)> :hello === :hello
true
iex(25)> :hello === :world
false
iex(26)> true === :true
true
iex(27)> is_atom(false)
true
Enter fullscreen mode Exit fullscreen mode

If you were wondering what the "iex" is preceding my code, that is the Elixir Interactive Shell. It is the built in shell that allows you to write statements and execute them, resulting in an output.

So there are some basic fundamentals to dip your toes into Elixir!
I hope to continue exploring Elixir further and hopefully build some projects using this language in the future.

Resources:

Top comments (0)