Introduction to Elixir

Introduction to Elixir

Slides for the "Introduction to Elixir" talk I gave at the first Elixir/Erlang meetup in Italy, set in Bologna (October 2015).

Faafc04d9e69b73b9f49995fd4c94d4d?s=128

Andrea Leopardi

October 17, 2015
Tweet

Transcript

  1. introduction to Elixir

  2. Erlang compiles to bytecode

  3. Elixir is what would happen if Erlang, Clojure and Ruby

    somehow had a baby and it wasn't an accident Devin Torres
  4. Andrea Leopardi @whatyouhide

  5. metaprogramming tooling interop concurrency features

  6. features

  7. data structures

  8. 199 "t-rex" :dr_manhattan [:red, :white, :blue] {:error, "nope"} %{hair: :red,

    weight: :high}
  9. immutability

  10. all data structures are immutable

  11. animals = ["lion", "table", "bear"] List.delete(animals, "table") animals #=> ["lion",

    "table", "bear"]
  12. data+functions

  13. data functions modules

  14. String.upcase("meh") m o d u l e f u n

    c t i o n d a t a
  15. defmodule String do def upcase(str) do # ... end end

  16. high-order functions

  17. doubler = fn(i) -> i * 2 end Enum.map([1, 2,

    3], doubler) #=> [2, 4, 6]
  18. pattern matching

  19. = ≠ =

  20. match the structure of the data bind variables while doing

    that
  21. [first, second, third] = [ :houston, :austin, :dallas, ] first

    #=> :houston second #=> :austin
  22. [first, second, _] = [ :houston, :austin, :dallas, ] first

    #=> :houston second #=> :austin
  23. [first, second, _] = [ :houston, :austin, :dallas, :seattle, ]

    ** (MatchError)
  24. protocols

  25. à la Clojure

  26. JSON.encode(thing) defines API

  27. defprotocol JSON do def encode(thing) end

  28. JSON.encode(thing) dispatches to implementation

  29. defimpl JSON, for: Integer do def encode(i), do: # ...

    end
  30. concurrency

  31. Any sufficiently complicated concurrent program in another language contains an

    ad hoc informally-specified bug-ridden slow implementation of half of Erlang. Robert Virding
  32. spawn fn -> IO.puts "from another process" end

  33. not OSprocesses

  34. lightweight isolation message passing

  35. for _ <- 1..100_000 do spawn(fn -> :ok end) end

    lightweight
  36. animals = [:dog, :cat] spawn fn -> animals # it's

    a copy end isolation
  37. isolation per-process garbage collection!!!

  38. message passing the only way processes can communicate

  39. pid = spawn fn -> # ... end send pid,

    "a message" message passing
  40. spawn fn -> receive do msg -> :ok end end

    message passing
  41. spawn_monitor fn -> raise "die!" end receive do m ->

    m end #=> {:DOWN, ...} message passing
  42. actor model

  43. def loop(state) do receive do {from, :get} -> send(from, state)

    loop(state) {from, :put, new_state} -> loop(new_state) end end
  44. def loop(state) do receive do {from, :get} -> send(from, state)

    loop(state) {from, :put, new_state} -> loop(new_state) end end
  45. def loop(state) do receive do {from, :get} -> send(from, state)

    loop(state) {from, :put, new_state} -> loop(new_state) end end
  46. def loop(state) do receive do {from, :get} -> send(from, state)

    loop(state) {from, :put, new_state} -> loop(new_state) end end
  47. def loop(state) do receive do {from, :get} -> send(from, state)

    loop(state) {from, :put, new_state} -> loop(new_state) end end
  48. def loop(state) do receive do {from, :get} -> send(from, state)

    loop(state) {from, :put, new_state} -> loop(new_state) end end
  49. def loop(state) do receive do {from, :get} -> send(from, state)

    loop(state) {from, :put, new_state} -> loop(new_state) end end
  50. def loop(state) do receive do {from, :get} -> send(from, state)

    loop(state) {from, :put, new_state} -> loop(new_state) end end
  51. def loop(state) do receive do {from, :get} -> send(from, state)

    loop(state) {from, :put, new_state} -> loop(new_state) end end spawn(&loop/0)
  52. Node.spawn(node, fun)

  53. interop

  54. Elixir data structures are Erlang data structures

  55. lists:map(fun(X) -> X * 2 end, [1, 2, 3]). :lists.map(fn(x)

    -> x * 2 end, [1, 2, 3])
  56. :queue.in("customer1", :queue.new())

  57. tooling

  58. iex> 8 * 2 16 IEx

  59. mix new my_app compile test deps.get Mix

  60. test "math works!" do assert 1 + 1 end ExUnit

  61. Hey there, <%= name %>! EEx

  62. defp deps do [{:phoenix, "~> 1.0"}, {:ecto, "~> 1.0"}] end

    Hex
  63. so

  64. lots of companies use Elixir in prod it's awesome to

    work with thriving ecosystem, smart devs phoenix metaprogramming
  65. @whatyouhide