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

Elixir: Programação Funcional e Pragmática @ En...

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. NOSSA GERAÇÃO TEM UM PROBLEMA. Desenvolvedores web tem que lidar

    com concorrência. Não há escapatória.
  2. “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. ”
  3. — ROBERT VIRDING “Any sufficiently complicated concurrent program in another

    language contains an ad hoc informally-specified bug-ridden slow implementation of half of Erlang.”
  4. 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
  5. defmodule Counter do def start(value) do receive do :increment ->

    start(value + 1) {:get, pid} -> send(pid, value) end end end
  6. 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
  7. 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
  8. ACTOR MODEL. 1. Enviar mensagens para outros atores; 2. Criar

    novos atores; 3. Especificar o comportamento para as próximas mensagens.
  9. 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
  10. 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
  11. Intel Xeon CPU X5675 @ 3.07GHz 24 CPU - 96GB

    Using 40% of CPU and Memory