Why Elixir Should be Your Next Language
Why Elixir?
- Small lang with simple abstractions
- Developer empathy as a core value
- Gentle learning curve
- Fault tolerant
- Understandable concurrency
- Interesting performance properties
- Transforms how you think
Elixir is Small
$ iex
iex> 1 # integer
iex> 0x1F # integer
iex> 1.0 # float
iex> true # boolean
iex> :atom # atom (symbol)
iex> "elixir" # string (binary)
iex> 'elixir' # charlist (erlang string)
iex> [1, 2, 3] # list
iex> {1, 2, 3} # tuple
iex> %{a: 1, b: 2, c: 3} # map
Basic Types
Elixir is Small
iex> 1 + 2
3
iex> 5 * 5
25
iex> 10 / 2
5.0
iex> div(10, 2) # int div
5
iex> div 10, 2
5
iex> rem 10, 3 # modulo
1
Math Expressions
iex> 0b1011
11
iex> 0o777
511
iex> 0x1F
31
iex> 1.0
1.0
iex> 1.0e-10
1.0e-10
iex> round(3.58)
4
iex> trunc(3.58)
3
Elixir is Small
iex> "hellö"
"hellö"
iex> "hellö #{:world}"
"hellö world"
iex> byte_size("hellö")
6
iex> String.length("hellö")
5
iex> String.upcase("hellö")
"HELLÖ"
String Expressions
Elixir is Small
iex> [1, 2, true, 3]
[1, 2, true, 3]
iex> length [1, 2, 3]
3
iex> [1, 2, 3] ++ [4, 5, 6]
[1, 2, 3, 4, 5, 6]
iex> [1, true, 2, false, 3, true] -- [true, false]
[1, 2, 3, true]
iex> [11, 12, 13]
'\v\f\r'
iex> [104, 101, 108, 108, 111]
'hello'
List Expressions
Elixir is Small
iex> tuple = {:ok, "hello"}
{:ok, "hello"}
iex> put_elem(tuple, 1, "world")
{:ok, "world"}
iex> tuple
{:ok, "hello"}
Tuple Expressions
Elixir is Small
iex> [a: 1, b: 2] # keyword list
[a: 1, b: 2]
iex> [{:a, 1}, {:b, 2}] # same as above
[a: 1, b: 2]
iex> %{a: 1, b: 2, c: 3}
%{a: 1, b: 2, c: 3}
iex> %{"a" => 1, :b => 2, {:user, 3} => 3}
%{:b => 2, {:user, 3} => 3, "a" => 1}
Keyword Lists and Maps
Elixir is Small
defmodule Calc do
def add(x, y) do
x + y
end
def mult(x, y) do
do_mult(x, y)
end
defp do_mult(x, 1), do: x
defp do_mult(x, y) when y > 1 do
do_mult(x, y-1) + x
end
end
Modules
What even is Elixir?
- Based on Erlang and the BEAM
- Highly Concurrent
- Fault Tolerant
- Functional paradigm
- First-class tooling
OMG
|> WTF
|> BBQ
"abcdefg"
123
%{"OMG": "wtf"}
defmodule Foo do
@moduledoc """
OMG WTF
"""
def foo(%{omg: wtf}) do
wtf
end
end
Why Elixir Should Be Your Next Language
By asonge
Why Elixir Should Be Your Next Language
- 255