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

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).

Andrea Leopardi

October 17, 2015
Tweet

More Decks by Andrea Leopardi

Other Decks in Programming

Transcript

  1. introduction to
    Elixir

    View Slide

  2. Erlang
    compiles to
    bytecode

    View Slide

  3. Elixir is what would happen if Erlang, Clojure and
    Ruby somehow had a baby and it wasn't an
    accident
    Devin Torres

    View Slide

  4. Andrea Leopardi
    @whatyouhide

    View Slide

  5. metaprogramming
    tooling
    interop
    concurrency
    features

    View Slide

  6. features

    View Slide

  7. data structures

    View Slide

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

    View Slide

  9. immutability

    View Slide

  10. all data structures
    are immutable

    View Slide

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

    View Slide

  12. data+functions

    View Slide

  13. data
    functions
    modules

    View Slide

  14. String.upcase("meh")
    m

    o

    d

    u

    l

    e
    f

    u

    n

    c

    t

    i

    o

    n
    d

    a

    t

    a

    View Slide

  15. defmodule String do
    def upcase(str) do
    # ...
    end
    end

    View Slide

  16. high-order functions

    View Slide

  17. doubler = fn(i) -> i * 2 end
    Enum.map([1, 2, 3], doubler)
    #=> [2, 4, 6]

    View Slide

  18. pattern matching

    View Slide

  19. = ≠ =

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  23. [first, second, _] = [
    :houston,
    :austin,
    :dallas,
    :seattle,
    ]
    ** (MatchError)

    View Slide

  24. protocols

    View Slide

  25. à la Clojure

    View Slide

  26. JSON.encode(thing)
    defines API

    View Slide

  27. defprotocol JSON do
    def encode(thing)
    end

    View Slide

  28. JSON.encode(thing)
    dispatches to implementation

    View Slide

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

    View Slide

  30. concurrency

    View Slide

  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

    View Slide

  32. spawn fn ->
    IO.puts "from another process"
    end

    View Slide

  33. not OSprocesses

    View Slide

  34. lightweight
    isolation
    message passing

    View Slide

  35. for _ spawn(fn -> :ok end)
    end
    lightweight

    View Slide

  36. animals = [:dog, :cat]
    spawn fn ->
    animals # it's a copy
    end
    isolation

    View Slide

  37. isolation
    per-process garbage
    collection!!!

    View Slide

  38. message passing
    the only way
    processes can
    communicate

    View Slide

  39. pid = spawn fn ->
    # ...
    end
    send pid, "a message"
    message passing

    View Slide

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

    View Slide

  41. spawn_monitor fn ->
    raise "die!"
    end
    receive do m -> m end
    #=> {:DOWN, ...}
    message passing

    View Slide

  42. actor model

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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)

    View Slide

  52. Node.spawn(node, fun)

    View Slide

  53. interop

    View Slide

  54. Elixir data structures
    are
    Erlang data structures

    View Slide

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

    View Slide

  56. :queue.in("customer1",
    :queue.new())

    View Slide

  57. tooling

    View Slide

  58. iex> 8 * 2
    16
    IEx

    View Slide

  59. mix new my_app
    compile
    test
    deps.get
    Mix

    View Slide

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

    View Slide

  61. Hey there, !
    EEx

    View Slide

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

    View Slide

  63. so

    View Slide

  64. lots of companies use Elixir in prod
    it's awesome to work with
    thriving ecosystem, smart devs
    phoenix
    metaprogramming

    View Slide

  65. @whatyouhide

    View Slide