Code written while following along with this Elixir course.
Cheat sheet: https://devhints.io/elixir
Elixir is a functional, concurrent programming language built on the Erlang VM, designed for scalable and fault-tolerant applications. The Erlang VM (BEAM) is a virtual machine optimized for running concurrent, distributed, and fault-tolerant applications.
Installation: https://elixir-lang.org/install.html
Functional programming is a style where functions are the main building blocks, there are no classes, objects, methods and properties, or mutable state, and programs rely on immutable data, pure functions, and function composition.
String.length("Test") # Elixir
"Test".length # NodeJS
Immutability means once a value is assigned, it cannot be changed, and any “modification” creates a new value instead.
iex(1)> list = [1, 2, 3]
[1, 2, 3]
iex(2)> list ++ [4, 5]
[1, 2, 3, 4, 5]
iex(3)> list # Here you'll notice that list haven't changed, to change you'll need to rebind like this: list = list ++ [4, 5]
[1, 2, 3]
If we compare immutability with Javascript, you'll notice it's not mutable.
const array = [1, 2, 3]
array.push(999)
array // prints 1, 2, 3, 999
Watch this video for more.
A module groups related code together, and functions are named blocks inside modules that perform specific tasks and can be called with arguments to produce results.
Elixir’s built-in tools let you format code (mix format), run tests (mix test), and generate documentation (mix docs) easily.
ex_doc library first in the project.File extensions:
.ex: Compiled Elixir source files used for application modules and libraries..exs: Interpreted Elixir script files used for scripts, tests, or one-off tasks.IEx is Elixir’s interactive shell that lets you run Elixir code, test functions, and explore your application in real time.
Mix is Elixir’s built-in build tool that helps you create projects, manage dependencies, compile code, run tests, and automate common development tasks.
You can run a single Elixir file by using elixir filename.exs.
OTP is an awesome set of tools and libraries that Elixir inherits from Erlang, a programming language on whose VM it runs. It gives you ready-made building blocks like:
An Application environment is a key-value store that holds runtime configuration for a specific OTP application.
When you run your project (via mix run, iex -S mix, or when deployed), Elixir looks for your app’s Application.start/2 function. That function is responsible for starting processes that your app needs to run.
# Example below launches and supervises a Finch HTTP client (than having to define it in different parts of the code)
def start(_type, _args) do
children = [
{Finch, name: MyFinch}
]
Hex is Elixir’s package manager for finding, installing, and publishing libraries and dependencies.
To install packages in Elixir, add them to the deps list in your mix.exs file and then run:
mix deps.get
mix.exs and run mix deps.get, you can use it by adding alias package_name to the code.
import and can be used between your modules, check this video for a well detailed overview and best practices.Module attributes are values you attach to a module using the @ symbol. They're similar to Javascript's const.
defmodule Example4 do
use Application
@x 5 # module attribute at the module level
def start(_type, _args) do
Example4.main()
Supervisor.start_link([], strategy: :one_for_one)
end
def main do
IO.puts(@x)
end
end
@moduledoc is another module attribute used to store documentation for a module, typically multi-line string, and is read by tools like ExDoc.
An atom is a constant whose name is its value (like :ok or :error or even :hello) and is best for labels, status flags, or keys in maps because comparisons are fast and memory-efficient. A string is text data (like "hello") used for dynamic or user-facing content. Use atoms for fixed, known values and strings for anything that can change or comes from outside your code.
:"hello world"