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?
The Secret Weapon: The BEAM Virtual Machine
- Massive Concurrency: It can run millions of isolated processes simultaneously on a single machine without breaking a sweat.
- 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.
- 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
Let's continue to the interactive shell (IEx)
Starting the Shell
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)>
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
+, -, 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
/ 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
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.