Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rubyists! Have a sip of Elixir!

Rubyists! Have a sip of Elixir!

This talk is about Elixir – a functional, meta-programming aware language built on top of the Erlang VM.

I introduce some of Elixir's more interesting language features. Then I will demonstrate some of the features that Erlang gives Elixir for free, such as the OTP framework, that let's you build fault tolerant and distributed systems.

So come along and join me to experience programming joy.

F3fe8fa67ae6dbc1d19189e80c9e96a2?s=128

Benjamin Tan Wei Hao

November 18, 2014
Tweet

Transcript

  1. Rubyists: Have a sip of Elixir RUBYCONF | 18 Nov

    2014 | SAN DEIGO BENJAMIN TAN WEI HAO @BENTANWEIHAO
  2. None
  3. Thanks!

  4. We are (always!) hiring! Thanks!

  5. None
  6. None
  7. None
  8. None
  9. None
  10. None
  11. None
  12. None
  13. None
  14. None
  15. None
  16. None
  17. None
  18. None
  19. None
  20. Backstory

  21. Joe Armstrong, Erlang inventor

  22. Mike Williams Robert Virding Bjarne Dacker

  23. Switch

  24. Switch

  25. Switch

  26. Switch

  27. Switch Switch Switch

  28. Who uses Erlang

  29. None
  30. ✔ Standard Library ✔ Tooling ✔ Metaprogramming ✔ Syntax ✔

    Interoperability
  31. why learn Elixir

  32. TOOLS the ✔ Interactive Elixir ✔ mix build tool

  33. iex

  34. mix

  35. PIPE Operator the *Awesome*

  36. The Pipe operator

  37. The Pipe operator FAIL:

  38. The Pipe operator FAIL: WITH PIPES:

  39. WITH PIPES: WITH PIPES + SYNTAX SUGAR!

  40. WITH PIPES: WITH PIPES + SYNTAX SUGAR! Function Capture

  41. A Real World EXAMPLE OUTPUT:

  42. A Real World EXAMPLE

  43. A Real World EXAMPLE

  44. A Real World EXAMPLE

  45. A Real World EXAMPLE

  46. A Real World EXAMPLE

  47. None
  48. None
  49. Concurrency model Actor

  50. 1. is a Process An Actor:

  51. 1. is a Process 2. performs a specific task An

    Actor:
  52. 1. is a Process 2. performs a specific task 3.

    can send and receive a message An Actor:
  53. 1. is a Process 2. performs a specific task 3.

    can send and receive a message 4. responds to specific types of messages An Actor:
  54. 1. is a Process 2. performs a specific task 3.

    can send and receive a message 4. responds to specific types of messages 5. no shared memory with other processes An Actor:
  55. processes THE BASIC CONCURRENCY PRIMITIVE

  56. This is a Process <0.88.0>

  57. <0.88.0> This is a Process ID (pid)

  58. a Process communicates <0.81.0> by sending messages <0.88.0> send <0.81.0>,

    “OHAI”
  59. a Process communicates <0.81.0> <0.88.0> send <0.88.0>,“WASSAP” and receiving messages.

  60. None
  61. hello ACKERMANN A CONCURRENCY DEMO

  62. The Ackermann function

  63. The Ackermann function

  64. The Ackermann function

  65. The Ackermann function

  66. None
  67. None
  68. .

  69. Step 1: spawn a Process

  70. Step 1: spawn a Process <0.61.0>

  71. <0.61.0> Step 2: Send a Message send w1, {1, 2}

  72. <0.61.0> Step 2: Send a Message send w1, {1, 2}

  73. <0.61.0> Step 2: Send a Message send w1, {1, 2}

  74. <0.61.0> Step 2: Send a Message send w1, {1, 2}

  75. <0.61.0> Step 2: Send a Message send w1, “LOLWUT”

  76. <0.61.0> Step 2: Send a Message send w1, “LOLWUT”

  77. None
  78. None
  79. None
  80. None
  81. None
  82. fault tolerance STAYING UP WHEN SH*T HAPPENS

  83. Supervisor A Supervision Tree Worker

  84. Supervisor A Supervision Tree hierachy Worker

  85. <0.81.0> Supervisor: Detected <0.81.0> died!

  86. Supervisor: Restart child! <0.88.0>

  87. <0.81.0> Supervisor: Detected <0.81.0> died!

  88. <0.81.0> Supervisor: Kill, kill, kill!!! <0.79.0> <0.80.0>

  89. Supervisor: Restart all the children! <0.88.0> <0.89.0> <0.90.0>

  90. Supervisor: Restart all the children! <0.88.0> <0.89.0> <0.90.0>

  91. None
  92. None
  93. let’s build HTTP LOAD TESTER

  94. AN Example Run of Blitzy

  95. AN Example Run of Blitzy

  96. None
  97. The Protocol Coordinator Process n = 3 # = 0

    Coordinator: I need to handle 3 workers. I have received messages from 0 workers.
  98. The Protocol Coordinator Process Worker Process send ,{:ok,5000} Worker: Hey

    Coordinator, yahoo.com took 5000 msecs. <0.81.0> n = 3 # = 0
  99. The Protocol Coordinator Process Coordinator: Ok, now I have 2

    more workers to hear from. n = 3 # = 1
  100. The Protocol Coordinator Process Worker Process send ,{:error, :404} Worker:

    Hey Coordinator, yahoo.com errored out. <0.83.0> n = 3 # = 1
  101. The Protocol Coordinator Process Coordinator: Ok, now I have 1

    more worker to hear from. n = 3 # = 2
  102. A Serial HTTP LOAD TESTER

  103. Worker Process http://www.yahoo.com |> HTTP Client

  104. HTTP Client http://www.yahoo.com |> {:ok, %{status_code: 200}} {:error, %{reason: :timeout}}

  105. HTTP Client http://www.yahoo.com |> {:ok, %{status_code: 200}} |> do_request/2 {:ok,

    5000}
  106. HTTP Client http://www.yahoo.com |> |> do_request/2 {:error, %{reason: :timeout}} {:error,:timeout}

  107. HTTP Client http://www.yahoo.com |> |> do_request/2 {:error, %{reason: :404}} {:error,:unknown}

  108. None
  109. {:error,:timeout} {:ok, 5000} n = 3 # = 1

  110. Coordinator Process

  111. Coordinator Process Hi, I am Coordinator. n = 3 #

    = 0
  112. None
  113. n = 3 # = 3 BASE CASE: ! !

    ! ! ! ! PROCESSED WORKERS == NUMBER OF WORKERS SAME VALUE!
  114. {:ok, 5000} n = 3 # = 1

  115. {:error,:timeout} n = 3 # = 1

  116. Running the program

  117. Running the program

  118. TASKS EXECUTING COMPUTATIONS ASYNCHRONOUSLY

  119. Tasks Example

  120. Tasks Example

  121. Running the program n = 3 # = 0

  122. Running the program n = 3 # = 0 worker

    1
  123. Running the program n = 3 # = 1 worker

    2
  124. Running the program n = 3 # = 2 worker

    3
  125. Running the program n = 3 # = 3 RESULT:

    2 succeeded, 1 failure.
  126. None
  127. A Concurrent HTTP LOAD TESTER

  128. Running the program

  129. Serial: Concurrent:

  130. Running the program n = 3 # = 0 worker

    1 worker 2 worker 3
  131. None
  132. A Distributed HTTP LOAD TESTER

  133. Erlang Runtime System Erlang Runtime System A B C send(B,

     Msg) Network
  134. Erlang Runtime System Erlang Runtime System A B C send(C,

    Msg) Network
  135. Running the program

  136. Running the program MASTER

  137. Running the program MASTER SLAVE 1 SLAVE 2 SLAVE 3

  138. Running the program

  139. Running the program

  140. Running the program

  141. Running the program

  142. None
  143. None
  144. Running the program

  145. Running the program

  146. None
  147. None
  148. None
  149. Chris McCord! @chrismccord

  150. http://www.elixirsips.com

  151. None
  152. None
  153. http://benjamintanweihao.github.io

  154. None
  155. Coming Soon-ish!

  156. HEATHER MILLER for the slide designs I stole. Credits My

    HUI LING, for letting me ignore her. http://misterjazzz.deviantart.com/art/Constructicons-with-Prowl-C-446618997 http://thisotplife.tumblr.com http://battlefordreamisland.wikia.com/ Image Credits Network by Brennan Novak from The Noun Project Fork by Dmitry Baranovskiy from The Noun Project
  157. Fin. Questions? Thank you! BENJAMIN TAN WEI HAO @BENTANWEIHAO