Erlang 
Elixir Webilea Hands-On Session 
at Obst & Gemüse, 19.3.2014 
By André Graf 
@der_graf / @erlio_basel
Preliminaries 
● Erlang and Elixir Programs are compiled for 
the BEAM* 
● Similar datatypes and runtime behavior 
● Different syntax, language features, stdlib 
*Bogdan/Björn's Erlang Abstract Machine
1986 
Erlang has been developed 
by Ericsson, for building 
highly available telco 
systems 
Massively concurrent 
server systems 
Server systems 
that run forever 
Hot-Code 
Upgrades 
Inherently parallel 
problem domain 
High availability 
through redundancy 
Debugging 
Live Systems
● Functional 
● Compiled 
● Dynamically typed 
● VM & Shell 
● Very lightweight processes 
● Processes don't share state 
● Message Passing Concurrency 
● Process Supervision (Let it crash) 
● Built-in Distribution 
● Hot-Code Upgrades
Erlang OTP 
● Architecture Patterns 
● Middlewares 
● Libraries 
● Tools 
● You could use Erlang witout OTP (nobody does) 
● OTP is included in the Erlang distribution 
● OTP is closely tied to the Erlang VM
1998 
Erlang has been 
opensourced and other 
companies started using it. 
Ericsson went for C++ 
Massively concurrent 
server systems 
Server systems 
that run forever 
Hot-Code 
Upgrades 
Inherently parallel 
problem domain 
High availability 
through redundancy 
Debugging 
Live Systems
2007
2012
● Functional 
● Compiled 
● Dynamically typed 
● VM & Shell 
● Very lightweight processes 
● Processes don't share state 
● Message Passing Concurrency 
● Process Supervision (Let it crash) 
● Built-in Distribution 
● Hot-Code Upgrades
Erlang vs. Elixir 
very subjective 
+ Elixir has a more familiar Syntax. 
+ Elixir fixes several language design mistakes 
from the past. * 
+ Elixir will bring more people to the Beam. 
+ Elixir has better meta-programming support. 
- Elixir has (currently) no own OTP framework. 
> killer for production use. 
* http://joearms.github.io/2013/05/31/a-week-with-elixir.html
Erlang Hands-On 
1. Installation Party 
2. Language introduction http://www.tryerlang.org/ 
3. My first Erlang module 
4. My first Erlang process 
5. Let it crash 
6. OTP behaviours gen_server / supervisor 
7. Recap / Discussion
Language Introduction 
Goto http://www.tryerlang.org/
My first Erlang Module 
● Module-Name == Filename.erl 
– e.g. my_first_module.erl 
-module(my_first_module). 
-export([my_first_function/1]). 
% this is a comment for my first function 
% this is another line of the comment 
my_first_function(Param) -> 
io:format(“wow my first function got called with param: ~p~n”, [Param]). 
1. Compile with erlc my_first_module.erl 
2. Start Erlang shell with: erl 
3. Call function my_first_module:my_first_function(“hello world”).
My first Erlang Process 
-module(my_first_module). 
-export([my_first_function/0, 
start_my_first_process/0]). 
my_first_function() -> 
receive 
Msg -> 
io:format(“wow my first process got a msg: ~p~n”, [Msg]) 
end. 
start_my_first_process() -> 
spawn(?MODULE, my_first_function, []). 
1. Compile with erlc my_first_module.erl 
or if you are inside the shell with: 
c(my_first_module). 
l(my_first_module). 
2. Start your first process: 
Pid = my_first_module:start_my_first_process(). 
3. Send your first message: 
Pid ! yeah_this_is_my_first_message. 
4. How can you reuse the Process???
Let it Crash 
-module(my_first_module). 
-export([my_first_function/0, 
start_my_first_process/0]). 
my_first_function() -> 
receive 
Msg -> 
dispatch_my_msg(Msg), 
my_first_function() %% yay, recursion! 
end. 
start_my_first_process() -> 
spawn(?MODULE, my_first_function, []). 
dispatch_my_msg({hello, world}) -> 
io:format(“what a great hello world~n”); 
dispatch_my_msg({A, B}) -> 
io:format(“tuple with ~p and ~p~n”, [A,B]). 
1. kinda stupid dispatcher, right???
Let it Crash 
-module(my_first_module). 
-export([my_first_function/0, 
start_my_first_monitored_process/1, 
my_monitor/1]). 
my_first_function() -> 
receive 
Msg -> 
dispatch_my_msg(Msg), 
my_first_function() 
end. 
my_monitor(ProcName) when is_atom(ProcName) -> 
Pid = spawn(?MODULE, my_first_function, []), 
true = register(ProcName, Pid), 
MonitorRef = monitor(process, Pid), 
receive 
{'DOWN', MonitorRef, _Type, Pid, _Info} -> 
my_monitor(ProcName); 
stop -> 
demonitor(MonitorRef) 
end. 
start_my_first_monitored_process(ProcName) -> 
spawn(?MODULE, my_monitor, [ProcName]). 
...
OTP Behaviours 
● gen_server 
– Generic way to write servers in Erlang OTP Style 
● Supervisor 
– A Supervisor that supervises other OTP compliant 
processes (e.g. gen_servers, gen_fsms)
gen_server 
-module(sample_gen_server). 
-behaviour(gen_server). 
-export([start_link/0]). 
%% gen_server callbacks 
-export([init/1, 
handle_call/3, 
handle_cast/2, 
handle_info/2, 
terminate/2, 
code_change/3]). 
%%% API 
start_link() -> 
gen_server:start_link(?MODULE, [], []). 
%%% gen_server callbacks 
init([]) -> 
{ok, []}. 
handle_call(_Request, _From, State) -> 
Reply = ok, 
{reply, Reply, State}. 
handle_cast(_Msg, State) -> 
{noreply, State}. 
handle_info(_Info, State) -> 
{noreply, State}. 
terminate(_Reason, _State) -> 
ok. 
code_change(_OldVsn, State, _Extra) -> 
{ok, State}.
supervisor -module(sample_supervisor). 
-behaviour(supervisor). 
-export([start_link/0]). 
%% Supervisor callbacks 
-export([init/1]). 
%% =================================================================== 
%% API functions 
%% =================================================================== 
start_link() -> 
supervisor:start_link({local, ?MODULE}, ?MODULE, []). 
%% =================================================================== 
%% Supervisor callbacks 
%% =================================================================== 
init([]) -> 
{ok, { {one_for_one, 5, 10}, [ %% 5 restarts within 10 seconds are allowed 
{sample_gen_server, { 
sample_gen_server, start_link, [] 
}, permanent, 5000, worker, [sample_gen_server]} 
]}}.
Recap/Discussion
Erlang 
Elixir Webilea Hands-On Session 
at Obst & Gemüse, 19.3.2014 
By André Graf 
@der_graf / @erlio_basel

