Elixir is a dynamic, functional language designed for building scalable and maintainable applications. It leverages the Erlang VM, known for running low-latency, distributed, and fault-tolerant systems.
- Getting Started
- Basic Syntax
- Data Types
- Pattern Matching
- Control Structures
- Functions
- Modules
- Concurrency
- Mix - Build Tool
- Testing
To install Elixir, follow the official guide here. Ensure you have Erlang installed as Elixir depends on it.
$ elixir -v
Erlang/OTP 24 [erts-12.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit]
Elixir 1.12.0 (compiled with Erlang/OTP 24)
Elixir's interactive shell, IEx, is a powerful tool to try out code.
$ iex
Erlang/OTP 24 [erts-12.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit]
Interactive Elixir (1.12.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
Elixir has several basic types: integers, floats, booleans, atoms, strings, lists, and tuples.
iex> 1 # integer
iex> 0x1F # integer
iex> 1.0 # float
iex> true # boolean
iex> :atom # atom
iex> "hello" # string
iex> [1, 2, 3] # list
iex> {1, 2, 3} # tuple
Elixir supports integers and floating-point numbers.
iex> 1 + 1
2
iex> 10 / 2
5.0
iex> div(10, 2)
5
iex> rem(10, 3)
1
Elixir uses true
and false
for boolean values.
iex> true and false
false
iex> true or false
true
iex> not true
false
Atoms are constants where their name is their value.
iex> :hello
:hello
iex> :hello == :world
false
Strings in Elixir are UTF-8 encoded binaries.
iex> "hello"
"hello"
iex> String.length("hello")
5
iex> "hello " <> "world"
"hello world"
Lists are a collection of values.
iex> [1, 2, 3]
[1, 2, 3]
iex> length([1, 2, 3])
3
iex> [1, 2, 3] ++ [4, 5, 6]
[1, 2, 3, 4, 5, 6]
iex> [1, 2, 3] -- [2]
[1, 3]
iex> hd([1, 2, 3])
1
iex> tl([1, 2, 3])
[2, 3]
Tuples store a fixed number of elements.
iex> {:ok, "hello"}
{:ok, "hello"}
iex> tuple_size({:ok, "hello"})
2
iex> elem({:ok, "hello"}, 1)
"hello"
iex> put_elem({:ok, "hello"}, 1, "world")
{:ok, "world"}
Pattern matching is a powerful feature in Elixir.
iex> {a, b, c} = {:hello, "world", 42}
{:hello, "world", 42}
iex> a
:hello
iex> [head | tail] = [1, 2, 3]
[1, 2, 3]
iex> head
1
iex> tail
[2, 3]
iex> if true do
...> "This works!"
...> else
...> "This will never be seen"
...> end
"This works!"
iex> unless false do
...> "This works!"
...> end
"This works!"
iex> case {1, 2, 3} do
...> {4, 5, 6} ->
...> "This won't match"
...> {1, x, 3} ->
...> "This will match and bind x to 2"
...> _ ->
...> "This would match any value"
...> end
"This will match and bind x to 2"
iex> cond do
...> 2 + 2 == 5 ->
...> "This is not true"
...> 2 * 2 == 3 ->
...> "Nor this"
...> 1 + 1 == 2 ->
...> "But this is"
...> end
"But this is"
iex> add = fn a, b -> a + b end
iex> add.(1, 2)
3
defmodule Math do
def sum(a, b) do
a + b
end
end
iex> Math.sum(1, 2)
3
iex> add = &(&1 + &2)
iex> add.(1, 2)
3
Modules are a way to group functions.
defmodule Math do
def sum(a, b) do
a + b
end
def zero?(0), do: true
def zero?(x) when is_integer(x), do: false
end
iex> Math.sum(1, 2)
3
iex> Math.zero?(0)
true
iex> Math.zero?(3)
false
Elixir provides concurrency with lightweight processes.
iex> spawn(fn -> IO.puts("Hello from a process!") end)
Hello from a process!
defmodule Greeter do
def greet do
receive do
{sender, msg} ->
send(sender, {:ok, "Hello, #{msg}"})
end
end
end
pid = spawn(Greeter, :greet, [])
send(pid, {self(), "World!"})
receive do
{:ok, message} ->
IO.puts(message)
end
Mix is a build tool that provides tasks for creating, compiling, and testing Elixir projects.
$ mix new my_app
$ cd my_app
$ mix test
Elixir uses ExUnit for writing tests.
defmodule MathTest do
use ExUnit.Case
doctest Math
test "sum of two numbers" do
assert Math.sum(1, 2) == 3
end
end
Run the tests with mix test
.
This guide covers the basics of Elixir. For more in-depth knowledge, refer to the official documentation and explore the rich ecosystem of libraries and tools.
This guide covers essential aspects of Elixir, providing a solid foundation for further exploration and development. Let me know if you need additional sections or specific examples.