• Simplifies concurrent programming • Fault-Tolerant (“let it crash” error handling philosophy) • Designed by Joe Armstrong in 1986 • Legendary nine 9’s of uptime
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
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
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
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
Erlang VM” • Elixir is VERY different from Ruby, but the syntax is similar – Functional vs OO – Pattern Matching – Recursion – Immutable Data Structures
Tuples that are stored contiguously in memory {1,2,3,4} # We can access a tuple element with the `elem` function elem({1, 2, 3}, 0) #=> 1 # Tuples can be pattern matched {:a, second, third} = {:a, :b, :c} second #=> :b third #=> :c # Tuple length matters when pattern matching {a, b, c} = {1, 2} #=> ** (MatchError) no match of right hand side value: {1,2} Tuples
Elixir, just like in Erlang, the `=` denotes pattern # matching and not an assignment. It is a "challenge" # We can access the head and tail of a list as follows [head|tail] = [1,2,3] head #=> 1 tail #=> [2,3] # Sometimes pattern matching fails [head|tail] = [] #=> ** (MatchError) no match of right hand side value: [] Lists
'char list' # char list # Strings are all encoded in UTF-‐8: "José" #=> "José" # `?a` returns the ASCII integer for the letter `a` ?a #=> 97 # Strings are really binaries, and char lists are linked lists. <<?a, ?b, ?c>> #=> "abc" [?a, ?b, ?c] #=> 'abc' Strings
`=` 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}
all the items in the list together to compute the sum def sum_list([head | tail], acc) do sum_list(tail, acc + head) end def sum_list([], acc) do acc End
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
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
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)