Slide 1

Slide 1 text

A Chat About Erlang Bryce Kerley Miami Ruby Brigade February 18, 2013 Monday, February 18, 13

Slide 2

Slide 2 text

Administrativia Please check on on the Meetup iPhone or Android app. Monday, February 18, 13

Slide 3

Slide 3 text

Who Am I? Consulting Engineer Basho Technologies [email protected] @bonzoesc Monday, February 18, 13

Slide 4

Slide 4 text

Disclaimer The thoughts, views, and opinions in this presentation are mine, and not my employer’s. Monday, February 18, 13

Slide 5

Slide 5 text

Hello World io:format("Hello World\n"). Monday, February 18, 13

Slide 6

Slide 6 text

What is Erlang? Functional Distributed Reliable Monday, February 18, 13

Slide 7

Slide 7 text

1> X = 5. 5 2> Y = X. 5 3> 5 = Y. 5 4> Y = 6. ** exception error: no match Erlang is Functional Monday, February 18, 13

Slide 8

Slide 8 text

5 3> 5 = Y. 5 4> Y = 6. ** exception error: no match 5> Z = fun(N) -> N * 2 end. #Fun 6> Z. #Fun 7> Z(5). 10 Erlang is Functional Monday, February 18, 13

Slide 9

Slide 9 text

Erlang is Distributed > ./bin/riak attach Attaching to riak_ee-1.2.1rc2/dev/dev1 (^D to exit) ([email protected])1> nodes(). ['[email protected]','[email protected]','[email protected]', '[email protected]','[email protected]'] Monday, February 18, 13

Slide 10

Slide 10 text

Erlang is Reliable Illustration by Fred Hébert, from “Learn You Some Erlang for Great Good!” Monday, February 18, 13

Slide 11

Slide 11 text

Monday, February 18, 13

Slide 12

Slide 12 text

What is Erlang? Functional Distributed Reliable Monday, February 18, 13

Slide 13

Slide 13 text

What is Ruby? Object-Oriented Functional Rich Syntax Flexible Monday, February 18, 13

Slide 14

Slide 14 text

What is Ruby bad at? Multitasking Garbage collection Predictability Monday, February 18, 13

Slide 15

Slide 15 text

What is Erlang good at? Multitasking Garbage collection Predictability Monday, February 18, 13

Slide 16

Slide 16 text

Hello World io:format("Hello World\n"). Monday, February 18, 13

Slide 17

Slide 17 text

Hello World io:format("Hello World\n"). module Monday, February 18, 13

Slide 18

Slide 18 text

Hello World io:format("Hello World\n"). module function Monday, February 18, 13

Slide 19

Slide 19 text

Hello World io:format("Hello World\n"). module function string Monday, February 18, 13

Slide 20

Slide 20 text

Hello World io:format("Hello World\n"). module function string newline Monday, February 18, 13

Slide 21

Slide 21 text

Hello World io:format("Hello World\n"). module function string newline end statement Monday, February 18, 13

Slide 22

Slide 22 text

Syntax handle_event(_Event,  _StateName,  StateData)  -­‐>        {stop,badmsg,StateData}. https://github.com/basho/riak_kv/blob/53b343/src/riak_kv_get_fsm.erl#L289-L291 Monday, February 18, 13

Slide 23

Slide 23 text

Syntax handle_event(_Event,  _StateName,  StateData)  -­‐>        {stop,badmsg,StateData}. https://github.com/basho/riak_kv/blob/53b343/src/riak_kv_get_fsm.erl#L289-L291 function Monday, February 18, 13

Slide 24

Slide 24 text

Syntax handle_event(_Event,  _StateName,  StateData)  -­‐>        {stop,badmsg,StateData}. https://github.com/basho/riak_kv/blob/53b343/src/riak_kv_get_fsm.erl#L289-L291 function arguments Monday, February 18, 13

Slide 25

Slide 25 text

Syntax handle_event(_Event,  _StateName,  StateData)  -­‐>        {stop,badmsg,StateData}. https://github.com/basho/riak_kv/blob/53b343/src/riak_kv_get_fsm.erl#L289-L291 function arguments symbol Monday, February 18, 13

Slide 26

Slide 26 text

Syntax handle_event(_Event,  _StateName,  StateData)  -­‐>        {stop,badmsg,StateData}. https://github.com/basho/riak_kv/blob/53b343/src/riak_kv_get_fsm.erl#L289-L291 function arguments symbol tuple Monday, February 18, 13

Slide 27

Slide 27 text

