Erlang • Designed for scalability and real-time systems
• Functional
• Simplifies concurrent programming
• Fault-Tolerant (“let it crash” error handling philosophy)
• Designed by Joe Armstrong in 1986
• Legendary nine 9’s of uptime
The Virtual Machine • Each instance of the Erlang VM is called a node
• You can have one or more nodes on a physical machine
• You can spread nodes across multiple physical machines
• Nodes are one OS process, with one more more threads
Processes • Erlang processes are not OS processes
• Erlang VM distributes work of Erlang processes between the OS threads of the VM’s OS process
• Processes are concurrent
• There is no “main” process. Everything is a process and there are not special types of processes
Processes • Processes are independent of each other
• Processes cannot modify each other
• No data is shared between processes
• Processes can only interact with each other via message passing
• Processes are extremely lightweight
• Processes can be linked together, processes can also be monitored by other processes
Processes are Actors • Implements the actor model
• Processes encapsulate state
• Elixir’s processes are more object-oriented than objects in object-oriented languages
Elixir • Dynamic
• Functional
• Built on top of the Erlang VM
• Leverages the VM to build concurrent, distributed and fault-tolerant applications.
• Created in 2011 by José Valim
• First major release expect sometime in the next month
Why use Elixir? • All the benefits of Erlang
• Easily reuse Erlang libraries
• No additional performance costs
• Better tooling, which allows for greater productivity
• Better syntax
• Simplified metaprogramming
Data Types • All data is immutable
• Atoms, just like symbols in Ruby
• Booleans have been faked using the atoms ‘true’ and ‘false’
#
Atoms
:hello
#
Atoms
can
be
pattern
matched
:a
=
:a
Functions • In Ruby, functions/methods are identified by name, in Elixir they are identified by name and arity.
def
add(0,
0)
do:
0
def
add(x,
y)
do:
x
+
y
Pattern Matching #
In
Elixir,
just
like
in
Erlang,
the
`=`
denotes
pattern
matching
:a
=
:a
#=>
:a
:a
=
:b
#=>
**
(MatchError)
no
match
of
right
hand
side
value:
:b
#
Atoms
can
be
used
in
pattern
matching
{:ok,
foo}
=
{:ok,
"foo
bar
baz"}
foo
#=>
“foo
bar
baz”
{:ok,
foo}
=
{:error,
:crashed}
#=>
**
(MatchError)
no
match
of
right
hand
side
value:
{:error,
:crashed}
Recursion • There are no loops, only recursion
#
Added
all
the
items
in
the
list
together
to
compute
the
sum
def
sum_list([head
|
tail],
acc)
do
sum_list(tail,
acc
+
head)
end
Processes • Message being sent to a process
• A process receiving messages
#
`pid`
is
a
process
message
=
{:add,
1,
2}
send
pid,
message
#
Listen
for
messages
receive
do
{:add,
x,
y}
-‐>
x
+
y
{:subtract,
x,
y}
-‐>
x
-‐
y
_
-‐>
:error
end
Process Relationships • Process spawn other processes
• Processes can be linked
• Processes can also monitor other processes
#
Spawn
a
process
pid
=
spawn(worker_fun)
#
Link
the
process
calling
`link`
function
with
`pid`
process
link
pid
#
Monitor
`pid`
process
from
the
process
calling
the
`monitor`
function
monitor
pid
ProcessChain • Thousands of processes arranged in a loop passing hundreds of messages around the loop.
• Demonstrates the efficiency of the Erlang VM’s message passing and lightweight nature of processes
ProcessChain <0.97.0>
<0.38.0>
Chain Link
Controller
<0.83.0>
Chain Link
<0.84.0>
Chain Link
<0.77.0>
Chain Link
<0.43.0>
Chain Link
<0.26.0>
Shell (Us)
Tennis • Two processes maintaining their own state, sending messages back an forth, with processes randomly failing to send a response
<0.83.0>
<0.97.0>
Player 1
Player 2
{:ping,
<0.83.0>}
{:ping,
<0.97.0>}
<0.59.0>
Shell (Us)
Things I didn’t cover • OTP - Open Telecom Platform
• Communication between nodes on different machines
• Metaprogramming
• Protocols
• The amazing pipe operator
• List comprehensions
• Much more!