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 full-size slide

  2. Erlang
    compiles to
    bytecode

    View full-size 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 full-size slide

  4. Andrea Leopardi
    @whatyouhide

    View full-size slide

  5. metaprogramming
    tooling
    interop
    concurrency
    features

    View full-size slide

  6. data structures

    View full-size slide

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

    View full-size slide

  8. immutability

    View full-size slide

  9. all data structures
    are immutable

    View full-size slide

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

    View full-size slide

  11. data+functions

    View full-size slide

  12. data
    functions
    modules

    View full-size slide

  13. String.upcase("meh")
    m

    o

    d

    u

    l

    e
    f

    u

    n

    c

    t

    i

    o

    n
    d

    a

    t

    a

    View full-size slide

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

    View full-size slide

  15. high-order functions

    View full-size slide

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

    View full-size slide

  17. pattern matching

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  22. à la Clojure

    View full-size slide

  23. JSON.encode(thing)
    defines API

    View full-size slide

  24. defprotocol JSON do
    def encode(thing)
    end

    View full-size slide

  25. JSON.encode(thing)
    dispatches to implementation

    View full-size slide

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

    View full-size slide

  27. 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 full-size slide

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

    View full-size slide

  29. not OSprocesses

    View full-size slide

  30. lightweight
    isolation
    message passing

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  33. isolation
    per-process garbage
    collection!!!

    View full-size slide

  34. message passing
    the only way
    processes can
    communicate

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size slide

  46. 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 full-size slide

  47. Node.spawn(node, fun)

    View full-size slide

  48. Elixir data structures
    are
    Erlang data structures

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  51. iex> 8 * 2
    16
    IEx

    View full-size slide

  52. mix new my_app
    compile
    test
    deps.get
    Mix

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  57. @whatyouhide

    View full-size slide