Erlang Introduction                             Serhiy Oplakanets, Lviv 2012Friday, February 10, 12
Classification                 • high-level                 • general-purpose                 • garbage-collected          ...
$ erl            Erlang R14B04 (erts-5.8.5)            [source] [64-bit] [smp:4:4] [rq:            4] [async-threads:0] [h...
SyntaxFriday, February 10, 12
Data TypesFriday, February 10, 12
1> 2 + 2.            4            2> 3 * 5.            15            3> 7 / 6.            1.1666666666666667Friday, Februa...
1> [].            []            2> [1, 2, 3].            [1,2,3]            3> [101,104,63].            "eh?"Friday, Febru...
1> "a string".            "a string"            2>            [97,32,115,116,114,105,110,103].            "a string"Friday...
1> $H.            72            2> $e.            101            3> $l.            108            4> $o.            111   ...
1> true.            true            2> false.            false            3> true == false.            false            4>...
1> atom.            atom            2> anotherAtom.            anotherAtom            3> One more atom?.            One mo...
4> is_atom(One more atom?).            trueFriday, February 10, 12
1> is_atom(true).            true            2> is_atom(false).            trueFriday, February 10, 12
1> {1,2,3}.            {1,2,3}            2> {16, "hello"}.            {16,"hello"}            3> {3.14, "Pi", {"foo", bar...
Invariable VariablesFriday, February 10, 12
1> Variable = "value".            "value"            2> AnotherVariable = 128.            128Friday, February 10, 12
1> R = 5, Pi = 3.14.            3.14            2> Pi.            3.14            3> Pi * R * R.            78.5Friday, Fe...
1> X = 1.            1            3> X = 2.            ** exception error: no match of right            hand side value 2 ...
Pattern MatchingFriday, February 10, 12
1> X = 1, Y = 1.            1            2> X = Y.            ???Friday, February 10, 12
1> UnboundVariable.            * 1: variable UnboundVariable            is unboundFriday, February 10, 12
1> X = 1.            1            2> X.            1            3> Y.            * 1: variable Y is unbound            4> ...
1> User = {"John", "Doe", 35}.            ...            2> {Name, Surname, Age} = User.            ...            3> Name...
1> User = {"John", "Doe", 35}.            ...            2> {Name, Surname} = User.            ** exception error: no matc...
2> User = {"John", "Doe", 35}.            ...            3> {Name, "Doe", 35} = User.            ...            4> Name.  ...
1> User = {"John", "Doe", 35}.            ...            2> {Name, _, Age} = User.            ...            3> Age.      ...
1> _ = 1.            1            2> _.            * 1: variable _ is unbound            5> _ = 3.            3Friday, Feb...
1> [Head | Tail] = [1,2,3].            [1,2,3]            2> Head.            1            3> Tail.            [2,3]Friday...
1> [1 | [2 | [3]]].            [1,2,3]Friday, February 10, 12
Modules and FunctionsFriday, February 10, 12
-module(test).            -export([main/0]).            main() -> "Hello".Friday, February 10, 12
1> test:main().            "Hello"Friday, February 10, 12
-module(test).            -export([factorial/1]).            factorial(0) -> 1;            factorial(N) -> N * factorial(N...
$ erlc test.erl            $ erl            ...            1> test:factorial(3).            6            2> test:factorial...
-module(logger).            -export([log/1, log/2]).            log(Msg) -> {"info", Msg}.            log(Level, Msg) -> {...
log(Msg) -> log("info", Msg).            log(Level, Msg) -> {Level, Msg}.Friday, February 10, 12
Function GuardsFriday, February 10, 12
-module(test).            -export([fib/1]).            fib(0) -> 0;            fib(1) -> 1;            fib(N) when N > 0 -...
test:fib(0).            0            3> test:fib(1).            1            5> test:fib(8).            21            6> t...
% Refactored factorial function:            factorial(0) -> 1;            factorial(N)                     when is_integer...
7> factorial:factorial(3).            6            8> factorial:factorial(0).            1            9> factorial:factori...
Concurrency                 • any function can become a process                 • process is a function executing in      ...
Processes/Actors                 • processes are extremely lightweight and                          fast                 •...
spawn(Module, Function, Arguments) -> PidFriday, February 10, 12
Any function can                          become a processFriday, February 10, 12
8> io:format("Hello~n").            Hello            ...            2> spawn(io, format, ["Hello~n"]).            Hello   ...
-module(actor).            -export([loop/0]).            loop() ->                          receive                       ...
2> Pid = spawn(actor, loop, []).            <0.40.0>            3> is_process_alive(Pid).            true            4> Pi...
loop() ->                          receive                                 die ->                                     io:f...
2> Pid = spawn(actor, loop, []).            ...            3> is_process_alive(Pid).            true            4> Pid ! "...
loop() ->                          receive                                 die ->                                     io:f...
2> Pid = spawn(actor, loop, []).            ...            3> Pid ! "Hello".            Got: "Hello"            4> is_proc...
Processes are executed                in parallel and share no                           dataFriday, February 10, 12
-module(counter).            -export([loop/1]).            loop(N) ->                    receive                          ...
2> Counter = spawn(counter, loop, [0]).            ...            3> Counter ! increment.            ...            4> Cou...
2> C1 = spawn(counter, loop, [0]).            <0.40.0>            3> C2 = spawn(counter, loop, [10]).            <0.42.0> ...
Distributed ErlangFriday, February 10, 12
spawn(Node, Module, Function, Args) -> PidFriday, February 10, 12
1> spawn(DistributedNode, test, factorial, [10]).Friday, February 10, 12
s erl -sname slave -setcookie 123!@#qwe            ...            (slave@Serhiys-MacBook-Pro)1> node().            slave@S...
$ erl -sname master -setcookie 123!@#qwe            ...            (master@Serhiys-MacBook-Pro)1>            net_adm:ping(...
Fault ToleranceFriday, February 10, 12
-module(actor).            -export([loop/0]).            loop() ->                      receive                           ...
2> Pid = spawn(actor, loop, []).            ...            3> Pid ! 4.            sqrt(4) = 2.0            ...            ...
Supervisors                 • supervisors monitor processes and take                          actions on exit signals     ...
start() -> spawn(actor, restarter, []).            restarter() ->                     process_flag(trap_exit, true),      ...
2> actor:start().            3> myActor ! 4.            sqrt(4) = 2.0            4> myActor ! "foo".            Process cr...
Other great features                 • Hot code loading                 • OTP Framework                 •Friday, February ...
Simplicity                 • You just learned                  • ~70% of Erlang syntax                  • most major abstr...
Complexity                 • Erlang/OTP is a complex framework                 • Learning it takes time and practiceFriday...
Bad Stuff                 • syntax                 • Strings                 • code organization                 • librari...
Practical Application                 • Notable uses:                  • Facebook (Chat)                  • CouchDB       ...
How to learn Erlang?Friday, February 10, 12
http://learnyousomeerlang.com/Friday, February 10, 12
Erlang getting started guide:             http://www.erlang.org/doc/getting_started/intro.htmlFriday, February 10, 12
Programming Erlang: Software for a Concurrent World      http://pragprog.com/book/jaerlang/programming-erlangFriday, Febru...
$ erlFriday, February 10, 12
[$Q, $u, $e, $s, $t, $i, $o, $n, $s, $?].Friday, February 10, 12
Thank youFriday, February 10, 12
Upcoming SlideShare
Loading in...5
×

Erlang Introduction

1,196

Published on

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,196
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Erlang Introduction

  1. 1. Erlang Introduction Serhiy Oplakanets, Lviv 2012Friday, February 10, 12
  2. 2. Classification • high-level • general-purpose • garbage-collected • dynamically typed • functional • concurrentFriday, February 10, 12
  3. 3. $ erl Erlang R14B04 (erts-5.8.5) [source] [64-bit] [smp:4:4] [rq: 4] [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.8.5 (abort with ^G) 1>Friday, February 10, 12
  4. 4. SyntaxFriday, February 10, 12
  5. 5. Data TypesFriday, February 10, 12
  6. 6. 1> 2 + 2. 4 2> 3 * 5. 15 3> 7 / 6. 1.1666666666666667Friday, February 10, 12
  7. 7. 1> []. [] 2> [1, 2, 3]. [1,2,3] 3> [101,104,63]. "eh?"Friday, February 10, 12
  8. 8. 1> "a string". "a string" 2> [97,32,115,116,114,105,110,103]. "a string"Friday, February 10, 12
  9. 9. 1> $H. 72 2> $e. 101 3> $l. 108 4> $o. 111 5> [$H, $e, $l, $l, $o]. "Hello"Friday, February 10, 12
  10. 10. 1> true. true 2> false. false 3> true == false. false 4> false == false. true 5> true /= false. trueFriday, February 10, 12
  11. 11. 1> atom. atom 2> anotherAtom. anotherAtom 3> One more atom?. One more atom?Friday, February 10, 12
  12. 12. 4> is_atom(One more atom?). trueFriday, February 10, 12
  13. 13. 1> is_atom(true). true 2> is_atom(false). trueFriday, February 10, 12
  14. 14. 1> {1,2,3}. {1,2,3} 2> {16, "hello"}. {16,"hello"} 3> {3.14, "Pi", {"foo", bar}}. {3.14,"Pi",{"foo",bar}}Friday, February 10, 12
  15. 15. Invariable VariablesFriday, February 10, 12
  16. 16. 1> Variable = "value". "value" 2> AnotherVariable = 128. 128Friday, February 10, 12
  17. 17. 1> R = 5, Pi = 3.14. 3.14 2> Pi. 3.14 3> Pi * R * R. 78.5Friday, February 10, 12
  18. 18. 1> X = 1. 1 3> X = 2. ** exception error: no match of right hand side value 2 4> X = 1. 1 ?!?!Friday, February 10, 12
  19. 19. Pattern MatchingFriday, February 10, 12
  20. 20. 1> X = 1, Y = 1. 1 2> X = Y. ???Friday, February 10, 12
  21. 21. 1> UnboundVariable. * 1: variable UnboundVariable is unboundFriday, February 10, 12
  22. 22. 1> X = 1. 1 2> X. 1 3> Y. * 1: variable Y is unbound 4> Y = 2. 2 5> Y. 2Friday, February 10, 12
  23. 23. 1> User = {"John", "Doe", 35}. ... 2> {Name, Surname, Age} = User. ... 3> Name. "John" 4> Age. 35Friday, February 10, 12
  24. 24. 1> User = {"John", "Doe", 35}. ... 2> {Name, Surname} = User. ** exception error: no match of right hand side value {"John","Doe",35}Friday, February 10, 12
  25. 25. 2> User = {"John", "Doe", 35}. ... 3> {Name, "Doe", 35} = User. ... 4> Name. "John"Friday, February 10, 12
  26. 26. 1> User = {"John", "Doe", 35}. ... 2> {Name, _, Age} = User. ... 3> Age. 35 4> Name. "John"Friday, February 10, 12
  27. 27. 1> _ = 1. 1 2> _. * 1: variable _ is unbound 5> _ = 3. 3Friday, February 10, 12
  28. 28. 1> [Head | Tail] = [1,2,3]. [1,2,3] 2> Head. 1 3> Tail. [2,3]Friday, February 10, 12
  29. 29. 1> [1 | [2 | [3]]]. [1,2,3]Friday, February 10, 12
  30. 30. Modules and FunctionsFriday, February 10, 12
  31. 31. -module(test). -export([main/0]). main() -> "Hello".Friday, February 10, 12
  32. 32. 1> test:main(). "Hello"Friday, February 10, 12
  33. 33. -module(test). -export([factorial/1]). factorial(0) -> 1; factorial(N) -> N * factorial(N - 1).Friday, February 10, 12
  34. 34. $ erlc test.erl $ erl ... 1> test:factorial(3). 6 2> test:factorial(5). 120Friday, February 10, 12
  35. 35. -module(logger). -export([log/1, log/2]). log(Msg) -> {"info", Msg}. log(Level, Msg) -> {Level, Msg}. --------------------8<-------------------- 1> logger:log("Some info message."). {"info","Some info message."} 2> logger:log("error", "Kernel panic!"). {"error","Kernel panic!"}Friday, February 10, 12
  36. 36. log(Msg) -> log("info", Msg). log(Level, Msg) -> {Level, Msg}.Friday, February 10, 12
  37. 37. Function GuardsFriday, February 10, 12
  38. 38. -module(test). -export([fib/1]). fib(0) -> 0; fib(1) -> 1; fib(N) when N > 0 -> fib(N-1) + fib(N-2).Friday, February 10, 12
  39. 39. test:fib(0). 0 3> test:fib(1). 1 5> test:fib(8). 21 6> test:fib(-23). ** exception error: no function clause matching test:fib(-23)Friday, February 10, 12
  40. 40. % Refactored factorial function: factorial(0) -> 1; factorial(N) when is_integer(N) and (N > 0) -> N * factorial(N - 1); factorial(_) -> {error, "Invalid argument"}.Friday, February 10, 12
  41. 41. 7> factorial:factorial(3). 6 8> factorial:factorial(0). 1 9> factorial:factorial(-1). {error,"Invalid argument"} 10> factorial:factorial("a"). {error,"Invalid argument"}Friday, February 10, 12
  42. 42. Concurrency • any function can become a process • process is a function executing in parallel • process shares nothing with other processesFriday, February 10, 12
  43. 43. Processes/Actors • processes are extremely lightweight and fast • ~15 seconds to spawn 100k processes on my machine (MacBook Pro 8.1, OSX)Friday, February 10, 12
  44. 44. spawn(Module, Function, Arguments) -> PidFriday, February 10, 12
  45. 45. Any function can become a processFriday, February 10, 12
  46. 46. 8> io:format("Hello~n"). Hello ... 2> spawn(io, format, ["Hello~n"]). Hello ...Friday, February 10, 12
  47. 47. -module(actor). -export([loop/0]). loop() -> receive die -> io:format("Exiting~n") end.Friday, February 10, 12
  48. 48. 2> Pid = spawn(actor, loop, []). <0.40.0> 3> is_process_alive(Pid). true 4> Pid ! die. Exiting die 5> is_process_alive(Pid). falseFriday, February 10, 12
  49. 49. loop() -> receive die -> io:format("Exiting~n"); Msg -> io:format("Got: ~p~n", [Msg]) end.Friday, February 10, 12
  50. 50. 2> Pid = spawn(actor, loop, []). ... 3> is_process_alive(Pid). true 4> Pid ! "Hello". Got: "Hello" ... 5> is_process_alive(Pid). falseFriday, February 10, 12
  51. 51. loop() -> receive die -> io:format("Exiting~n"); Msg -> io:format("Got: ~p~n", [Msg]), loop() end.Friday, February 10, 12
  52. 52. 2> Pid = spawn(actor, loop, []). ... 3> Pid ! "Hello". Got: "Hello" 4> is_process_alive(Pid). true 5> Pid ! "Hello again!". Got: "Hello again!" 6> is_process_alive(Pid). true 7> Pid ! die. Exiting 8> is_process_alive(Pid). falseFriday, February 10, 12
  53. 53. Processes are executed in parallel and share no dataFriday, February 10, 12
  54. 54. -module(counter). -export([loop/1]). loop(N) -> receive increment -> loop(N + 1); decrement -> loop(N - 1); print -> io:format("Current counter value: ~w~n", [N]), loop(N) end.Friday, February 10, 12
  55. 55. 2> Counter = spawn(counter, loop, [0]). ... 3> Counter ! increment. ... 4> Counter ! increment. ... 5> Counter ! print. Current counter value: 2 ... 6> Counter ! decrement. ... 7> Counter ! print. Current counter value: 1Friday, February 10, 12
  56. 56. 2> C1 = spawn(counter, loop, [0]). <0.40.0> 3> C2 = spawn(counter, loop, [10]). <0.42.0> 4> C1 ! increment, C1 ! increment, C1 ! print. Current counter value: 2 5> C2 ! decrement, C2 ! print. Current counter value: 9Friday, February 10, 12
  57. 57. Distributed ErlangFriday, February 10, 12
  58. 58. spawn(Node, Module, Function, Args) -> PidFriday, February 10, 12
  59. 59. 1> spawn(DistributedNode, test, factorial, [10]).Friday, February 10, 12
  60. 60. s erl -sname slave -setcookie 123!@#qwe ... (slave@Serhiys-MacBook-Pro)1> node(). slave@Serhiys-MacBook-ProFriday, February 10, 12
  61. 61. $ erl -sname master -setcookie 123!@#qwe ... (master@Serhiys-MacBook-Pro)1> net_adm:ping(slave@Serhiys-MacBook-Pro). pong (master@Serhiys-MacBook-Pro)3>spawn(slave@Serhiys- MacBook-Pro, factorial, factorial, [100]). 933262154439441526816992388562667004907159682643816 214685929638952175999932299156089414639761565182862 536979208272237582511852109168640000000000000000000 00000 <6619.52.0>Friday, February 10, 12
  62. 62. Fault ToleranceFriday, February 10, 12
  63. 63. -module(actor). -export([loop/0]). loop() -> receive N -> S = math:sqrt(N), io:format("sqrt(~p) = ~p~n", [N,S]), loop() end.Friday, February 10, 12
  64. 64. 2> Pid = spawn(actor, loop, []). ... 3> Pid ! 4. sqrt(4) = 2.0 ... 5> Pid ! "a". =ERROR REPORT==== 24-Jan-2012::14:06:02 === Error in process <0.40.0> ... 6> is_process_alive(Pid). falseFriday, February 10, 12
  65. 65. Supervisors • supervisors monitor processes and take actions on exit signals • process linking does the trickFriday, February 10, 12
  66. 66. start() -> spawn(actor, restarter, []). restarter() -> process_flag(trap_exit, true), Pid = spawn_link(actor, loop, []), register(myActor, Pid), receive {EXIT, _Pid, _Reason} -> io:format("Process crashed. Restarting~n"), restarter() end.Friday, February 10, 12
  67. 67. 2> actor:start(). 3> myActor ! 4. sqrt(4) = 2.0 4> myActor ! "foo". Process crashed. Restarting =ERROR REPORT==== Error in process <0.41.0> ... 5> is_process_alive(whereis(myActor)). true 6> myActor ! 9. sqrt(9) = 3.0Friday, February 10, 12
  68. 68. Other great features • Hot code loading • OTP Framework •Friday, February 10, 12
  69. 69. Simplicity • You just learned • ~70% of Erlang syntax • most major abstractions: modules, functions and processes • most of the core data typesFriday, February 10, 12
  70. 70. Complexity • Erlang/OTP is a complex framework • Learning it takes time and practiceFriday, February 10, 12
  71. 71. Bad Stuff • syntax • Strings • code organization • libraries • underestimated complexity of OTP and concurrent applications generally • your colleagues will not understand you :)Friday, February 10, 12
  72. 72. Practical Application • Notable uses: • Facebook (Chat) • CouchDB • RabbitMQ • Membase • RiakFriday, February 10, 12
  73. 73. How to learn Erlang?Friday, February 10, 12
  74. 74. http://learnyousomeerlang.com/Friday, February 10, 12
  75. 75. Erlang getting started guide: http://www.erlang.org/doc/getting_started/intro.htmlFriday, February 10, 12
  76. 76. Programming Erlang: Software for a Concurrent World http://pragprog.com/book/jaerlang/programming-erlangFriday, February 10, 12
  77. 77. $ erlFriday, February 10, 12
  78. 78. [$Q, $u, $e, $s, $t, $i, $o, $n, $s, $?].Friday, February 10, 12
  79. 79. Thank youFriday, February 10, 12
  1. ¿Le ha llamado la atención una diapositiva en particular?

    Recortar diapositivas es una manera útil de recopilar información importante para consultarla más tarde.

×