Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Where do I Start? 

Slide 3

Slide 3 text

http://learnyousomeerlang.com

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

http://learnyouhaskell.com

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Books

Slide 8

Slide 8 text

Erlang

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Getting Started

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Data Types

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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]

Slide 22

Slide 22 text

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"

Slide 23

Slide 23 text

Functional Programming ∞

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

λ-calculus vs Turing machine

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Functions in Modules -­‐module(example2). -­‐export([sumList/1]). sumList(List)  -­‐>  sumList(List,  0). sumList([],  Acc)  -­‐>  Acc; sumList([H|T],  Acc)  -­‐>  sumList(T,  H  +  Acc).

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Concurrency Model

Slide 32

Slide 32 text

Processes spawn_link spawn_monitor Pid1 ☰ Pid2 ☰ Pid2 ☰ Pid1 ☰ message queue

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Distributed Nodes

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

OTP ⚙

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Tools ⚒

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

Questions? Alexander Zhuravlev [email protected] @zaa 