Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Erlang

Introduction to Erlang

Second part of the first Fun(Lx) meetup: an intro to Erlang.
The slides are numerous due to code examples.

João Duarte

March 07, 2014
Tweet

More Decks by João Duarte

Other Decks in Programming

Transcript

  1. Erlang - Types 1 % number “Hi!” % string hello

    % atom F % variable true % boolean <<”1”>> % binary F = fun() -> “hi!” end % function spawn(F) % process
  2. Erlang - Data Structures {“João Duarte”, 27} % tuple [1,2,3,hello,“hi”,5]

    % list % other structures: % sets, proplists, dicts, digraphs, queues
  3. Erlang - Modules -module(funlx). -export([first_meetup/1, hi/0]). hi() -> “Hi, Fun(Lx)”.

    first_meetup(presenters) -> [“Joao”, “Nick”]. funlx.erl
  4. Erlang - REPL $ erl Erlang R13B01 (erts-5.7.2) [source] [smp:2:2]

    [rq:2] [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.7.2 (abort with ^G) 1>
  5. Erlang - REPL $ erl .. 1> c(funlx). {ok, funlx}

    2> funlx:hi(). “Hi Fun(Lx)” 3>
  6. Erlang - REPL $ erl .. 1> c(funlx). {ok, funlx}

    2> funlx:hi(). “Hi Fun(Lx)” 3> funlx:first_meetup(presenters). [“João”, “Nick”]
  7. Erlang - Immutability > X = 1. 1 > X

    = X + 1. exception error: no match of right hand side value 2 (╯°□°)╯︵ ┻━┻
  8. Erlang - Immutability > X = 1. 1 > X1

    = X + 1. 2 ┬─┬ ノ( ゜-゜ノ)
  9. Erlang - Immutability When you see “=” Don’t think about

    assigment Remember that Erlang is declarative “X = 1.” is an assertion. “X = X + 1.” makes no (mathematical) sense!
  10. Erlang - Pattern Matching 1> {Var1, “hi”, [1,Var2,3]} = {1,

    “hi”, [1,2,3]}. {1, “hello”, [1,2,3]} 2>
  11. Erlang - Pattern Matching 1> {Var1, “hi”, [1,Var2,3]} = {1,

    “hi”, [1,2,3]}. {1, “hello”, [1,2,3]} 2> Var1. 1 3> Var2. 2
  12. Erlang - Pattern Matching > L = [1,2,3,4]. [1,2,3,4] >

    [Head | Tail] = L. [1,2,3,4] > Head. 1 > Tail. [2,3,4]
  13. Erlang - Pattern Matching > L = [1]. [1] >

    [Head | Tail] = L. [1] > Head. 1 > Tail.
  14. Erlang - Pattern Matching > L = [1]. [1] >

    [Head | Tail] = L. [1] > Head. 1 > Tail. []
  15. Erlang - Pattern Matching local_greet(Language) -> if Language == portuguese

    -> “Olá”; Language == english -> “Hi”; Language == russian -> “привет”; true -> “你好” end.
  16. Erlang - Pattern Matching local_greet(Language) -> case Language of portuguese

    -> “Olá”; english -> “Hi”; russian -> “привет”; _ -> “你好” end.
  17. Erlang - Pattern Matching local_greet(portuguese) -> “Olá”; local_greet(english) -> “Hi”;

    local_greet(russian) -> “привет”; local_greet(_) -> “你好”.
  18. Erlang - Pattern Matching DbUri = {mysql, {“192.168.1.11”, 3306}, {user,

    pass}}. get_port(Uri) -> IpPort = element(2, Uri), element(2, IpPort).
  19. Erlang - Pattern Matching DbUri = {mysql, {“192.168.1.11”, 3306}, {user,

    pass}}. get_port({mysql, {_, Port}, _}) -> Port.
  20. Erlang - Pattern Matching day_period(Hour) when Hour =< 11 ->

    “a.m.”; day_period(Hour) when Hour >= 12 -> “p.m.”.
  21. Erlang - Processes 1> F = fun() -> timer:sleep(10000) end.

    #Fun<erl_eval.20.67289768> 2> Pid = spawn(F). <0.83.0> 3>
  22. Erlang - Processes 1> F = fun() -> timer:sleep(10000) end.

    #Fun<erl_eval.20.67289768> 2> Pid = spawn(F). <0.83.0> 3> Pid2 = spawn(F). <0.84.0> 4>
  23. Erlang - Processes 4> Pid ! “hi”. “hi” 5> exit(Pid,

    “because I want to”). true 6> erlang:process_info(Pid). undefined 7>
  24. Erlang - Processes -module(funlx). -export([do_addition/3, adder/0]). do_addition(Adder, X, Y) ->

    Adder ! {self(), X, Y}, io:format(“Calculating..~n”), receive Result -> Result end.
  25. Erlang - Processes do_addition(Adder, X, Y) -> Adder ! {self(),

    X, Y}, io:format(“Calculating..~n”), receive Result -> Result end. adder() -> receive {From, X, Y} -> From ! X + Y end.
  26. Erlang - Processes 1> c(funlx). {ok, funlx} 2> P =

    spawn(fun funlx:adder/0). <0.83.0> 3>
  27. Erlang - Processes 1> c(funlx). {ok, funlx} 2> P =

    spawn(fun funlx:adder/0). <0.83.0> 3> funlx:do_addition(P, 10, 32). Calculating.. 42 4>
  28. Erlang - Processes 1> c(funlx). {ok, funlx} 2> P =

    spawn(fun funlx:adder/0). <0.83.0> 3> funlx:do_addition(P, 10, 32). Calculating.. 42 4> funlx:do_addition(P, 10, 32). Calculating.. ???
  29. Erlang - Processes 1> c(funlx). {ok, funlx} 2> P =

    spawn(fun funlx:adder/0). <0.83.0> 3> funlx:do_addition(P, 10, 32). Calculating.. 42 4> funlx:do_addition(P, 10, 32). Calculating.. ⌛
  30. Erlang - Processes do_addition(Adder, X, Y) -> Adder ! {self(),

    X, Y}, io:format(“Calculating..~n”), receive Result -> Result end. adder() -> receive {From, X, Y} -> From ! X + Y end.
  31. Erlang - Processes do_addition(Adder, X, Y) -> Adder ! {self(),

    X, Y}, io:format(“Calculating..~n”), receive Result -> Result end. adder() -> receive {From, X, Y} -> From ! X + Y end, adder().
  32. Erlang - Processes 1> c(funlx). {ok, funlx} 2> P =

    spawn(fun funlx:adder/0). <0.83.0> 3> funlx:do_addition(P, 10, 32). Calculating.. 42 4> funlx:do_addition(P, 1, 3). Calculating.. 4
  33. Erlang - Processes do_addition(Adder, X, Y) -> Adder = spawn(fun

    funlx:adder/0), Adder ! {self(), X, Y}, io:format(“Calculating..~n”), receive Result -> Result end. adder() -> receive {From, X, Y} -> From ! X + Y end.
  34. Erlang - Processes 1> c(funlx). {ok, funlx} 2> funlx:do_addition(10, 32).

    Calculating.. 42 4> funlx:do_addition(1, 3). Calculating.. 4
  35. Erlang - Processes 5> funlx:do_addition(1, “hello”). Calculating.. =ERROR REPORT==== 5-Mar-2014::20:24:06

    === Error in process <0.83.0> with exit value: {badarith,[{funlx,adder,0,[{file,"funlx. erl"},{line,14}]}]} ⌛
  36. Erlang - Processes do_addition(X, Y) -> Adder = spawn_link(fun funlx:adder/0),

    Adder ! {self(), X, Y}, io:format(“Calculating..~n”), receive Result -> Result end. adder() -> receive {From, X, Y} -> From ! X + Y end.
  37. Erlang - Processes 1> c(funlx). {ok, funlx} 2> funlx:do_addition(1, “hello”).

    Calculating.. =ERROR REPORT==== 5-Mar-2014::20:24:06 === Error in process <0.83.0> with exit value: {badarith,[{funlx,adder,0,[{file," funlx.erl"},{line,14}]}]} 3>
  38. Erlang - Processes do_addition(X, Y) -> process_flag(trap_exit, true), Adder =

    spawn_link(fun funlx:adder/0), Adder ! {self(), X, Y}, io:format(“Calculating..~n”), receive Result -> Result end.
  39. Erlang - Processes 1> c(funlx). {ok, funlx} 2> funlx:do_addition(1, “hello”).

    Calculating.. {'EXIT',<0.41.0>, {badarith,[{funlx,adder,0, [{file,"funlx.erl"},{line,32}]}]}} =ERROR REPORT==== 5-Mar-2014::20:24:06 === Error in process <0.41.0> with exit value: {badarith,[{funlx,adder,0,[{file," funlx.erl"},{line,14}]}]} 3>
  40. Erlang - Processes do_addition(X, Y) -> process_flag(trap_exit, true), Adder =

    spawn_link(fun funlx:adder/0), Adder ! {self(), X, Y}, io:format(“Calculating..~n”), receive {‘EXIT’, _, _} -> error; Result -> Result end.
  41. Erlang - Processes 1> c(funlx). {ok, funlx} 2> Result =

    funlx:do_addition(1, “hello”). Calculating.. error =ERROR REPORT==== 5-Mar-2014::20:24:06 === Error in process <0.41.0> with exit value: {badarith,[{funlx,adder,0,[{file," funlx.erl"},{line,14}]}]} 4> Result. error