Oboe
No history yet

Elixir language

Welcome! I'm excited to help you dive into Elixir.

Before we jump into the code, it helps to know what kind of projects you've worked on. To help me tailor our session, could you tell me a little bit about your programming experience, specifically if you have used any functional programming languages before?

I have experience with OOP (like Python, JS, or Java) but not functional programming.

It is great to have you here! Transitioning from object-oriented programming to functional programming is an exciting shift in mindset, and Elixir is a fantastic language to bridge that gap.

What is Elixir?

At its core, Elixir is a dynamic, language designed for building scalable, high-concurrency, and maintainable applications. If you are coming from Python or JavaScript, you might be used to objects, mutable states, and class hierarchies. Elixir discards these in favor of immutable data and pure functions. Instead of modifying an object's state in place, you pass data through a series of transformations, like an assembly line.

The Secret Weapon: The BEAM Virtual Machine

Elixir does not run directly on your hardware. It compiles to bytecode that runs on the , the Erlang Virtual Machine. Erlang was built by Ericsson in the 1980s to run massive telecommunication switches. It was designed from day one to handle millions of simultaneous connections and to stay up no matter what—even if some hardware fails. By running on the BEAM, Elixir inherits three superpowers:
  1. Massive Concurrency: It can run millions of isolated processes simultaneously on a single machine without breaking a sweat.
  2. Fault Tolerance: If a process crashes, it doesn't bring down the whole application. Elixir uses "supervisors" to automatically restart failed parts of your app to a known safe state.
  3. Low Latency: The virtual machine ensures that no single heavy process can hoard the CPU, keeping your application highly responsive under heavy loads.

The Design Philosophy

Elixir was created in 2011 by José Valim. He loved the performance and resilience of Erlang, but wanted a modern language with high developer productivity. To achieve this, he designed Elixir's syntax to feel clean and friendly (strongly inspired by Ruby), while pulling powerful metaprogramming capabilities from Clojure, and keeping the rock-solid runtime characteristics of Erlang.

Let's continue to the interactive shell (IEx)

Let's dive straight into the interactive shell, known as . It is the playground where you will write, test, and instantly run Elixir code without having to set up a massive project structure first.

Starting the Shell

To fire up the shell, you simply open your terminal and type iex. If you are on Windows using PowerShell, you might need to run iex.bat instead, because iex is a built-in alias in PowerShell. Once loaded, you will see a welcoming prompt like this:
Interactive Elixir (1.20.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> 
The iex(1)> prompt is waiting for you to type some Elixir code. Let's try typing a simple mathematical expression to see how it evaluates things instantly.
iex(1)> 40 + 2
42

Continue to basic data types and operations

Let's dive into Elixir's core building blocks! Since you are comfortable with Python and JavaScript, the way Elixir handles numbers will feel very familiar, but there is one crucial twist when it comes to division.

Integers and Floats

In Elixir, numbers are split into (whole numbers) and floats (decimal numbers). Just like in JS or Python, you can perform standard arithmetic using +, -, and *. Let's see how these look inside our interactive shell:
iex(1)> 5 + 5
10

iex(2)> 10 * 3
30

iex(3)> 5.5 + 4.5
10.0

The Division Twist

In Elixir, the standard / operator always returns a float, even if you divide two whole numbers evenly. This is different from languages like Python 2 or Java, where dividing integers yields an integer.
iex(4)> 10 / 2
5.0
If you want to perform integer division (where you get a whole number back and drop the remainder), Elixir provides a specific function called div/2 (the /2 simply means it takes two arguments). If you want just the remainder, you use rem/2:
iex(5)> div(10, 3)
3

iex(6)> rem(10, 3)
1

This separation ensures that your arithmetic types are completely predictable. Let's see how these feel in practice.

Continue to Atoms

Let's explore , which are one of the most unique and useful data types you will encounter in Elixir.

What is an Atom?

An atom is simply a constant whose name is also its value. In Elixir, they always start with a colon :.

iex(1)> :hello
:hello

iex(2)> :ok
:ok

If you are coming from JavaScript or Ruby, you can think of atoms as being identical to Symbols. If you are coming from Python or Java, they act very much like Enums. They are frequently used to signal the status of an operation, like returning :ok when a database query succeeds or :error when something goes wrong.

The Hidden Secret of Booleans and Modules

Under the hood, Elixir does not actually have a distinct boolean type! The values true and false are just syntactic sugar for the atoms :true and :false.

You can even test this equivalence directly in your interactive shell:

iex(3)> :true == true
true

iex(4)> :false == false
true

Even the names of modules you write (like String or Integer) are represented as atoms behind the scenes. This unified design is one of the ways the BEAM VM keeps its internal mechanics so clean and efficient.