Erlang Jump Start

Erlang Jump Start

Short and to the point introduction into Erlang Programming language and ideas behind it.

Covered:
- Functional Programming Ideas
- Erlang as a Functional Language
- Erlang Data Types
- Erlang Concurrency Model
- OTP Introduction
- Useful Erlang Tools (eunit, rebar, cover, xref)

Presented on ULCamp::Dev (http://ulcamp.ru/dev) on June, 15th 2013.

Bf9e2ff97f5f5739fda92f21850c8b55?s=128

Alexander Zhuravlev

June 15, 2013
Tweet

Transcript

  1. Erlang Jump Start Alexander Zhuravlev, Echo Inc. http://aboutecho.com @zaa

  2. Where do I Start? 

  3. http://learnyousomeerlang.com

  4. None
  5. http://learnyouhaskell.com

  6. http://www.erlang.org/doc/ http://erldocs.com/ Reference Documentation

  7. Books

  8. Erlang

  9. Erlang is Erlang (the language) + OTP (Open Telecom Platform)

    + ERTS (Erlang RunTime System)
  10. Erlang (the language) Functional Strict evaluation Dynamic typing Concurrency oriented

  11. Erlang Selling Points Built in concurrency model Fault tolerance Soft

    realtime Pattern matching SMP support out of the box Very simple language
  12. Erlang History 1986 First version in Prolog 1990 Added distribution

    1997 BEAM VM in C 1998 Open sourced 2006 SMP support
  13. Getting Started

  14. Installation https://www.erlang-solutions.com/downloads/ Erlang installation distributives for all major operating systems:

    Windows, Mac OS X, Linux
  15. Erlang Shell zmac:~>  erl Erlang  R16B  (erts-­‐5.10.1)  [source]  [64-­‐bit]  [smp:2:2]

     [async-­‐ threads:10]  [hipe]  [kernel-­‐poll:false]  [dtrace] Eshell  V5.10.1    (abort  with  ^G) 1>  help(). **  shell  internal  commands  ** b()                -­‐-­‐  display  all  variable  bindings e(N)              -­‐-­‐  repeat  the  expression  in  query  <N> ...
  16. First Module -­‐module(example). -­‐export([sum/2]). sum(X,  Y)  -­‐>  X  +  Y.

    zmac:~>  erl Erlang  R16B  (erts-­‐5.10.1)  [source]  [64-­‐bit]  [smp:2:2]  [async-­‐ threads:10]  [hipe]  [kernel-­‐poll:false]  [dtrace] Eshell  V5.10.1    (abort  with  ^G) 1>  c(example). {ok,example} 2>  example:sum(1,  2). 3
  17. First Modification -­‐module(example). -­‐export([sum/2,  product/2]). sum(X,  Y)  -­‐>  X  +

     Y. product(X,  Y)  -­‐>  X  *  Y. 3>  c(example). {ok,example} 4>  example:product(1,  2). 2
  18. Data Types

  19. Data Types Integer 123456789 16#deadbeef Not limited in size Float

    1.20E-10 64 bit Atom ok Not garbage collected Reference #Ref<0.0.0.59> Unique value from make_ref/0 Binary <<139,123>> Sequence of bytes Pid <0.45.0> Process ID Port #Port<erl_eval.20.17052888> Port ID from open_port/2 Fun #Fun<erl_eval.20.17052888> First class function
  20. Collections List [1,2,3] Tuple {1, “two”, ‘three’} String “Hello, World”

    Record #example{field = “Value”}
  21. List Comprehensions zmac:~>  erl Erlang  R16B  (erts-­‐5.10.1)  [source]  [64-­‐bit]  [smp:2:2]

     [async-­‐ threads:10]  [hipe]  [kernel-­‐poll:false]  [dtrace] Eshell  V5.10.1    (abort  with  ^G) 1>  [X  +  1  ||  X  <-­‐  [1,  2,  3]]. [2,3,4] 2>  [X  +  Y  ||  X  <-­‐  [1,2,3],  Y  <-­‐  [10,  100,  1000]]. [11,101,1001,12,102,1002,13,103,1003] 3>  [X  +  Y  ||  X  <-­‐  [1,2,3],  Y  <-­‐  [10,  100,  1000],  X  +  Y  >  1000]. [1001,1002,1003]
  22. Property Lists zmac:~>  erl Erlang  R16B  (erts-­‐5.10.1)  [source]  [64-­‐bit]  [smp:2:2]

     [async-­‐ threads:10]  [hipe]  [kernel-­‐poll:false]  [dtrace] Eshell  V5.10.1    (abort  with  ^G) 1>  PL  =  [{a,  "value  for  a"},  {b,  "value  for  b"}]. [{a,"value  for  a"},{b,"value  for  b"}] 2>  proplists:get_value(a,  PL,  "default  value"). "value  for  a" 3>  proplists:get_value(c,  PL,  "default  value"). "default  value"
  23. Functional Programming ∞

  24. In functional programming, programs are executed by evaluating expressions, in

    contrast with imperative programming where programs are composed of statements which change global state when executed. Functional Languages Philosophy
  25. λ-calculus vs Turing machine

  26. First class functions Higher-order functions Immutable data Referential transparency Recursion

    Tail call optimization Pattern matching Functional Languages Properties
  27. Functions as Values zmac:~>  erl Erlang  R16B  (erts-­‐5.10.1)  [source]  [64-­‐bit]

     [smp:2:2]  [async-­‐ threads:10]  [hipe]  [kernel-­‐poll:false]  [dtrace] Eshell  V5.10.1    (abort  with  ^G) 1>  Sum  =  fun(X,  Y)  -­‐>  X  +  Y  end. #Fun<erl_eval.12.17052888> 2>  Sum(2,  3). 5
  28. Functions in Modules -­‐module(example2). -­‐export([sumList/1]). sumList(List)  -­‐>  sumList(List,  0). sumList([],

     Acc)  -­‐>  Acc; sumList([H|T],  Acc)  -­‐>  sumList(T,  H  +  Acc).
  29. Higher Order Functions -­‐module(example3). -­‐export([sumList/1]). sumList(List)  -­‐>    lists:foldl(  

         fun(X,  Acc)  -­‐>            X  +  Acc        end,        0,  List).
  30. Currying zmac:~>  erl Erlang  R16B  (erts-­‐5.10.1)  [source]  [64-­‐bit]  [smp:2:2]  [async-­‐

    threads:10]  [hipe]  [kernel-­‐poll:false]  [dtrace] Eshell  V5.10.1    (abort  with  ^G) 1>  Sum  =  fun(X)  -­‐>  fun(Y)  -­‐>  X  +  Y  end  end. #Fun<erl_eval.6.17052888> 2>  Sum(1). #Fun<erl_eval.6.17052888> 3>  (Sum(1))(2). 3 4>  SumOne  =  Sum(1). #Fun<erl_eval.6.17052888> 5>  SumOne(5). 6
  31. Concurrency Model

  32. Processes spawn_link spawn_monitor Pid1 ☰ Pid2 ☰ Pid2 ☰ Pid1

    ☰ message queue
  33. Message Passing zmac:~>  erl Erlang  R16B  (erts-­‐5.10.1)  [source]  [64-­‐bit]  [smp:2:2]

     [async-­‐ threads:10]  [hipe]  [kernel-­‐poll:false]  [dtrace] Eshell  V5.10.1    (abort  with  ^G) 1>  Self  =  self(). <0.32.0> 2>  Pid  =  spawn(fun()  -­‐>  receive  ping  -­‐>  Self  !  pong  end  end). <0.35.0> 3>  Pid  !  ping. ping 4>  Result  =  receive  Value  -­‐>  Value  after  5000  -­‐>  timeout  end. pong 5>  Result. pong
  34. Distributed Nodes

  35. Distributed Nodes zmac:~>  erl  -­‐sname  foo  -­‐setcookie  testcookie Erlang  R16B

     (erts-­‐5.10.1)  [source]  [64-­‐bit]  [smp:2:2]  [async-­‐ threads:10]  [hipe]  [kernel-­‐poll:false]  [dtrace] Eshell  V5.10.1    (abort  with  ^G) (foo@zmac)1>  net_adm:ping(boo@zmac). pong zmac:~>  erl  -­‐sname  boo  -­‐setcookie  testcookie Erlang  R16B  (erts-­‐5.10.1)  [source]  [64-­‐bit]  [smp:2:2]  [async-­‐ threads:10]  [hipe]  [kernel-­‐poll:false]  [dtrace] Eshell  V5.10.1    (abort  with  ^G) (boo@zmac)1>  net_adm:ping(foo@zmac). pong
  36. OTP ⚙

  37. Generic Behaviours supervisor Supervision trees gen_server Server gen_fsm Finite State

    Machine gen_event Event Processing application Applications
  38. gen_server behaviour -­‐module(counter_server). -­‐behaviour(gen_server). -­‐export([incr/0]). -­‐export([init/1,  handle_call/3,  handle_cast/2,  handle_info/2,  

    start_link/0]). start_link()  -­‐>        gen_server:start_link({local,  ?MODULE},  ?MODULE,  [],  []). incr()  -­‐>        gen_server:call(?MODULE,  incr). init(_Args)  -­‐>        {ok,  0}. handle_call(incr,  _From,  Counter)  -­‐>        {reply,  Counter,  Counter  +  1}.
  39. Tools ⚒

  40. Tools eunit Unit/Integration Testing ct Integration Testing cover Code coverage

    xref Cross reference checks rebar Build tool escript Erlang as script
  41. EUnit -­‐module(example5). -­‐export([sumList/1]). -­‐include_lib("eunit/include/eunit.hrl"). sumList(List)  -­‐>  lists:sum(List). sumList_test()  -­‐>  

                     ?assertEqual(3,  sumList([1,  2])). 1>  c(example5). {ok,example5} 2>  eunit:test(example5).    Test  passed. ok 3>  eunit:test(example5,  [verbose]). ========================  EUnit  ======================== example5:  sumList_test  (module  'example5')...ok =======================================================    Test  passed. ok
  42. Rebar {deps,  [    {lager,  "2.0.0",        

       {git,  "git://github.com/basho/lager.git",              {tag,  "e749242087706e480ecb530d99c5615e7e0dc7d6"}}}    ]}. {erl_opts,  [{parse_transform,  lager_transform},  debug_info]}. {cover_enabled,  true}. {xref_checks,  [undefined_function_calls,undefined_functions]}. {post_hooks,  [    {compile,  "rm  -­‐rf  ./apps/popcorn/ebin/*.mustache;"},    {compile,  "cp  ./apps/popcorn/priv/templates/*.mustache  ./ apps/popcorn/ebin"} ]}.
  43. Questions? Alexander Zhuravlev zaa@ikato.com @zaa 