An Introduction to Erlang      for Python programmers   Paul Barry – Institute of Technology, Carlow in Ireland           ...
Grab the slides:http://paulbarry.itcarlow.ie/ErlangWebcast.pdf                                                 2
Disclaimer             3
What exactly is Erlang?                          4
“Erlang is a declarative,dynamically-typed,  functional, concurrent, distributed and   fault-tolerant programming language...
Scratching an itch in  the early 80s...                        6
Why learn Erlang?                    7
“Learn a new programming   language every year”                           8
“Buy at least one newprogramming book every year”                               9
So, really, why learn Erlang?                                10
A better programmer,    you will be...                       11
Learn how others solve problems with adifferent language, then apply these new   techniques to your current language      ...
Learn what each language is good at,  then pick the right tool for the job                                         13
Works with a really big hammer!                                  14
What is Erlang good at?                          15
What Erlang Wasnt Designed To Do                                    16
Erlang wasnt designed to...    Run in a browser    Process text efficiently    Be easy to teach    Be easy to learn  ...
Erlang was designed to buildsoftware systems that never stop                                   18
What exactly do you mean by “never”?                      19
“Never” at Ericsson means “no more                          than 4 minutes downtime per year”...http://www.infoq.com/prese...
Wow! Thats 5 nines   availability!*                                21             * 99.999% uptime
Lets build softwarethats 100% defect free!                           22
Lets throw lots and lotsof code at the problem and      itll go away, eh?                              23
There has to be a better way                               24
Let it crash!Erlang programmers concentrate on    coding for the correct case      and crashing on failure                ...
Robustness is achieved by havingprogrammers concentrate on processes and the   interactions (or messages) between them    ...
Early error detection/recovery and the use of concurrent programming techniques  lets you build fault tolerant systems    ...
COP: Concurrency-Oriented Programming                                        28
The problem is...                    29
The problem is...Erlang is a little weird                           30
Consider this code...-module(hello_server).-export([hello/0]).hello() ->    receive           {FromPID, Who} ->           ...
Strange punctuation symbols . ; ,-module(hello_server).-export([hello/0]).hello() ->    receive           {FromPID, Who} -...
Lots of arrows -> in lots of places-module(hello_server).-export([hello/0]).hello() ->    receive           {FromPID, Who}...
Even stranger symbols - _ !-module(hello_server).-export([hello/0]).hello() ->    receive           {FromPID, Who} ->     ...
Whats the deal with [] {} and () ?-module(hello_server).-export([hello/0]).hello() ->    receive           {FromPID, Who} ...
Functions that call themselves-module(hello_server).-export([hello/0]).hello() ->    receive           {FromPID, Who} ->  ...
This is weird...and theres even more                        37
Erlang Culture Shock                       38
Shock #1        Erlangs syntax is based on Prolog-module(howdy).-export([hi/1]).hi(Name) ->    io:format(Hi there, ~p!~n, ...
Shock #2Erlang is not object-oriented                                40
Shock #3Everything in Erlang is immutable   Not just tuples and not just strings...           EVERYTHING                  ...
So... this doesnt work!                    X = 10.                  X = X + 1.** exception error: no match of right hand s...
Erlangs troublesome = operator The “=” operator does not mean “assign”      It means “match” or “bind to”                 ...
No side effects here!     Destructive updates are forbidden              and (as an added twist)Variables must start with ...
Shock #4There are no built-in looping constructs        No for and no while                                           45
So... how do you loop?                         46
List ComprehensionsErlang:               Alist = [1, 2, 3, 4, 5].               Doubled = [X*2 || X <- Alist].Python:     ...
Shock #5“Regular” looping is via recursion                                     48
Spot the loop?-module(hello_server).-export([hello/0]).hello() ->    receive           {FromPID, Who} ->              case...
Shock #6Strings are stored as a    list of integers                          50
Sweet mother of allthings Erlang! Whose bright       idea was that?                              51
Shock #7      There is noif... then... else...      statement                        52
There are if and case expressions     but... theyre kinda weird                                    53
54
With enough pig-headed persistence,    weirdness can be overcome                                      55
So... who do I call tohelp me learn Erlang?                          56
57
58
Erlang is a language you study  as getting up-to-speed with       Erlang takes time                                 59
Web Resources    http://www.erlang.org    Try Erlang:            http://www.tryerlang.org/    “Learn Some Erlang For G...
Some Unique “Characteristics”                                61
Banned by Ericsson in 1998 for   “not being open enough”                                 62
63http://video.google.com/videoplay?docid=-5830318882717959520
64
This is all great, but... how exactly do I use   Erlang to build afault-tolerant system?                            65
Learn Three Things    Create an Erlang process with spawn()    Send a message to a process with !    Process a message ...
Remember this code?%% Saved in hello_server.erl.-module(hello_server).-export([hello/0]).hello() ->   receive       {FromP...
Create the hello() processPid = spawn(fun hello_server:hello/0).<0.38.0>                                    68
Send a message to a process with !          Pid ! robert.          robert                                 69
A little twistPid ! {self(), robert}.{<0.31.0>,robert}                          70
Messages sent to a process with !   are received by receive                                    71
Can you spot the pattern match?%% Saved in hello_server.erl.-module(hello_server).-export([hello/0]).hello() ->   receive ...
The {FromPID,                       Who}         tuple matches%% Saved in hello_server.erl.-module(hello_server).-export([...
Send a message back...%% Saved in hello_server.erl.-module(hello_server).-export([hello/0]).hello() ->    receive       {F...
spawn, send (!) then receivePid = spawn(fun hello_server:hello/0),Pid ! {self(), robert},receive      Response ->         ...
Things get really interesting       when the processes arelinked together with spawn_link()     and the trap_exit signal  ...
Processes that are linked together canreact appropriately to EXIT messages from  each other... and what happens next is   ...
Distributed Erlang                    I need to spawn                         hello/0                               hello/...
Pid = spawn(fun hello_server:hello/0),                           becomesPid = spawn(‘hr@glasnost.itcarlow.ie’, hello_serve...
Over time, youll end up repeatinga lot of your process management code...                                           80
81
OTP is the jewel in Erlangs crown                                     82
So... if Erlangs socool, how come no one       is using it?                         83
The TIOBE Index                  84
85
Erlang in Telecoms                     86
Erlang in Data                 87
Erlang on the Web                    88
Erlang in Gaming                                     “…Mochi Media is the worlds                                     large...
More Erlang in Gaming                                                                                              90Thank...
So... is Erlangworth learning?                  91
Yes      92
Lots more to discover...                      Fun with fun          Bit-strings and bit syntax                      ETS...
Friendly Communityerlang-questions mailing list                                94
Questions?             95
Upcoming SlideShare
Loading in …5
×

Introduction to Erlang for Python Programmers

1,195 views
1,070 views

Published on

What is Erlang? Why it is important? Why should Python programmers learn Erlang? How is Erlang different? How is Erlang the same? These and other questions will be answered during this talk, as well as this one: Should Erlang be the new programming language you learn this year?

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,195
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
22
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Introduction to Erlang for Python Programmers

  1. 1. An Introduction to Erlang for Python programmers Paul Barry – Institute of Technology, Carlow in Ireland PyCon Ireland 2011 - October 2011
  2. 2. Grab the slides:http://paulbarry.itcarlow.ie/ErlangWebcast.pdf 2
  3. 3. Disclaimer 3
  4. 4. What exactly is Erlang? 4
  5. 5. “Erlang is a declarative,dynamically-typed, functional, concurrent, distributed and fault-tolerant programming language with garbage collection and code hot-swapping built into its runtime.” Buzz-word city, dude! 5
  6. 6. Scratching an itch in the early 80s... 6
  7. 7. Why learn Erlang? 7
  8. 8. “Learn a new programming language every year” 8
  9. 9. “Buy at least one newprogramming book every year” 9
  10. 10. So, really, why learn Erlang? 10
  11. 11. A better programmer, you will be... 11
  12. 12. Learn how others solve problems with adifferent language, then apply these new techniques to your current language 12
  13. 13. Learn what each language is good at, then pick the right tool for the job 13
  14. 14. Works with a really big hammer! 14
  15. 15. What is Erlang good at? 15
  16. 16. What Erlang Wasnt Designed To Do 16
  17. 17. Erlang wasnt designed to... Run in a browser Process text efficiently Be easy to teach Be easy to learn Build dynamic websites Run on mobile phones Allow non-programmers to program Build GUIs 17
  18. 18. Erlang was designed to buildsoftware systems that never stop 18
  19. 19. What exactly do you mean by “never”? 19
  20. 20. “Never” at Ericsson means “no more than 4 minutes downtime per year”...http://www.infoq.com/presentations/Systems-that-Never-Stop-Joe-Armstrong 20
  21. 21. Wow! Thats 5 nines availability!* 21 * 99.999% uptime
  22. 22. Lets build softwarethats 100% defect free! 22
  23. 23. Lets throw lots and lotsof code at the problem and itll go away, eh? 23
  24. 24. There has to be a better way 24
  25. 25. Let it crash!Erlang programmers concentrate on coding for the correct case and crashing on failure 25
  26. 26. Robustness is achieved by havingprogrammers concentrate on processes and the interactions (or messages) between them 26
  27. 27. Early error detection/recovery and the use of concurrent programming techniques lets you build fault tolerant systems 27
  28. 28. COP: Concurrency-Oriented Programming 28
  29. 29. The problem is... 29
  30. 30. The problem is...Erlang is a little weird 30
  31. 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 dont know you." end, hello() end. 31
  32. 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 dont know you." end, hello() end. 32
  33. 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 dont know you." end, hello() end. 33
  34. 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 dont know you." end, hello() end. 34
  35. 35. Whats 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 dont know you." end, hello() end. 35
  36. 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 dont know you." end, hello() end. 36
  37. 37. This is weird...and theres even more 37
  38. 38. Erlang Culture Shock 38
  39. 39. Shock #1 Erlangs syntax is based on Prolog-module(howdy).-export([hi/1]).hi(Name) -> io:format(Hi there, ~p!~n, [Name]).{person, First, Last} = {person, Paul, Barry}.howdy:hi(Last). 39
  40. 40. Shock #2Erlang is not object-oriented 40
  41. 41. Shock #3Everything in Erlang is immutable Not just tuples and not just strings... EVERYTHING 41
  42. 42. So... this doesnt work! X = 10. X = X + 1.** exception error: no match of right hand side value 11 Y = X + 1. 42
  43. 43. Erlangs troublesome = operator The “=” operator does not mean “assign” It means “match” or “bind to” 43
  44. 44. No side effects here! Destructive updates are forbidden and (as an added twist)Variables must start with an Uppercase letter: X Pid Func 44
  45. 45. Shock #4There are no built-in looping constructs No for and no while 45
  46. 46. So... how do you loop? 46
  47. 47. List ComprehensionsErlang: Alist = [1, 2, 3, 4, 5]. Doubled = [X*2 || X <- Alist].Python: alist = [1, 2, 3, 4, 5] doubled = [x*2 for x in alist]Note: alist and doubled are mutable; Alist and Doubled are not 47
  48. 48. Shock #5“Regular” looping is via recursion 48
  49. 49. Spot the loop?-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 dont know you." end, hello() 49 end.
  50. 50. Shock #6Strings are stored as a list of integers 50
  51. 51. Sweet mother of allthings Erlang! Whose bright idea was that? 51
  52. 52. Shock #7 There is noif... then... else... statement 52
  53. 53. There are if and case expressions but... theyre kinda weird 53
  54. 54. 54
  55. 55. With enough pig-headed persistence, weirdness can be overcome 55
  56. 56. So... who do I call tohelp me learn Erlang? 56
  57. 57. 57
  58. 58. 58
  59. 59. Erlang is a language you study as getting up-to-speed with Erlang takes time 59
  60. 60. Web Resources http://www.erlang.org Try Erlang:  http://www.tryerlang.org/ “Learn Some Erlang For Great Good!”:  http://learnyousomeerlang.com/ The Erlang Factory:  http://erlang-factory.com/ InfoQ:  http://www.infoq.com 60
  61. 61. Some Unique “Characteristics” 61
  62. 62. Banned by Ericsson in 1998 for “not being open enough” 62
  63. 63. 63http://video.google.com/videoplay?docid=-5830318882717959520
  64. 64. 64
  65. 65. This is all great, but... how exactly do I use Erlang to build afault-tolerant system? 65
  66. 66. Learn Three Things Create an Erlang process with spawn() Send a message to a process with ! Process a message with receive 66
  67. 67. Remember this code?%% Saved in hello_server.erl.-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 dont know you." end, hello() end. 67
  68. 68. Create the hello() processPid = spawn(fun hello_server:hello/0).<0.38.0> 68
  69. 69. Send a message to a process with ! Pid ! robert. robert 69
  70. 70. A little twistPid ! {self(), robert}.{<0.31.0>,robert} 70
  71. 71. Messages sent to a process with ! are received by receive 71
  72. 72. Can you spot the pattern match?%% Saved in hello_server.erl.-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 dont know you." end, hello() end. 72
  73. 73. The {FromPID, Who} tuple matches%% Saved in hello_server.erl.-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 dont know you." end, hello() end. 73
  74. 74. Send a message back...%% Saved in hello_server.erl.-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 dont know you." end, hello() end. 74
  75. 75. spawn, send (!) then receivePid = spawn(fun hello_server:hello/0),Pid ! {self(), robert},receive Response -> Responseend."Hello Robert." 75
  76. 76. Things get really interesting when the processes arelinked together with spawn_link() and the trap_exit signal 76
  77. 77. Processes that are linked together canreact appropriately to EXIT messages from each other... and what happens next is controlled by the programmer 77
  78. 78. Distributed Erlang I need to spawn hello/0 hello/0 is on glasnost... registeredpbmac.itcarlow.ie as hr 78 glasnost.itcarlow.ie
  79. 79. Pid = spawn(fun hello_server:hello/0), becomesPid = spawn(‘hr@glasnost.itcarlow.ie’, hello_server, hello, []), 79
  80. 80. Over time, youll end up repeatinga lot of your process management code... 80
  81. 81. 81
  82. 82. OTP is the jewel in Erlangs crown 82
  83. 83. So... if Erlangs socool, how come no one is using it? 83
  84. 84. The TIOBE Index 84
  85. 85. 85
  86. 86. Erlang in Telecoms 86
  87. 87. Erlang in Data 87
  88. 88. Erlang on the Web 88
  89. 89. Erlang in Gaming “…Mochi Media is the worlds largest browser-based games network, with more than 140 million monthly active users and 15,000 games on nearly 40,000 publisher websites…” 89Check out Bob Ippolito’s Erlang-Factory talks!
  90. 90. More Erlang in Gaming 90Thanks to Malcolm Dowse (of DemonWare, Dublin) for permission to use theseimages, which were first delivered as part of Malcolms talk to Erlang Factory London 2011.
  91. 91. So... is Erlangworth learning? 91
  92. 92. Yes 92
  93. 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 93
  94. 94. Friendly Communityerlang-questions mailing list 94
  95. 95. Questions? 95

×