Introduction to Erlang/(Elixir) at a Webilea Hands-On Session

  • 1.
    Erlang Elixir WebileaHands-On Session at Obst & Gemüse, 19.3.2014 By André Graf @der_graf / @erlio_basel
  • 2.
    Preliminaries ● Erlangand Elixir Programs are compiled for the BEAM* ● Similar datatypes and runtime behavior ● Different syntax, language features, stdlib *Bogdan/Björn's Erlang Abstract Machine
  • 3.
    1986 Erlang hasbeen developed by Ericsson, for building highly available telco systems Massively concurrent server systems Server systems that run forever Hot-Code Upgrades Inherently parallel problem domain High availability through redundancy Debugging Live Systems
  • 4.
    ● Functional ●Compiled ● Dynamically typed ● VM & Shell ● Very lightweight processes ● Processes don't share state ● Message Passing Concurrency ● Process Supervision (Let it crash) ● Built-in Distribution ● Hot-Code Upgrades
  • 5.
    Erlang OTP ●Architecture Patterns ● Middlewares ● Libraries ● Tools ● You could use Erlang witout OTP (nobody does) ● OTP is included in the Erlang distribution ● OTP is closely tied to the Erlang VM
  • 6.
    1998 Erlang hasbeen opensourced and other companies started using it. Ericsson went for C++ Massively concurrent server systems Server systems that run forever Hot-Code Upgrades Inherently parallel problem domain High availability through redundancy Debugging Live Systems
  • 7.
  • 9.
  • 10.
    ● Functional ●Compiled ● Dynamically typed ● VM & Shell ● Very lightweight processes ● Processes don't share state ● Message Passing Concurrency ● Process Supervision (Let it crash) ● Built-in Distribution ● Hot-Code Upgrades
  • 11.
    Erlang vs. Elixir very subjective + Elixir has a more familiar Syntax. + Elixir fixes several language design mistakes from the past. * + Elixir will bring more people to the Beam. + Elixir has better meta-programming support. - Elixir has (currently) no own OTP framework. > killer for production use. * http://joearms.github.io/2013/05/31/a-week-with-elixir.html
  • 12.
    Erlang Hands-On 1.Installation Party 2. Language introduction http://www.tryerlang.org/ 3. My first Erlang module 4. My first Erlang process 5. Let it crash 6. OTP behaviours gen_server / supervisor 7. Recap / Discussion
  • 13.
    Language Introduction Gotohttp://www.tryerlang.org/
  • 14.
    My first ErlangModule ● Module-Name == Filename.erl – e.g. my_first_module.erl -module(my_first_module). -export([my_first_function/1]). % this is a comment for my first function % this is another line of the comment my_first_function(Param) -> io:format(“wow my first function got called with param: ~p~n”, [Param]). 1. Compile with erlc my_first_module.erl 2. Start Erlang shell with: erl 3. Call function my_first_module:my_first_function(“hello world”).
  • 15.
    My first ErlangProcess -module(my_first_module). -export([my_first_function/0, start_my_first_process/0]). my_first_function() -> receive Msg -> io:format(“wow my first process got a msg: ~p~n”, [Msg]) end. start_my_first_process() -> spawn(?MODULE, my_first_function, []). 1. Compile with erlc my_first_module.erl or if you are inside the shell with: c(my_first_module). l(my_first_module). 2. Start your first process: Pid = my_first_module:start_my_first_process(). 3. Send your first message: Pid ! yeah_this_is_my_first_message. 4. How can you reuse the Process???
  • 16.
    Let it Crash -module(my_first_module). -export([my_first_function/0, start_my_first_process/0]). my_first_function() -> receive Msg -> dispatch_my_msg(Msg), my_first_function() %% yay, recursion! end. start_my_first_process() -> spawn(?MODULE, my_first_function, []). dispatch_my_msg({hello, world}) -> io:format(“what a great hello world~n”); dispatch_my_msg({A, B}) -> io:format(“tuple with ~p and ~p~n”, [A,B]). 1. kinda stupid dispatcher, right???
  • 17.
    Let it Crash -module(my_first_module). -export([my_first_function/0, start_my_first_monitored_process/1, my_monitor/1]). my_first_function() -> receive Msg -> dispatch_my_msg(Msg), my_first_function() end. my_monitor(ProcName) when is_atom(ProcName) -> Pid = spawn(?MODULE, my_first_function, []), true = register(ProcName, Pid), MonitorRef = monitor(process, Pid), receive {'DOWN', MonitorRef, _Type, Pid, _Info} -> my_monitor(ProcName); stop -> demonitor(MonitorRef) end. start_my_first_monitored_process(ProcName) -> spawn(?MODULE, my_monitor, [ProcName]). ...
  • 18.
    OTP Behaviours ●gen_server – Generic way to write servers in Erlang OTP Style ● Supervisor – A Supervisor that supervises other OTP compliant processes (e.g. gen_servers, gen_fsms)
  • 19.
    gen_server -module(sample_gen_server). -behaviour(gen_server). -export([start_link/0]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). %%% API start_link() -> gen_server:start_link(?MODULE, [], []). %%% gen_server callbacks init([]) -> {ok, []}. handle_call(_Request, _From, State) -> Reply = ok, {reply, Reply, State}. handle_cast(_Msg, State) -> {noreply, State}. handle_info(_Info, State) -> {noreply, State}. terminate(_Reason, _State) -> ok. code_change(_OldVsn, State, _Extra) -> {ok, State}.
  • 20.
    supervisor -module(sample_supervisor). -behaviour(supervisor). -export([start_link/0]). %% Supervisor callbacks -export([init/1]). %% =================================================================== %% API functions %% =================================================================== start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []). %% =================================================================== %% Supervisor callbacks %% =================================================================== init([]) -> {ok, { {one_for_one, 5, 10}, [ %% 5 restarts within 10 seconds are allowed {sample_gen_server, { sample_gen_server, start_link, [] }, permanent, 5000, worker, [sample_gen_server]} ]}}.
  • 21.
  • 22.
    Erlang Elixir WebileaHands-On Session at Obst & Gemüse, 19.3.2014 By André Graf @der_graf / @erlio_basel