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

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.

Alexander Zhuravlev

June 15, 2013
Tweet

More Decks by Alexander Zhuravlev

Other Decks in Programming

Transcript

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

    View Slide

  2. Where
    do I Start?

    View Slide

  3. http://learnyousomeerlang.com

    View Slide

  4. View Slide

  5. http://learnyouhaskell.com

    View Slide

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

    View Slide

  7. Books

    View Slide

  8. Erlang

    View Slide

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

    View Slide

  10. Erlang (the language)
    Functional
    Strict evaluation
    Dynamic typing
    Concurrency oriented

    View Slide

  11. Erlang Selling Points
    Built in concurrency model
    Fault tolerance
    Soft realtime
    Pattern matching
    SMP support out of the box
    Very simple language

    View Slide

  12. Erlang History
    1986 First version in Prolog
    1990 Added distribution
    1997 BEAM VM in C
    1998 Open sourced
    2006 SMP support

    View Slide

  13. Getting
    Started

    View Slide

  14. Installation
    https://www.erlang-solutions.com/downloads/
    Erlang installation distributives for all major
    operating systems: Windows, Mac OS X, Linux

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  18. Data
    Types

    View Slide

  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 Port ID from open_port/2
    Fun #Fun First class function

    View Slide

  20. Collections
    List [1,2,3]
    Tuple {1, “two”, ‘three’}
    String “Hello, World”
    Record #example{field = “Value”}

    View Slide

  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]

    View Slide

  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"

    View Slide

  23. Functional
    Programming

    View Slide

  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

    View Slide

  25. λ-calculus
    vs
    Turing machine

    View Slide

  26. First class functions
    Higher-order functions
    Immutable data
    Referential transparency
    Recursion
    Tail call optimization
    Pattern matching
    Functional Languages Properties

    View Slide

  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
    2>  Sum(2,  3).
    5

    View Slide

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

    View Slide

  29. Higher Order Functions
    -­‐module(example3).
    -­‐export([sumList/1]).
    sumList(List)  -­‐>
       lists:foldl(
           fun(X,  Acc)  -­‐>
               X  +  Acc
           end,
           0,  List).

    View Slide

  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
    2>  Sum(1).
    #Fun
    3>  (Sum(1))(2).
    3
    4>  SumOne  =  Sum(1).
    #Fun
    5>  SumOne(5).
    6

    View Slide

  31. Concurrency
    Model

    View Slide

  32. Processes
    spawn_link
    spawn_monitor
    Pid1
    ☰ Pid2

    Pid2

    Pid1

    message queue

    View Slide

  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

    View Slide

  34. Distributed
    Nodes

    View Slide

  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

    View Slide

  36. OTP

    View Slide

  37. Generic Behaviours
    supervisor Supervision trees
    gen_server Server
    gen_fsm Finite State Machine
    gen_event Event Processing
    application Applications

    View Slide

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

    View Slide

  39. Tools

    View Slide

  40. Tools
    eunit Unit/Integration Testing
    ct Integration Testing
    cover Code coverage
    xref Cross reference checks
    rebar Build tool
    escript Erlang as script

    View Slide

  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

    View Slide

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

    View Slide

  43. Questions?
    Alexander Zhuravlev
    [email protected]
    @zaa

    View Slide