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

Elixir: Programação Funcional e Pragmática @ Encontro Locaweb 2016

Elixir: Programação Funcional e Pragmática @ Encontro Locaweb 2016

Plataformatec

June 22, 2016
Tweet

More Decks by Plataformatec

Other Decks in Technology

Transcript

  1. @georgeguimaraes / @plataformatec
    ELIXIR.
    PROGRAMAÇÃO
    FUNCIONAL E
    PRAGMÁTICA

    View full-size slide

  2. consulting and software engineering

    View full-size slide

  3. ESTAMOS
    CONTRATANDO!

    http://plataformatec.workable.com

    View full-size slide

  4. NOSSA GERAÇÃO TEM
    UM PROBLEMA.
    Desenvolvedores web tem que lidar com
    concorrência. Não há escapatória.

    View full-size slide

  5. CONCORRÊNCIA.
    Capacidade de lidar com várias coisas (ao
    mesmo tempo, ou serialmente).

    View full-size slide

  6. PARALELISMO.
    Capacidade de fazer várias coisas ao mesmo
    tempo.

    View full-size slide

  7. CONCORRÊNCIA.
    Websockets, HTTP2, Alta quantidade de
    requests, demanda instável.

    View full-size slide

  8. THREADS E EVENT
    LOOP.
    Modelos primitivos para lidar com
    concorrência.

    View full-size slide

  9. “what makes multithreaded
    programming difficult is not
    that writing it is hard, but
    that testing it is hard. It’s not
    the pitfalls that you can fall
    into; it’s the fact that you
    don’t necessarily know
    whether you’ve fallen into
    one of them. ”

    View full-size slide

  10. — ROBERT VIRDING
    “Any sufficiently complicated concurrent
    program in another language contains an
    ad hoc informally-specified bug-ridden
    slow implementation of half of Erlang.”

    View full-size slide

  11. PLATAFORMA
    ELIXIR.
    Erlang and OTP, now with modern tooling.

    View full-size slide

  12. Switch
    Switch

    View full-size slide

  13. Switch
    Browser
    Endpoint
    Server

    View full-size slide

  14. — ELIXIR DEVELOPER
    “We stand in the shoulders of giants”

    View full-size slide

  15. #
    #MYELIXIRSTATUS

    View full-size slide

  16. DOCUMENTAÇÃO DE
    ALTO NÍVEL.
    Documentação ruim é bug.

    View full-size slide

  17. FERRAMENTAL
    EMBUTIDO.
    Hex, Mix, ExUnit.

    View full-size slide

  18. LINGUAGEM
    FUNCIONAL.

    View full-size slide

  19. IMUTABILIDADE.
    “Isso muda tudo”

    View full-size slide

  20. list = [1, 2, 3]
    DoSomethingWith(list)
    list = [4, 5, 6]
    AnotherThingWith(list)

    View full-size slide

  21. NÃO TENHO LOOPS!

    View full-size slide

  22. defmodule Fibonacci do
    def calc(0), do: 0
    def calc(1), do: 1
    def calc(n), do: calc(n-1) + calc(n-2)
    end
    Fibonacci.calc(10)
    # => 55

    View full-size slide

  23. MAS, E COMO FAZER
    UM CONTADOR?
    Não é possível mudar o conteúdo de uma
    variável????

    View full-size slide

  24. defmodule Counter do
    def start(value) do
    receive do
    :increment -> start(value + 1)
    {:get, pid} -> send(pid, value)
    end
    end
    end

    View full-size slide

  25. defmodule Counter do
    def start(value) do
    receive do
    :increment -> start(value + 1)
    {:get, caller} -> send(caller, value)
    end
    end
    end
    pid = spawn(fn -> Counter.start(10) end)
    send(pid, :increment)
    send(pid, :increment)
    send(pid, :increment)
    send(pid, {:get, self})
    flush
    # => 13

    View full-size slide

  26. shell Counter.start(10)
    spawn

    View full-size slide

  27. shell 11
    increment

    View full-size slide

  28. shell 12
    increment

    View full-size slide

  29. shell 13
    increment

    View full-size slide

  30. shell 13
    :get, self
    13

    View full-size slide

  31. defmodule Counter do
    def start(value) do
    receive do
    :increment -> start(value + 1)
    {:get, caller} -> send(caller, value)
    end
    end
    end
    pid = spawn(fn -> Counter.start(10) end)
    send(pid, :increment)
    send(pid, :increment)
    send(pid, :increment)
    send(pid, {:get, self})
    flush
    # => 13

    View full-size slide

  32. ACTOR MODEL.
    1. Enviar mensagens para outros atores;
    2. Criar novos atores;
    3. Especificar o comportamento para as
    próximas mensagens.

    View full-size slide

  33. Sequential
    code

    View full-size slide

  34. Sequential
    code
    elixir

    View full-size slide

  35. Sequential
    code
    elixir

    View full-size slide

  36. FAULT-TOLERANT.
    DISTRIBUTED.
    O objetivo não era concorrência.

    View full-size slide

  37. Switch
    Switch

    View full-size slide

  38. CONCORRÊNCIA É UM
    CASO DE DISTRIBUIÇÃO.
    Tá distribuído, mas apenas em uma máquina.

    View full-size slide

  39. defmodule MyApp do
    use Application
    def start(_type, _args) do
    import Supervisor.Spec, warn: false
    children = [
    supervisor(Playfair.Repo, []),
    worker(Playfair.Mailer, []),
    worker(Playfair.FileWriter, []),
    ]
    opts = [strategy: :one_for_one, name: Playfair.Supervisor]
    Supervisor.start_link(children, opts)
    end
    end

    View full-size slide

  40. defmodule MyApp.Mixfile do
    use Mix.Project
    def application do
    [mod: {Playfair, []},
    applications: [:phoenix, :phoenix_html, :cowboy, :logger, :gettext,
    :phoenix_ecto, :postgrex, :calendar]]
    end
    defp deps do
    [
    {:phoenix, "~> 1.1.4"},
    {:postgrex, ">= 0.0.0"},
    {:phoenix_ecto, "~> 2.0"},
    {:phoenix_html, "~> 2.4"},
    {:phoenix_live_reload, "~> 1.0", only: :dev},
    {:gettext, "~> 0.10"},
    {:cowboy, "~> 1.0"},
    {:credo, "~> 0.3", only: [:dev, :test]},
    {:basic_auth, "~> 1.0"},
    {:csv, "~> 1.2.4"},
    {:scrivener, "~> 1.0"},
    {:scrivener_html, "~> 1.0"},
    {:calendar, "~> 0.13"}
    ]
    end
    end

    View full-size slide

  41. ERROS ACONTECEM.
    Let it crash.

    View full-size slide

  42. ESTADO CORRUPTO
    PODE OCORRER.
    Let it crash. O processo vai voltar com um
    estado limpo.

    View full-size slide

  43. MAS SERÁ QUE
    FUNCIONA MESMO?

    View full-size slide

  44. http://blog.whatsapp.com/index.php/
    2012/01/1-million-is-so-2011/
    2 million connections
    on a single node

    View full-size slide

  45. Intel Xeon CPU X5675 @ 3.07GHz
    24 CPU - 96GB
    Using 40% of CPU and Memory

    View full-size slide

  46. MICROSERVIÇOS EM
    UM MUNDO ELIXIR.
    Precisamos sempre de APIs HTTP?

    View full-size slide

  47. node@srv2
    node@srv1
    elixir

    View full-size slide

  48. ONDE APRENDER?.

    View full-size slide

  49. consulting and software engineering

    View full-size slide

  50. DÚVIDAS?

    @plataformatec

    @georgeguimaraes

    View full-size slide

  51. @elixirlang / elixir-lang.org

    View full-size slide