<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">
<b>An Introduction to Erlang</b>
for Python programmers
Paul Barry – Institute of Technology, Carlow in IrelandPyCon Ireland 2011 - October 2011
</div><span class="text_page_counter">Trang 2</span><div class="page_container" data-page="2">
Grab the slides:
class="text_page_counter">Trang 3</span><div class="page_container" data-page="3">
Disclaimer
</div><span class="text_page_counter">Trang 4</span><div class="page_container" data-page="4">
What exactly is Erlang?
</div><span class="text_page_counter">Trang 5</span><div class="page_container" data-page="5">
<i>“Erlang is a declarative,dynamically-typed,</i>
<i>functional, concurrent, distributed andfault-tolerant programming language</i>
<i>with garbage collection and code</i>
<i>hot-swapping built into its runtime.”</i>
Buzz-word city, dude!
</div><span class="text_page_counter">Trang 6</span><div class="page_container" data-page="6">
Scratching an itch inthe early '80s...
</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7">
Why learn Erlang?
</div><span class="text_page_counter">Trang 8</span><div class="page_container" data-page="8">
<i>“Learn a new programminglanguage every year”</i>
</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9">
<i>“Buy at least one new</i>
<i>programming book every year”</i>
</div><span class="text_page_counter">Trang 10</span><div class="page_container" data-page="10">
<i>So, really, why learn Erlang?</i>
</div><span class="text_page_counter">Trang 11</span><div class="page_container" data-page="11">
A better programmer,you will be...
</div><span class="text_page_counter">Trang 13</span><div class="page_container" data-page="13">
Learn what each language is good at,
<i>then pick the right tool for the job</i>
</div><span class="text_page_counter">Trang 14</span><div class="page_container" data-page="14">
Works with a really big hammer!
</div><span class="text_page_counter">Trang 15</span><div class="page_container" data-page="15">
What is Erlang good at?
</div><span class="text_page_counter">Trang 16</span><div class="page_container" data-page="16">
<i>What Erlang Wasn't Designed To Do</i>
</div><span class="text_page_counter">Trang 17</span><div class="page_container" data-page="17">
Build dynamic websites
Run on mobile phones
Allow non-programmers to program
Build GUIs
</div><span class="text_page_counter">Trang 18</span><div class="page_container" data-page="18">
Erlang was designed to build
<b>software systems that never stop</b>
</div><span class="text_page_counter">Trang 19</span><div class="page_container" data-page="19">
<i>What exactly do you</i>
mean by “never”?
</div><span class="text_page_counter">Trang 20</span><div class="page_container" data-page="20">
“Never” at Ericsson means “no morethan 4 minutes downtime per year”...
</div><span class="text_page_counter">Trang 21</span><div class="page_container" data-page="21">
Wow! That's 5 ninesavailability!*
* 99.999% uptime
</div><span class="text_page_counter">Trang 22</span><div class="page_container" data-page="22">
Let's build softwarethat's 100% defect free!
</div><span class="text_page_counter">Trang 24</span><div class="page_container" data-page="24">
There has to be a better way
</div><span class="text_page_counter">Trang 25</span><div class="page_container" data-page="25">
Erlang programmers concentrate on
<b>coding for the correct case</b>
and crashing on failureLet it crash!
</div><span class="text_page_counter">Trang 26</span><div class="page_container" data-page="26">
Robustness is achieved by having
<b>programmers concentrate on processes and the</b>
<i><b>interactions (or messages) between them</b></i>
</div><span class="text_page_counter">Trang 28</span><div class="page_container" data-page="28">
<b>COP: Concurrency-Oriented Programming</b>
</div><span class="text_page_counter">Trang 29</span><div class="page_container" data-page="29">
The problem is...
</div><span class="text_page_counter">Trang 30</span><div class="page_container" data-page="30">
Erlang is a little weirdThe problem is...
</div><span class="text_page_counter">Trang 31</span><div class="page_container" data-page="31">
Consider this code...
-module(hello_server).-export([hello/0]).hello() ->
receive
{FromPID, Who} -> case Who of
robert -> FromPID ! "Hello Robert."; mike -> FromPID ! "Hello Mike.";
joe -> FromPID ! "Hello Joe.";
_ -> FromPID ! "I don't know you." end,
hello() end.
</div><span class="text_page_counter">Trang 32</span><div class="page_container" data-page="32">
Strange punctuation symbols . ; ,
-module(hello_server).-export([hello/0]).hello() ->
receive
{FromPID, Who} -> case Who of
robert -> FromPID ! "Hello Robert."; mike -> FromPID ! "Hello Mike.";
joe -> FromPID ! "Hello Joe.";
_ -> FromPID ! "I don't know you." end,
hello() end.
</div><span class="text_page_counter">Trang 33</span><div class="page_container" data-page="33">
Lots of arrows -> in lots of places
-module(hello_server).-export([hello/0]).hello() ->
receive
{FromPID, Who} -> case Who of
robert -> FromPID ! "Hello Robert."; mike -> FromPID ! "Hello Mike.";
joe -> FromPID ! "Hello Joe.";
_ -> FromPID ! "I don't know you." end,
hello() end.
</div><span class="text_page_counter">Trang 34</span><div class="page_container" data-page="34">
Even stranger symbols - _ !
-module(hello_server).-export([hello/0]).hello() ->
receive
{FromPID, Who} -> case Who of
robert -> FromPID ! "Hello Robert."; mike -> FromPID ! "Hello Mike.";
joe -> FromPID ! "Hello Joe.";
_ -> FromPID ! "I don't know you." end,
hello() end.
</div><span class="text_page_counter">Trang 35</span><div class="page_container" data-page="35">
What's the deal with [] {} and () ?
-module(hello_server).-export([hello/0]).hello() ->
receive
{FromPID, Who} -> case Who of
robert -> FromPID ! "Hello Robert."; mike -> FromPID ! "Hello Mike.";
joe -> FromPID ! "Hello Joe.";
_ -> FromPID ! "I don't know you." end,
hello() end.
</div><span class="text_page_counter">Trang 36</span><div class="page_container" data-page="36">
Functions that call themselves
-module(hello_server).-export([hello/0]).hello() ->
receive
{FromPID, Who} -> case Who of
robert -> FromPID ! "Hello Robert."; mike -> FromPID ! "Hello Mike.";
joe -> FromPID ! "Hello Joe.";
_ -> FromPID ! "I don't know you." end,
hello() end.
</div><span class="text_page_counter">Trang 37</span><div class="page_container" data-page="37">
<i>This is weird...</i>
and there's even more
</div><span class="text_page_counter">Trang 38</span><div class="page_container" data-page="38">
Erlang Culture Shock
</div><span class="text_page_counter">Trang 39</span><div class="page_container" data-page="39">
Shock #1
<b>Erlang's syntax is based on Prolog</b>
-module(howdy).-export([hi/1]).hi(Name) ->
io:format('Hi there, ~p!~n', [Name]).
{person, First, Last} = {person, 'Paul', 'Barry'}.howdy:hi(Last).
</div><span class="text_page_counter">Trang 40</span><div class="page_container" data-page="40">
Shock #2
<b>Erlang is not object-oriented</b>
</div><span class="text_page_counter">Trang 41</span><div class="page_container" data-page="41">
Shock #3
<b>Everything in Erlang is immutable</b>
Not just tuples and not just strings...
<b>EVERYTHING</b>
</div><span class="text_page_counter">Trang 42</span><div class="page_container" data-page="42">
<b>So... this doesn't work!</b>
X = 10.X = X + 1.
<i>** exception error: no match of right hand side value 11</i>
Y = X + 1.
</div><span class="text_page_counter">Trang 43</span><div class="page_container" data-page="43">
Erlang's troublesome = operator
<b>The “=” operator does not mean “assign”</b>
It means “match” or “bind to”
</div><span class="text_page_counter">Trang 44</span><div class="page_container" data-page="44">
<b>Destructive updates are forbidden</b>
and (as an added twist)
Variables must start with an Uppercase letter:
No side effects here!
</div><span class="text_page_counter">Trang 45</span><div class="page_container" data-page="45">
Shock #4
There are no built-in looping constructs
No for and no while
</div><span class="text_page_counter">Trang 46</span><div class="page_container" data-page="46">
So... how do you loop?
</div><span class="text_page_counter">Trang 47</span><div class="page_container" data-page="47">
List ComprehensionsErlang:
Alist = [1, 2, 3, 4, 5].
Doubled = [X*2 || X <- Alist].
alist = [1, 2, 3, 4, 5]
doubled = [x*2 for x in alist]
<i><b>Note: alist and doubled are mutable; Alist and Doubled are not</b></i>
</div><span class="text_page_counter">Trang 48</span><div class="page_container" data-page="48">
Shock #5
<b>“Regular” looping is via recursion</b>
</div><span class="text_page_counter">Trang 49</span><div class="page_container" data-page="49">
{FromPID, Who} -> case Who of
robert -> FromPID ! "Hello Robert."; mike -> FromPID ! "Hello Mike.";
joe -> FromPID ! "Hello Joe.";
_ -> FromPID ! "I don't know you." end,
hello() end.
</div><span class="text_page_counter">Trang 50</span><div class="page_container" data-page="50">
Shock #6
Strings are stored as a
<b>list of integers</b>
</div><span class="text_page_counter">Trang 51</span><div class="page_container" data-page="51">
Sweet mother of all
<i>things Erlang! Whose brightidea was that?</i>
</div><span class="text_page_counter">Trang 53</span><div class="page_container" data-page="53">
There are if and case expressionsbut... they're kinda weird
</div><span class="text_page_counter">Trang 54</span><div class="page_container" data-page="54">
54
</div><span class="text_page_counter">Trang 55</span><div class="page_container" data-page="55">
With enough pig-headed persistence,weirdness can be overcome
</div><span class="text_page_counter">Trang 56</span><div class="page_container" data-page="56">
So... who do I call tohelp me learn Erlang?
</div><span class="text_page_counter">Trang 57</span><div class="page_container" data-page="57">
57
</div><span class="text_page_counter">Trang 59</span><div class="page_container" data-page="59">
<i><b>Erlang is a language you study</b></i>
as getting up-to-speed with Erlang takes time
</div><span class="text_page_counter">Trang 61</span><div class="page_container" data-page="61">
Some Unique “Characteristics”
</div><span class="text_page_counter">Trang 62</span><div class="page_container" data-page="62">
Banned by Ericsson in 1998 for
<i>“not being open enough”</i>
</div><span class="text_page_counter">Trang 63</span><div class="page_container" data-page="63">
class="text_page_counter">Trang 64</span><div class="page_container" data-page="64">
64
</div><span class="text_page_counter">Trang 65</span><div class="page_container" data-page="65">
This is all great, but...
<i>how exactly do I use</i>
Erlang to build afault-tolerant system?
</div><span class="text_page_counter">Trang 66</span><div class="page_container" data-page="66">
Learn Three Things
Create an Erlang process with spawn()
Send a message to a process with !
Process a message with receive
</div><span class="text_page_counter">Trang 67</span><div class="page_container" data-page="67">
Remember this code?
<small>%% Saved in 'hello_server.erl'.</small>
<small>-module(hello_server). -export([hello/0]).</small>
<small>hello() -> receive </small>
<small> {FromPID, Who} -> case Who of</small>
<small> robert -> FromPID ! "Hello Robert."; mike -> FromPID ! "Hello Mike."; joe -> FromPID ! "Hello Joe."; _ -> FromPID ! "I don't know you." end,</small>
<small> hello() end.</small>
</div><span class="text_page_counter">Trang 68</span><div class="page_container" data-page="68">
Create the hello() process
Pid = spawn(fun hello_server:hello/0).
<0.38.0>
</div><span class="text_page_counter">Trang 69</span><div class="page_container" data-page="69">
Send a message to a process with !
Pid ! robert.
robert
</div><span class="text_page_counter">Trang 70</span><div class="page_container" data-page="70">
A little twist
Pid ! {self(), robert}.
{<0.31.0>,robert}
</div><span class="text_page_counter">Trang 71</span><div class="page_container" data-page="71">
Messages sent to a process with !are received by receive
</div><span class="text_page_counter">Trang 72</span><div class="page_container" data-page="72">
Can you spot the pattern match?
<small>%% Saved in 'hello_server.erl'.</small>
<small>-module(hello_server). -export([hello/0]).</small>
<small>hello() -> receive </small>
<small> {FromPID, Who} -> case Who of</small>
<small> robert -> FromPID ! "Hello Robert."; mike -> FromPID ! "Hello Mike."; joe -> FromPID ! "Hello Joe."; _ -> FromPID ! "I don't know you." end,</small>
<small> hello() end.</small>
</div><span class="text_page_counter">Trang 73</span><div class="page_container" data-page="73">
The {FromPID, Who} tuple matches
<small>%% Saved in 'hello_server.erl'.</small>
<small>-module(hello_server). -export([hello/0]).</small>
<small>hello() -> receive </small>
<b><small> {FromPID, Who} -></small></b>
<small> case Who of</small>
<small> robert -> FromPID ! "Hello Robert."; mike -> FromPID ! "Hello Mike."; joe -> FromPID ! "Hello Joe."; _ -> FromPID ! "I don't know you." end,</small>
<small> hello() end.</small>
</div><span class="text_page_counter">Trang 74</span><div class="page_container" data-page="74">
Send a message back...
<small>%% Saved in 'hello_server.erl'.</small>
<small>-module(hello_server). -export([hello/0]).</small>
<small>hello() -> receive </small>
<small> {FromPID, Who} -> case Who of</small>
<b><small> robert -> FromPID ! "Hello Robert.";</small></b>
<small> mike -> FromPID ! "Hello Mike."; joe -> FromPID ! "Hello Joe."; _ -> FromPID ! "I don't know you." end,</small>
<small> hello() end.</small>
</div><span class="text_page_counter">Trang 75</span><div class="page_container" data-page="75">
spawn, send (!) then receive
Pid = spawn(fun hello_server:hello/0),Pid ! {self(), robert},
Response ->
"Hello Robert."
</div><span class="text_page_counter">Trang 76</span><div class="page_container" data-page="76">
Things get really interestingwhen the processes are
<b>linked together with spawn_link()</b>
and the trap_exit signal
</div><span class="text_page_counter">Trang 78</span><div class="page_container" data-page="78">
Distributed Erlang
I need to spawnhello/0
on glasnost...
hello/0 isregistered
as 'hr'
</div><span class="text_page_counter">Trang 80</span><div class="page_container" data-page="80">
Over time, you'll end up repeating
a lot of your process management code...
</div><span class="text_page_counter">Trang 81</span><div class="page_container" data-page="81">
81
</div><span class="text_page_counter">Trang 82</span><div class="page_container" data-page="82">
OTP is the jewel in Erlang's crown
</div><span class="text_page_counter">Trang 83</span><div class="page_container" data-page="83">
So... if Erlang's socool, how come no one
is using it?
</div><span class="text_page_counter">Trang 84</span><div class="page_container" data-page="84">
The TIOBE Index
</div><span class="text_page_counter">Trang 85</span><div class="page_container" data-page="85">
85
</div><span class="text_page_counter">Trang 86</span><div class="page_container" data-page="86">
Erlang in Telecoms
</div><span class="text_page_counter">Trang 87</span><div class="page_container" data-page="87">
Erlang in Data
</div><span class="text_page_counter">Trang 88</span><div class="page_container" data-page="88">
Erlang on the Web
</div><span class="text_page_counter">Trang 89</span><div class="page_container" data-page="89">
Erlang in Gaming
<i><b>“…Mochi Media is the world's </b></i>
<i>largest browser-based games </i>
<i>network, with more than 140 million monthly active users and 15,000 games on nearly 40,000 publisher websites…”</i>
Check out Bob Ippolito’s Erlang-Factory talks!
</div><span class="text_page_counter">Trang 90</span><div class="page_container" data-page="90">
More Erlang in Gaming
<i><b>Thanks to Malcolm Dowse (of DemonWare, Dublin) for permission to use these</b></i>
<i> images, which were first delivered as part of Malcolm's talk to Erlang Factory London 2011.</i>
</div><span class="text_page_counter">Trang 91</span><div class="page_container" data-page="91">
So... is Erlangworth learning?<sup>So... is Erlang</sup>worth learning?
</div><span class="text_page_counter">Trang 92</span><div class="page_container" data-page="92">
<b>Yes</b>
</div><span class="text_page_counter">Trang 93</span><div class="page_container" data-page="93">
Lots more to discover...
Fun with fun
Bit-strings and bit syntax
ETS and DETS
“Live” code updating
The Mnesia Distributed Database
The Standard Library
CEAN
</div><span class="text_page_counter">Trang 94</span><div class="page_container" data-page="94">
Friendly Community
erlang-questions mailing list
</div><span class="text_page_counter">Trang 95</span><div class="page_container" data-page="95">
Questions?
</div>