Elixir 101
Mar 16, 2025
HomeWhy Elixir?
To build scalable and maintainable applications, Elixir is a dynamically typed functional programming language that is used. It is built on top of Erlang VM, which is known for its fault-tolerance and distributed systems capabilities. Elixir is a great choice for building real-time applications, IoT devices, and distributed systems.
It works even when things go wrong. It is designed to be fault-tolerant and can recover from failures. It is also known for its speed and performance.
Extremely strong community
What is Elixir phoenix?
Phoenix is a web framework for Elixir that is inspired by Ruby on Rails. It is known for its speed and performance. Phoenix uses the Elixir language to build web applications. It is a great choice for building real-time applications, APIs, and web applications.
Installing Elixir
To install Elixir, go to the following link: Elixir Installation and follow the instructions for your operating system.
for Ubuntu:
sudo add-apt-repository ppa:rabbitmq/rabbitmq-erlang
sudo apt update
sudo apt install git elixir erlang
for MacOS:
brew update
brew install elixir
for Windows (using scoop):
scoop install erlang
scoop install elixir
for windows (using chocolatey):
choco install elixir
Getting started with Elixir
IO.puts("Hello World!")
.ex is for compiled files, .exs is for interpreted files
What is mix
Mix is a build tool that provides tasks for creating, compiling, testing your Elixir projects. It is similar to npm in Node.js. It is used to create new projects, compile projects, run tests, and manage dependencies.
Creating a new project
mix help
mix new <project_name>
Project Structure:
my_project/
├── _build/ # Compiled files and artifacts
├── config/ # Configuration files
│ └── config.exs # Main configuration file
├── lib/ # Application code
│ └── my_project.ex # Main application module
├── test/ # Test files
│ ├── my_project_test.exs # Main test file
│ └── test_helper.exs # Test helper functions
├── .formatter.exs # Code formatting configuration
├── .gitignore # Git ignore file
├── mix.exs # Project configuration file
└── README.md # Project documentation
defmodule
is equivalend to namespace. It is used to define a module in Elixir.
defmodule MyModule do
def my_function do
:world # :world is an atom, which is a constant whose name is its value, kinda like string
end
end
How to run the code:
mix compile
iex -S mix
# in th iex shell
MyModule.my_function
We can also do this in the terminal:
mix run -e "MyModule.my_function"
But we see we don't notice any output, so we can use IO.puts to print the output:
defmodule MyModule do
def my_function do
IO.puts(:world)
end
end
Now we should see the output in the terminal.
Currently the way we are setup we have to first compile the code and then run the function. So what's the proper way to run it? How do I define a starting point.
mix run -e "MyModule.my_function"
Read this stack overflow for how to run an elixir application properly with just mix run
command:
Please go through this stack overflow (Important)
https://stackoverflow.com/questions/30687781/how-to-run-an-elixir-application
What is hex?
Hex is a package manager for the Erlang ecosystem. It is used to manage dependencies in Elixir projects. It is similar to npm in Node.js. It is used to install, update, and remove dependencies in Elixir projects.
Installing hex
mix local.hex # this will install hex
Now lets say we want to install uuid package:
Installing a package/ dependency
mix deps.get uuid
or we go into the mix.exs file and add the dependency:
defp deps do
[
{:uuid, "~> 1.2"}
]
end
and then run
mix deps.get # this will install all the dependencies in the mix.exs file
and then in our application we can use the uuid package:
defmodule MyModule do
alias UUID # this is how we import the module in elixir
def my_function do
IO.puts(UUID.uuid4()) # and just use it normally in our code.
end
end
Basic Elixir Data Types
Integers
x = 10
IO.puts(x)
if you wanna define constants in elixir you can use @
symbol:
@x 10 # we usually do this in the beginning of the file in the module
IO.puts(@x)
Atoms
Atoms are constants whose name is their value. They are used to represent something that is not going to change. They are used to represent states, status, and options.
:hello # not supposed to have aribitrary values
:"hello world" # if you want to have spaces in the atom
Atoms should provide with some performance benefits as they are stored in memory only once, so they are faster to compare.
Strings
Strings are enclosed in double quotes. They are used to represent text.
We have escape characters in elixir like \n
for new line, \t
for tab, etc.
We can use varibale inside strings using #{}
Numbers
Numbers in Elixir are represented as floats or integers. They are used to represent numbers.
10.5 # float
10 # integer
def main do
IO.puts(Float.ceil(0.1))
IO.puts(Float.floor(0.1))
IO.puts(Float.round(0.1))
end
Floats have their standard problems with the floating point numbers precision. So be careful when using them.
IO.puts(Integer.to_string(10, 2)) # this will convert 10 to binary
IO.puts(Integer.to_string(10, 16)) # this will convert 10 to hexadecimal
IO.puts(Integer.gcd(25, 10))
Conditionals
if else
name = "Caleb"
status = :gold
if status === :gold do
IO.puts("You are a gold member #{name}")
else
IO.puts("You are not a gold member")
end
name = "Caleb"
status = Enum.random([:gold, :silver, :bronze])
if status === :gold do
IO.puts("You are a gold member #{name}")
else
IO.puts("You are not a gold member")
end
case statement
name = "Caleb"
status = Enum.random([:gold, :silver, :bronze, :"not a member"])
case status do
:gold -> IO.puts("You are a gold member #{name}")
:silver -> IO.puts("You are a silver member #{name}")
:bronze -> IO.puts("You are a bronze member #{name}")
:"not a member" -> IO.puts("You are not a member")
_ -> IO.puts("Get out bruh")
end