An Introduction to Elixir
April 2020 • Mike Zornek
Slide 2
Slide 2 text
http://mikezornek.com
Slide 3
Slide 3 text
Elixir is a dynamic, functional language designed for
building scalable and maintainable applications. Elixir
leverages the Erlang VM, often called the BEAM,
which is known for running low-latency, distributed
and fault-tolerant systems.
Slide 4
Slide 4 text
Origins
Slide 5
Slide 5 text
José Valim
Slide 6
Slide 6 text
No content
Slide 7
Slide 7 text
No content
Slide 8
Slide 8 text
Elixir is a dynamic, functional language designed for
building scalable and maintainable applications. Elixir
leverages the Erlang VM, often called the BEAM,
which is known for running low-latency, distributed
and fault-tolerant systems.
Slide 9
Slide 9 text
* Code Expectations
Slide 10
Slide 10 text
Functional Language
Slide 11
Slide 11 text
defmodule Greeter do
def hello(name) do
"Hello, " <> name
end
end
iex> Greeter.hello("Sean")
"Hello, Sean"
Slide 12
Slide 12 text
# Other Languages
baz(new_function(other_function())
# Elixir’s Pipe Operator
other_function() |> new_function() |> baz()
defmodule Greeter do
def hello(name) do
"Hello, " <> name
end
end
iex(3)> Greeter.hello(6)
** (ArgumentError) argument error
:erlang.byte_size(6)
iex:3: Greeter.hello/1
iex(3)> Greeter.hello(true)
** (ArgumentError) argument error
:erlang.byte_size(true)
iex:3: Greeter.hello/1
Slide 18
Slide 18 text
defmodule Greeter do
@spec hello(String.t()) :: String.t()
def hello(name) do
"Hello, " <> name
end
end
# ** (CompileError) greeter.ex:8: undefined function hello/1
hello(6)
Slide 19
Slide 19 text
No content
Slide 20
Slide 20 text
Pattern Matching
Slide 21
Slide 21 text
# Algebra
10 = x * 2
Slide 22
Slide 22 text
iex(5)> x = 1
1
Slide 23
Slide 23 text
iex(5)> x = 1
1
iex(6)> 1 = x
1
Slide 24
Slide 24 text
iex(5)> x = 1
1
iex(6)> 1 = x
1
iex(7)> 2 = x
** (MatchError) no match of right hand side value: 1
iex(7)>
Slide 25
Slide 25 text
iex(7)> 3 = y
** (CompileError) iex:7: undefined function y/0
Slide 26
Slide 26 text
# Tuples
iex> {:ok, value} = {:ok, "Successful!"}
{:ok, "Successful!"}
iex> value
"Successful!"
iex> {:ok, value} = {:error}
** (MatchError) no match of right hand side value:
{:error}
Slide 27
Slide 27 text
Pattern Matching
Slide 28
Slide 28 text
Metaprogramming
Slide 29
Slide 29 text
defmodule Friends.Person do
use Ecto.Schema
schema "people" do
field :first_name, :string
field :last_name, :string
field :age, :integer
end
end
Slide 30
Slide 30 text
Scalable
Slide 31
Slide 31 text
Process
Slide 32
Slide 32 text
Process
Mailbox
State
Slide 33
Slide 33 text
Process A
Mailbox
State
Process B
Mailbox
State
Messages
Slide 34
Slide 34 text
defmodule Example do
def listen do
receive do
{:ok, "coffee"} -> IO.puts("Coffee time!")
{:ok, "tea"} -> IO.puts("Tea, Earl Gray, Hot.")
end
listen()
end
end
CPU CPU CPU CPU
BEAM
OS Thread OS Thread
OS Thread
OS Thread
Process
Process
Process
scheduler
Process
Process
Process
Process
Process
Process
Process
Process
Process
scheduler scheduler scheduler
OS process
Slide 37
Slide 37 text
BEAM
Server
CPU CPU CPU CPU
CPU CPU CPU CPU
Slide 38
Slide 38 text
BEAM
Server
CPU CPU CPU CPU
CPU CPU CPU CPU
Server
CPU CPU CPU CPU
CPU CPU CPU CPU
Server
CPU CPU CPU CPU
CPU CPU CPU CPU
Server
CPU CPU CPU CPU
CPU CPU CPU CPU
Slide 39
Slide 39 text
No content
Slide 40
Slide 40 text
Fault-Tolerant
Slide 41
Slide 41 text
Process
Supervisor
Slide 42
Slide 42 text
Process
Supervisor
Process
Process
Slide 43
Slide 43 text
Supervisor
Process
Process
Process
Supervisor
Process
Process
Process
Supervisor
Process
Process
Process
Supervisor
Supervisor
Slide 44
Slide 44 text
Low-Latency
Slide 45
Slide 45 text
No content
Slide 46
Slide 46 text
No content
Slide 47
Slide 47 text
Maintainable
Slide 48
Slide 48 text
No content
Slide 49
Slide 49 text
$ iex -S mix
iex(1)> :observer.start()
Slide 50
Slide 50 text
No content
Slide 51
Slide 51 text
No content
Slide 52
Slide 52 text
No content
Slide 53
Slide 53 text
No content
Slide 54
Slide 54 text
Notable Projects
• Building web applications using Phoenix.
• Working with databases using Ecto.
• Assemble data processing pipelines with Broadway.
• Crafting GraphQL APIs using Absinthe.
• Deploying embedded software using Nerves.
Slide 55
Slide 55 text
• Elixir Language Website Guide
https://elixir-lang.org/getting-started/introduction.html
• Elixir School
https://elixirschool.com/en/
• Elixir in Action (Book)
https://www.manning.com/books/elixir-in-action
• The Pragmatic Studio (Videos)
https://pragmaticstudio.com/elixir
• ElixirConf
https://www.youtube.com/channel/UC0l2QTnO1P2iph-86HHilMQ/videos
Slide 56
Slide 56 text
Thanks!
Available For Hire:
http://mikezornek.com/for-hire/
Contact:
@zorn on Micro.Blog + Twitter