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 Selling Points Built in concurrency model Fault tolerance Soft

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

    1997 BEAM VM in C 1998 Open sourced 2006 SMP support
  3. 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> ...
  4. 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
  5. 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
  6. 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
  7. 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]
  8. 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"
  9. 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
  10. First class functions Higher-order functions Immutable data Referential transparency Recursion

    Tail call optimization Pattern matching Functional Languages Properties
  11. 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
  12. Functions in Modules -­‐module(example2). -­‐export([sumList/1]). sumList(List)  -­‐>  sumList(List,  0). sumList([],

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

         fun(X,  Acc)  -­‐>            X  +  Acc        end,        0,  List).
  14. 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
  15. 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
  16. 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
  17. Generic Behaviours supervisor Supervision trees gen_server Server gen_fsm Finite State

    Machine gen_event Event Processing application Applications
  18. 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}.
  19. Tools eunit Unit/Integration Testing ct Integration Testing cover Code coverage

    xref Cross reference checks rebar Build tool escript Erlang as script
  20. 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
  21. 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"} ]}.