Top Banner
Erlang Elixir Webilea Hands-On Session at Obst & Gemüse, 19.3.2014 By André Graf @der_graf / @erlio_basel
22

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

Jul 17, 2015

Download

Software

André Graf
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Erlang/(Elixir) at a Webilea Hands-On Session

Erlang

ElixirWebilea Hands-On Session at Obst & Gemüse, 19.3.2014

By André Graf@der_graf / @erlio_basel

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

● Erlang and Elixir Programs are compiled for the BEAM*

● Similar datatypes and runtime behavior● Different syntax, language features, stdlib

Preliminaries

*Bogdan/Björn's Erlang Abstract Machine

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

Erlang has been developed by Ericsson, for building

highly available telco systems

Massively concurrentserver systems

Server systems that run forever

Hot-Code Upgrades

Inherently parallelproblem domain

High availabilitythrough redundancy

DebuggingLive Systems

1986

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

● 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

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

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

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

Erlang has been opensourced and other

companies started using it.Ericsson went for C++

Massively concurrentserver systems

Server systems that run forever

Hot-Code Upgrades

Inherently parallelproblem domain

High availabilitythrough redundancy

DebuggingLive Systems

1998

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

2007

Page 8: Introduction to Erlang/(Elixir) at a Webilea Hands-On Session
Page 9: Introduction to Erlang/(Elixir) at a Webilea Hands-On Session

2012

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

● 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

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

Erlang vs. Elixirvery 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

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

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

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

Language Introduction

Goto http://www.tryerlang.org/

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

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 commentmy_first_function(Param) ->

io:format(“wow my first function got called with param: ~p~n”, [Param]).

1. Compile with erlc my_first_module.erl2. Start Erlang shell with: erl3. Call function my_first_module:my_first_function(“hello world”).

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

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???

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

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???

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

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]).

...

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

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)

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

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]).

%%% APIstart_link() -> gen_server:start_link(?MODULE, [], []).

%%% gen_server callbacksinit([]) -> {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}.

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

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]}]}}.

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

Recap/Discussion

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

Erlang

ElixirWebilea Hands-On Session at Obst & Gemüse, 19.3.2014

By André Graf@der_graf / @erlio_basel