Pattern Matching More useful than equality More powerful than method overloading describe_error(Desc,  [],  [trashed])  -­‐>   {obserror,  Desc}; describe_error(Desc,  [],  Stack)  -­‐>   {obserror,  format("~s.  Stack  ~w",  [Desc,  Stack])}; describe_error(Desc,  Rest,  Stack)  -­‐>   {obserror,  format("~s.    Rest:  ~w  Stack  ~w",  [Desc,  Rest,  Stack])}. Monday, February 18, 13

Slide 28

Slide 28 text

Data Types •List •Tuple •Integer •Float •Atom (symbol) •Bit String Monday, February 18, 13

Slide 29

Slide 29 text

Data Types •Fun: anonymous function, closure •Reference: unique ID •Port identifier: pointer to an I/O port •Pid: pointer to an Erlang process Monday, February 18, 13

Slide 30

Slide 30 text

Synthetic Data Types •Binary: a Bit String with a multiple of 8 bits •String: a List of Integers •Record: a Tuple enforced by the compiler •IO List: a List of Integers, Binaries, and IO Lists; or a Binary •Boolean: the atoms true and false Monday, February 18, 13

Slide 31

Slide 31 text

Environment •Process: similar to a thread •Module: collection of functions •Macro: preprocessor facility (like C) Monday, February 18, 13

Slide 32

Slide 32 text

Process Monday, February 18, 13

Slide 33

Slide 33 text

Process •No shared memory Monday, February 18, 13

Slide 34

Slide 34 text

Process •No shared memory •Asynchronous messaging Monday, February 18, 13

Slide 35

Slide 35 text

Process •No shared memory •Asynchronous messaging •Garbage collected independently Monday, February 18, 13

Slide 36

Slide 36 text

Process •No shared memory •Asynchronous messaging •Garbage collected independently •Fast to create, fast to destroy Monday, February 18, 13

Slide 37

Slide 37 text

Process •No shared memory •Asynchronous messaging •Garbage collected independently •Fast to create, fast to destroy •Low overhead Monday, February 18, 13

Slide 38

Slide 38 text

Open Telecom Platform Illustration by Fred Hébert, from “Learn You Some Erlang for Great Good!” Monday, February 18, 13

Slide 39

Slide 39 text

Behaviors Similar to Ruby mixins Think Enumerable You implement callbacks Behavior supplies the rest Monday, February 18, 13

Slide 40

Slide 40 text

gen_server Generic server Has state Takes synchronous calls, returns responses Takes asynchronous casts Monday, February 18, 13

Slide 41

Slide 41 text

gen_server You Write It Does init turns arguments into state handle_call synchronous, respond and new state handle_cast asynchronous, new state handle_info timeout, new state terminate clean up code_change just upgraded, new state Monday, February 18, 13

Slide 42

Slide 42 text

gen_server State: whatever you want Last argument Last element in response tuple handle_call(all_vnodes_status,  _From,  State)  -­‐>        Reply  =  get_all_vnodes_status(State),        {reply,  Reply,  State}; https://github.com/basho/riak_core/blob/1276e4e926b7007262f7631a27cdb4ecf6cbcaaf/src/riak_core_vnode_manager.erl#L260-L262 Monday, February 18, 13

Slide 43

Slide 43 text

Other Behaviors gen_fsm: finite state machine gen_event: event dispatch and handling supervisor: keep trees of servers, fsms, events, etc. alive Monday, February 18, 13

Slide 44

Slide 44 text

rebar Think Rake Generate scaffolds Run build tasks Monday, February 18, 13

Slide 45

Slide 45 text

Riak Open-source database Built with Erlang, OTP, rebar, webmachine Great for sanity checking implementation Monday, February 18, 13

Slide 46

Slide 46 text

riak_core Distributed system framework powering Riak Nice starting point Monday, February 18, 13

Slide 47

Slide 47 text

What is Erlang bad at? Prolog syntax Opinionated rules No Rails-tier web frameworks Monday, February 18, 13

Slide 48

Slide 48 text

What is Ruby good at? Friendly syntax Few rules, lots of customizability Great frameworks Monday, February 18, 13

Slide 49

Slide 49 text

Should I Use Erlang? Monday, February 18, 13

Slide 50

Slide 50 text

Should I Use Erlang? Yes* Monday, February 18, 13

Slide 51

Slide 51 text

Should I Use Erlang? Yes* *If it makes sense Monday, February 18, 13

Slide 52

Slide 52 text

Use Cases Social network Real-time chat Code sharing site Monday, February 18, 13

Slide 53

Slide 53 text

Learn You Some Erlang http://learnyousomeerlang.com http://nostarch.com/erlang Monday, February 18, 13

Slide 54

Slide 54 text

Links http://bit.ly/erlang-miamirb http://erlang.org/ http://learnyousomeerlang.com/ https://github.com/basho/rebar/ https://github.com/basho/rebar_riak_core/ Monday, February 18, 13