Erlang/OTP for Rubyists

3,175 views

Published on

An introduction to Erlang I gave to the Kansas City Ruby Users Group on July 14, 2009.

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

No Downloads
Views
Total views
3,175
On SlideShare
0
From Embeds
0
Number of Embeds
227
Actions
Shares
0
Downloads
0
Comments
0
Likes
12
Embeds 0
No embeds

No notes for slide

Erlang/OTP for Rubyists

  1. 1. Erlang/OTP for Rubyists Sean Cribbs KC.rb - July 2009
  2. 2. Ruby Makes easy things trivial and hard things fun!
  3. 3. Erlang Makes easy things possible and impossible things trivial! Orion Henry and Blake Mizerany (Heroku)
  4. 4. What is Erlang? •Language, Runtime and Libraries (OTP) •Developed and maintained by Ericsson •Started mid 1980s, open-sourced 1998
  5. 5. Who uses it?
  6. 6. Who uses it?
  7. 7. Object-oriented Programming
  8. 8. object object object object object object object object object object object object Object-oriented object Programming object object object object object object object object object
  9. 9. Object-oriented Programming
  10. 10. Concurrency-oriented Programming
  11. 11. process process process process process process process process process process process process Concurrency-oriented process Programming process process process process process process process process process
  12. 12. Concurrency-oriented Programming
  13. 13. Fault-tolerance IFF Concurrency
  14. 14. Error Handling Intra-process Inter-process Inter-machine
  15. 15. Sharing doesn’t scale •Deadlocks •Race Conditions •Starvation •Synchronization •Data Locality •Corruption
  16. 16. Message passing is the answer
  17. 17. Cool Features •Concurrent •Distributed •Share-nothing •Fault-tolerant •Hot upgrades •Highly available
  18. 18. Big Differences •Functional - no objects •Single assignment, few side-effects •Initially challenging syntax •Nothing like gems
  19. 19. Are you ready to be UNCOMFORTABLY PARALLEL?
  20. 20. Spawn Pid = spawn( fun()-> io:format("Hello, Joe.") end).
  21. 21. Send Messages Pid ! {greet, “Joe”}.
  22. 22. Receive Messages loop() -> receive {greet,S} -> io:format(“Hello, ~s”, [S]), loop(); stop -> ok; _ -> loop() end.
  23. 23. Syntax Overview
  24. 24. Numbers 1 -1 2501 3.14 12345678987654321.987654321
  25. 25. Atoms foo type true false ‘EXIT’
  26. 26. Lists [1,2,3] [“dog”, “cat”, “fish”] “hello, world”
  27. 27. Tuples {name, “Sean”} {address, “8700 State Line Rd”, “Leawood”, “KS”} {{line, 10},{column,30}} {{2009,7,14},{19,0,0}}
  28. 28. Binaries <<“chunk of bytes”>> <<131,108,0,0,0,2,100,0,3,111,110,101,100 ,0,3,116,119,111, 106>>
  29. 29. Some Weirdos “abc” == [97,98,99]. % lists of bytes true, false % atoms $a == 97. % characters (ASCII) P#person.name % compile-time struct
  30. 30. Assignment
  31. 31. Pattern Matching
  32. 32. Pattern Matching A = 5. B = 10. Group = “KC.rb”.
  33. 33. Pattern Matching {Type, Value} = {username, “sean”}. [{username, Value}, {orders, Orders}] = [{username, “sean”}, {orders, [1,2,3]}].
  34. 34. Single Assignment Really, this is just pattern matching. A = 5. A = A + 1. % error: badmatch (5 = 6)
  35. 35. Lists functional programming’s old friend
  36. 36. List Operations [First|Rest] = [1,2,3,4,5]. [H|T] = [1,2,3,4,5]. List1 ++ List2.
  37. 37. List Comprehensions Think select/filter + map. A = [1,2,3,4,5]. [X * 2 || X <- A]. % [2,4,6,8,10] [X || X <- A, X rem 2 == 0]. % [2,4]
  38. 38. Functions even_or_odd(Num) -> if Num rem 2 == 0 -> even; true -> odd end.
  39. 39. Functions even_or_odd(N) when N rem 2 == 0 -> even; even_or_odd(N) -> odd.
  40. 40. Functions even_or_odd(N) when is_number(N), N rem 2 == 0 -> even; even_or_odd(N) when is_number(N) -> odd.
  41. 41. Anonymous Functions like lambda/proc, block Double = fun(X) -> 2*X end. Double(2). % 4 lists:map(Double, [1,2,3,4,5]). % [2,4,6,8,10]
  42. 42. No Loops! use recursion
  43. 43. A Little CS Theorem iterative === recursive
  44. 44. List “iteration” print([]) -> ok; print([H|T]) -> io:format(“~p”, [H]), print(T).
  45. 45. List “iteration” print([]) -> ok; print([H|T]) -> io:format(“~p”, [H]), print(T). print(T).
  46. 46. Normal Recursion loop() loop() loop() loop()
  47. 47. Tail Recursion (aka tail-call optimization) loop()
  48. 48. Concurrent programs can be complicated.
  49. 49. Behaviors “patterns” •gen_server - client-server •gen_fsm - state machine •supervisor - fault-tolerance
  50. 50. How does it scale? •Micro-benchmarks, arithmetic slow •Best for network(ed) apps •Campfire just as fast as C, ~ 2-4ms •Near-linear speedup on multicores •GC per-process, generational
  51. 51. Sean ! Questions.

×