Share
Embed
Transcript
54 slides
-
A Chat About Erlang Bryce Kerley Miami Ruby Brigade February
18, 2013 Monday, February 18, 13 -
Administrativia Please check on on the Meetup iPhone or Android
app. Monday, February 18, 13 -
Disclaimer The thoughts, views, and opinions in this presentation are
mine, and not my employer’s. Monday, February 18, 13 -
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 -
5 3> 5 = Y. 5 4> Y = 6.
** exception error: no match 5> Z = fun(N) -> N * 2 end. #Fun<erl_eval.6.80247286> 6> Z. #Fun<erl_eval.6.80247286> 7> Z(5). 10 Erlang is Functional Monday, February 18, 13 -
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 -
Erlang is Reliable Illustration by Fred Hébert, from “Learn You
Some Erlang for Great Good!” Monday, February 18, 13 -
Hello World io:format("Hello World\n"). module function string newline end statement
Monday, February 18, 13 -
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 -
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 -
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 -
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 -
Data Types •List •Tuple •Integer •Float •Atom (symbol) •Bit String
Monday, February 18, 13 -
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 -
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 -
Environment •Process: similar to a thread •Module: collection of functions
•Macro: preprocessor facility (like C) Monday, February 18, 13 -
Process •No shared memory •Asynchronous messaging •Garbage collected independently •Fast
to create, fast to destroy Monday, February 18, 13 -
Process •No shared memory •Asynchronous messaging •Garbage collected independently •Fast
to create, fast to destroy •Low overhead Monday, February 18, 13 -
Open Telecom Platform Illustration by Fred Hébert, from “Learn You
Some Erlang for Great Good!” Monday, February 18, 13 -
Behaviors Similar to Ruby mixins Think Enumerable You implement callbacks
Behavior supplies the rest Monday, February 18, 13 -
gen_server Generic server Has state Takes synchronous calls, returns responses
Takes asynchronous casts Monday, February 18, 13 -
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 -
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 -
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 -
Riak Open-source database Built with Erlang, OTP, rebar, webmachine Great
for sanity checking implementation Monday, February 18, 13 -
What is Erlang bad at? Prolog syntax Opinionated rules No
Rails-tier web frameworks Monday, February 18, 13 -
What is Ruby good at? Friendly syntax Few rules, lots
of customizability Great frameworks Monday, February 18, 13 -
Should I Use Erlang? Yes* *If it makes sense Monday,
February 18, 13