Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Bootstrap |> Elixir - Easy fun for busy developers

579 views

Published on

Tutorial for Elixir. Why it is awesome and why you should dive into the ecosystem.

Published in: Technology

Bootstrap |> Elixir - Easy fun for busy developers

  1. 1. Bootstrap EASY FUN FOR BUSY DEVELOPERS |> Elixir
  2. 2. YET ANOTHER PROGRAMMING LANGUANGE? Y A P L
  3. 3. DON’T RUN AWAY Elixir is special!
  4. 4. Elixir in a Nutshell = + Elixir in a Nutshell +
  5. 5. PROGRAMMERS OFTEN FEEL JOY WHEN THEY CAN CONCENTRATE ON THE CREATIVE SIDE OF PROGRAMMING, SO RUBY IS DESIGNED TO MAKE PROGRAMMERS HAPPY. I CONSIDER A PROGRAMMING LANGUAGE AS A USER INTERFACE, SO IT SHOULD FOLLOW THE PRINCIPLES OF USER INTERFACE. Yukihiro Matsumoto, Ruby Inventor Why Ruby is awesome
  6. 6. Principle of Conciseness Principle of Consistency Principle of Flexibility Why Ruby is awesome
  7. 7. THE AXD301 HAS ACHIEVED A NINE NINES RELIABILITY (YES, YOU READ THAT RIGHT, 99.9999999%). LET’S PUT THIS IN CONTEXT: 5 NINES IS RECKONED TO BE GOOD (5.2 MINUTES OF DOWNTIME/YEAR). 7 NINES ALMOST UNACHIEVABLE ... BUT WE DID 9. Joe Armstrong, Erlang Designer Why Erlang is awesome
  8. 8. Battle-proven BEAM and OTP Ever seen WhatsApp crash? Reactive before Reactive-is-HipTm Actors before Actors-are—HipTm Why Erlang is awesome
  9. 9. Why Clojure is awesome Because Rich Hickey is always right
  10. 10. SO…WHY NOT DO RUBY, ERLANG OR CLOJURE?
  11. 11. WHY NOT USE RUBY? SPEED, CONCURRENCY, SCALABILITY
  12. 12. WHY NOT USE ERLANG? SYNTAX, BAROQUE TOOLING
  13. 13. WHY NOT USE CLOJURE? …NO REASON, REALLY. GO AHEAD USE IT!
  14. 14. Scalability, lightweight Threads Fault-tolerance, Supervisor Functional, immutable DSLs using Meta-Programming Mix, Hex, ExUnit,… REPL for easy learning Why Elixir is awesome
  15. 15. Scalability, lightweight Threads Fault-tolerance, Supervisor Functional, immutable DSLs using Meta-Programming Mix, Hex, ExUnit,… REPL for easy learning Why Elixir is awesome
  16. 16. 100000 Processes 1..100_000 |> Enum.map(&(Task.async( fn -> &1 * &1 end ))) |> Enum.map(&Task.await/1)
  17. 17. [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500, ...] 100000 Processes
  18. 18. 1..100_000 |> Enum.map(&(Task.async( fn -> &1 * &1 end ))) |> Enum.map(&Task.await/1) ?what? 100000 Processes
  19. 19. Quick syntax bootcamp
  20. 20. Creating a project $ mix new demo $ cd demo $ mix test
  21. 21. Read Eval Print Loop $ iex -S mix Erlang/OTP 18 … Interactive Elixir (1.2.4) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> IO.puts “Hello Elixir” Hello Elixir :ok
  22. 22. Transform - don’t mutate 10 |> fib |> IO.puts IO.puts(fib(10)) transformation pipeline
  23. 23. Declaring functions def fib(0), do: 0 def fib(0) do 0 end (n-1) + fib(n-2) end same thing
  24. 24. Pattern matching def fib(0), do: 0 def fib(1), do: 1 def fib(n) when n > 1 iex> fib(1) pattern matching
  25. 25. Guard expressions def fib(n) when n > 1 do fib(n-1) + fib(n-2) end guard expression
  26. 26. iex> fib(-3) ** (FunctionClauseError) no function clause matching in fib/1 (fibnew) lib/fib2.ex:2: fib(-3) Things go wrong
  27. 27. Anonymous functions fn n -> n + 2 end &( &1 + 2 ) same thing
  28. 28. Function shortcuts IO.puts/2 def puts(device, item) /2 denotes the number of args
  29. 29. Compile-time structural decomposition def sqrt([h | t]), do: … iex> sqrt([1, 2, 3, 4 ])
  30. 30. Show me the code Demo
  31. 31. ELIXIR APPLICATION DESIGN APPLICATIONS, SUPERVISORS, PROCESSES
  32. 32. Shared resources Shared state Shared stability PROCESS SHARED STATE
  33. 33. Shared nothing Message passing PROCESS PROCESS PROCESS PROCESS PROCESS PROCESS PROCESS PROCESS PROCESS PROCESS PROCESS PROCESS PROCESS
  34. 34. Abstractions for Resilience Agent…abstractions around state GenServer…long-running, messaging Supervisor…let it crash Application…just think component
  35. 35. Abstractions for Resilience Agent…abstractions around state GenServer…long-running, messaging Supervisor…let it crash Application…just think component
  36. 36. Message-based design using GenServer BA GenServer.start_link {:ok, #PID<0.112.0>}
  37. 37. Message-based design using GenServer BA GenServer.call(B, {:sum, 1..3})
  38. 38. Message-based design using GenServer BA handle_call({:sum, 1..3}, from, state)
  39. 39. Message-based design using GenServer BA {:reply, {:ok, 6}, new_state)
  40. 40. GenServer Demo
  41. 41. Move risk to the bottom of the supervision tree APPLICATION SUPERVISOR WORKER C WORKER A WORKER B Here be dragons… FILE IO DB ACCESS
  42. 42. Supervisors watch their children APPLICATION SUPERVISOR WORKER C WORKER A WORKER B :one_for_one!
  43. 43. :one_for_one replaces failed process APPLICATION SUPERVISOR WORKER C WORKER A WORKER B WORKER B’ Auto-restart
  44. 44. Supervisor Demo
  45. 45. Processes are distributed across nodes PROCESS PROCESS PROCESS PROCESS PROCESS PROCESS PROCESS PROCESS PROCESS PROCESS PROCESS PROCESS PROCESS :”one@foo” :”two@foo”
  46. 46. Nodes and distribution Demo
  47. 47. MORE COOL FEATURES TEASERS ONLY
  48. 48. Testing documentation @doc """ iex> sieve(10) [2, 3, 5, 7] """ def sieve(n) do ... end defmodule Test do use ExUnit.Case doctest … end
  49. 49. Hygienic Macros defmacro time([do: body]) do quote do s = :os.system_time(…) unquote(body) f = :os.system_time(…) info(“Took #{s-f} ms") end end time do IO.puts "Something" :timer.sleep(100) end [info] Took 101 ms
  50. 50. There is so much more
  51. 51. Protocols Sigils Umbrella projects Ecto Phoenix Hot code replacement
  52. 52. SHOULD WE ALL START BUILDING EVERYTHING WITH ELIXIR?
  53. 53. WELL….MAYBE NOT?!
  54. 54. New insights and ideas Clean patterns Architecture for IoT Vibrant Community
  55. 55. 学⼀一⻔门语⾔言,就是多⼀一个观察世界的窗户。 To learn a language is to have one more window from which to look at the world
  56. 56. Elixir Homepage, http://elixir-lang.org/ Dave Thomas, Programming Elixir Fred Hebert, Stuff Goes Bad: Erlang in Anger José Valim, Introduction to Elixir https:// youtu.be/41PvAPSX0wg Slides + Code, https://git.io/vKUGc Do you want to know more?
  57. 57. Thank you very much! <david.schmitz@senacor.com> @koenighotze

×