Share
Embed
Transcript
40 slides
-
Erlang/OTP Las Sagas Nórdicas Julián David Duque Organizador MedellínJS Ingeniero
NodeSource @julian_duque - http://about.me/julianduqe -
Erlang es un lenguaje funcional de propósito general, especial para
aplicaciones concurrentes, distribuidas y tolerantes a fallos -
Los procesos en Erlang son un concepto fundamental Una aplicación
Erlang se puede dividir en múltiples procesos que corren aislados en la máquina virtual -
Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Eshell V7.1 (abort with ^G) 1> 42. 42 2> 1 + 1. 2 3> 45 - 3. 42 4> "Thor". "Thor" 5> Cada sentencia termina en `.` Erlang Shell (erl) -
Tipos de datos Números (enteros y punto flotante) Binarios /
Bitstrings Atoms Tuples Listas (y strings) Identificadores Únicos (Pids, Ports, References) Funs -
Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Eshell V7.1 (abort with ^G) 1> 42. 42 2> -42. -42 3> 16#FF6600. 16737792 4> 2#101101. 45 5> $9. 57 6> 3.141592. 3.141592 7> 1.68e23. 1.68e23 8> Números -
Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Eshell V7.1 (abort with ^G) 1> Name = "Thor". "Thor" 2> Name = "Loki". ** exception error: no match of right hand side value "Loki" 3> Name. "Thor" 4> "Thor" = Name. "Thor" 5> Variables con primera letra mayúscula Todas las variables son inmutables Variables -
Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Eshell V7.1 (abort with ^G) 1> << "hello", 32, "my", 32, "name", 32, "is", 32, "Thor" >>. <<"hello my name is Thor">> 2> << 255, 0, 0 >>. <<255,0,0>> 3> Binary / Bitstrings Secuencias de bytes Usados para guardar `chunks` de información -
Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Eshell V7.1 (abort with ^G) 1> name. name 2> ok. ok 3> error. error 4> error = error. error 5> error == error. true 6> ok == error. false 7> El nombre del atom es su mismo valor Atoms en Erlang se escriben en minúscula Atoms -
Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Eshell V7.1 (abort with ^G) 1> {1, 2, 3}. {1,2,3} 2> {one, two, three}. {one,two,three} 3> {name, “Thor”, weapon, “Mjolnir”}. {name,”Thor”,weapon,”Mjolnir”} 4> {ok, "Everything is fine in Midgard"}. {ok,"Everything is fine in Midgard"} 5> {point, 0, 0}. {point,0,0} 6> {point, 10, -10}. {point,10,-10} 7> Secuencia de términos Erlang Tuples -
Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Eshell V7.1 (abort with ^G) 1> [1, 2, 3]. [1,2,3] 2> []. [] 3> [one, two, three]. [one,two,three] 4> [[1,2,3],[4,5,6]]. [[1,2,3],[4,5,6]] 5> [{name, "Thor"}, {weapon, "Mjolnir"}]. [{name,"Thor"},{weapon,"Mjolnir"}] 6> El tipo de dato más importante de Erlang en cuanto a su aspecto funcional Listas -
Identificadores Únicos Pids (Identificador de Procesos) Ports (Identificador de Puertos)
Son como Pids pero son usados para comunicación con el mundo exterior References creados con la función make_ref() sirven como identificadores únicos o cookies. -
Funs Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]
[dtrace] Eshell V7.1 (abort with ^G) 1> Add = fun(A, B) -> A + B end. #Fun<erl_eval.12.54118792> 2> Add(39, 3). 42 3> Funciones anónimas (lambda o closure) -
Módulos y Funciones Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10]
[hipe] [kernel-poll:false] [dtrace] Eshell V7.1 (abort with ^G) 1> c("thor.erl"). {ok,thor} 2> thor:smash(). I will smash you with my hammer ok 3> thor:smash("Loki"). I will smash "Loki" with my hammer ok 4> -module(thor). -export([smash/0, smash/1]). smash() -> io:format("I will smash you with my hammer~n"). smash(Target) -> io:format("I will smash ~p with my hammer~n", [ Target ]). -
Pattern Matching Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe]
[kernel-poll:false] [dtrace] Eshell V7.1 (abort with ^G) 1> Name = "Thor". "Thor" 2> "Thor" = Name. "Thor" 3> Name = "Loki". ** exception error: no match of right hand side value "Loki" 4> {god, Name, Weapon} = {god, "Thor", "Mjolnir"}. {god,"Thor","Mjolnir"} 5> Name. "Thor" 6> Weapon. "Mjolnir" 7> -
Pattern Matching (Funciones) -module(maths). -export([add/1, add/2]). add(A, B) -> A
+ B. add({A, B}) -> A + B; add([A, B]) -> A + B. -
Pattern Matching (Funciones) Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10]
[hipe] [kernel-poll:false] [dtrace] Eshell V7.1 (abort with ^G) 1> c("maths.erl"). {ok,maths} 2> maths:add(2, 3). 5 3> maths:add({2, 3}). 5 4> maths:add([2, 3]). 5 5> maths:add([2, 3, 4]). ** exception error: no function clause matching maths:add([2,3,4]) (maths.erl, line 7) 6> -
Pattern Matching (Case … of) -module(thor). -export([smash/0, smash/1]). smash() ->
io:format("I will smash you with my hammer!~n"). smash(Target) -> case Target of {family, Name} -> io:format("I will not fight against ~p~n", [Name]); {enemy, Name} -> io:format("I will smash ~p with my hammer~n!", [Name]); _ -> smash() end. -
Pattern Matching (Case … of) Erlang/OTP 18 [erts-7.1] [source] [64-bit]
[smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace] Eshell V7.1 (abort with ^G) 1> c("thor.erl"). {ok,thor} 2> thor:smash({family, "Odin"}). I will not fight against "Odin" ok 3> thor:smash({enemy, "A Frost Giant"}). I will smash "A Frost Giant" with my hammer !ok 4> thor:smash("Someone else"). I will smash you with my hammer! ok 5> -
Function Guards Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe]
[kernel-poll:false] [dtrace] Eshell V7.1 (abort with ^G) 1> c("maths.erl"). {ok,maths} 2> maths:add(1,2). 3 3> maths:add("1", "2"). ** exception error: no function clause matching maths:add("1","2") (maths.erl, line 4) 4> -module(maths). -export([add/2]). add(A, B) when is_number(A) and is_number(B) -> A + B. -
Function Guards Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe]
[kernel-poll:false] [dtrace] Eshell V7.1 (abort with ^G) 1> erlang:is_ is_alive/0 is_atom/1 is_binary/1 is_bitstring/1 is_boolean/1 is_builtin/3 is_float/1 is_function/1 is_function/2 is_integer/1 is_list/1 is_map/1 is_number/1 is_pid/1 is_port/1 is_process_alive/1 is_record/2 is_record/3 is_reference/1 is_tuple/1 Además de otros condicionales y operaciones -
Recursividad -module(maths). -export([factorial/1]). factorial(0) -> 1; factorial(N) when N >
0 -> N * factorial(N - 1). Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace] Eshell V7.1 (abort with ^G) 1> c("maths.erl"). {ok,maths} 2> maths:factorial(0). 1 3> maths:factorial(1). 1 4> maths:factorial(6). 720 -
Recursividad -module(maths). -export([sum/1]). sum([]) -> 0; sum([Head | Tail]) ->
Head + sum(Tail). Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace] Eshell V7.1 (abort with ^G) 1> c("maths.erl"). {ok,maths} 2> maths:sum([]). 0 3> maths:sum([1]). 1 4> maths:sum([1,2]). 3 5> maths:sum([1,2,3]). 6 6> -
Procesos Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]
[dtrace] Eshell V7.1 (abort with ^G) 1> Pid = self(). <0.33.0> 2> Pid ! "Hello". "Hello" 3> Pid ! "World". "World" 4> flush(). Shell got "Hello" Shell got "World" ok 5> -
Procesos -module(processes). -export([run/0, ping/0]). run() -> Pid = spawn(fun ping/0),
Pid ! self(), receive pong -> ok end. ping() -> receive From -> From ! pong end. -
Procesos Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]
[dtrace] Eshell V7.1 (abort with ^G) 1> c("processes.erl"). {ok,processes} 2> processes:run(). ok -
Procesos -module(thor_process). -export([start/0]). start() -> Pid = spawn(fun smash/0), {ok,
Pid}. smash() -> receive {family, Name} -> io:format("I will not fight against ~p~n", [Name]); {enemy, Name} -> io:format("I will smash ~p with my hammer~n!", [Name]); _ -> io:format("I will smash you with my hamer~n!") end, smash(). -
Procesos Erlang/OTP 18 [erts-7.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]
[dtrace] Eshell V7.1 (abort with ^G) 1> c("thor_process.erl"). {ok,thor_process} 2> {ok, Pid} = thor_process:start(). {ok,<0.40.0>} 3> Pid ! {family, "Odin"}. I will not fight against "Odin" {family,"Odin"} 4> Pid ! {enemy, "Frost Giant"}. I will smash "Frost Giant" with my hammer !{enemy,"Frost Giant"} 5> Pid ! nothing. I will smash you with my hamer !nothing 6> -
OTP (Open Telecom Platform) Define el SASL (System Architecture Support
Libraries) Un conjunto de librerías y patrones de diseño para escribir aplicaciones en Erlang -
OTP (Open Telecom Platform) Behaviours, son los patrones de diseño
para escribir aplicaciones en Erlang/OTP application gen_server gen_fsm gen_event supervisor -
Gen_Server -module(...). -behaviour(gen_server). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -record(state,
{}). init([]) -> {ok, #state{}}. 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}. Instancia con start o start_link -
Permite ejecutar Erlang (y otros lenguajes que compilan a BEAM
como LFE y Elixir) Interfaces al mundo exterior (NIFs, Ports) Funciones incluidas en la VM (BIFs) (erlang:*) Erlang Virtual Machine (BEAM) -
Ligero Comunicación Asíncrona Aislamiento de Procesos Manejo de Errores (Let
it crash) Evolución Continua del Sistema (Hot reloading) Tiempo Real Caracteristicas