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

The ABCs of OTP

The ABCs of OTP

March 2nd, 2017 @ ElixirDaze

Jesse J. Anderson

March 02, 2017
Tweet

More Decks by Jesse J. Anderson

Other Decks in Technology

Transcript

  1. Main OTP Components (and other stuff like GenStage, GenEvent, GenFSM,

    etc) (and more stuff like Application, ETS, Mnesia)
  2. Main OTP Components (and other stuff like GenStage, GenEvent, GenFSM,

    etc) (and more stuff like Application, ETS, Mnesia) (even more stuff like Dialyzer, gen_tcp, Observer)
  3. iex(2)> pid = spawn(fn -> IO.puts(“Hello ElixirDaze”) end) Hello ElixirDaze

    #PID<0.65.0> iex(3)> Process.alive?(pid) false iex(4)>
  4. defmodule FantasyTeam.Basic do def start_link do spawn_link(__MODULE__, :loop, [%{}]) end

    def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end end
  5. def loop(state) do receive do {:add, name} -> player =

    FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, state) loop(state)
  6. def loop(state) do receive do {:add, name} -> player =

    FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, state) loop(state)
  7. FantasyTeam.Player.find("Russell Wilson") %{ active: "1", college: "Wisconsin", display_name: "Russell Wilson",

    dob: “1988-11-29”, fname: "Russell", height: "5-11", jersey: "3", lname: "Wilson", player_id: "1847", position: "QB", team: "SEA", weight: "206" }
  8. def loop(state) do receive do {:add, name} -> player =

    FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, state) loop(state)
  9. def loop(state) do receive do {:add, name} -> player =

    FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, state) loop(state)
  10. def loop(state) do receive do {:add, name} -> player =

    FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, state) loop(state)
  11. def loop(state) do receive do {:add, name} -> player =

    FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, state) loop(state)
  12. def loop(state) do receive do {:add, name} -> player =

    FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, state) loop(state)
  13. def loop(state) do receive do {:add, name} -> player =

    FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, state) loop(state)
  14. def loop(state) do receive do {:add, name} -> player =

    FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, state) loop(state)
  15. def loop(state) do receive do {:add, name} -> player =

    FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, state) loop(state)
  16. defmodule FantasyTeam.Basic do def start_link do spawn_link(__MODULE__, :loop, [%{}]) end

    def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end end
  17. iex(1)> pid = FantasyTeam.Basic.start_link #PID<0.65.0> iex(2)> send(pid, {:add, “Russell Wilson”})

    {:add, “Russell Wilson”} iex(3)> send(pid, {:add, “Doug Baldwin”}) {:add, “Doug Baldwin”} iex(4)>
  18. iex(1)> pid = FantasyTeam.Basic.start_link #PID<0.65.0> iex(2)> send(pid, {:add, “Russell Wilson”})

    {:add, “Russell Wilson”} iex(3)> send(pid, {:add, “Doug Baldwin”}) {:add, “Doug Baldwin”} iex(4)> send(pid, {:remove, “Doug Baldwin”}) {:remove, “Doug Baldwin”} iex(5)>
  19. iex(1)> pid = FantasyTeam.Basic.start_link #PID<0.65.0> iex(2)> send(pid, {:add, “Russell Wilson”})

    {:add, “Russell Wilson”} iex(3)> send(pid, {:add, “Doug Baldwin”}) {:add, “Doug Baldwin”} iex(4)> send(pid, {:remove, “Doug Baldwin”}) {:remove, “Doug Baldwin”} iex(5)> send(pid, {:team, self}) {:team, #PID<0.123.0>} iex(6)>
  20. iex(1)> pid = FantasyTeam.Basic.start_link #PID<0.65.0> iex(2)> send(pid, {:add, “Russell Wilson”})

    {:add, “Russell Wilson”} iex(3)> send(pid, {:add, “Doug Baldwin”}) {:add, “Doug Baldwin”} iex(4)> send(pid, {:remove, “Doug Baldwin”}) {:remove, “Doug Baldwin”} iex(5)> send(pid, {:team, self}) {:team, #PID<0.123.0>} iex(6)> flush {:ok, %{“Russell Wilson” => %{position: “QB”, team: “SEA”, ...}}
  21. defmodule FantasyTeam.Basic do def start_link do spawn_link(__MODULE__, :loop, [%{}]) end

    def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end end
  22. defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do

    GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks def init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end end defmodule FantasyTeam.Basic do def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end end
  23. defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do

    GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks def init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end end defmodule FantasyTeam.Basic do def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end end
  24. defmodule FantasyTeam.MyGenServer do use GenServer ... defmodule FantasyTeam.Basic do def

    start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end end
  25. defmodule FantasyTeam.MyGenServer do use GenServer. ... defmodule FantasyTeam.Basic do def

    start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end end
  26. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  27. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  28. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  29. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  30. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  31. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  32. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  33. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  34. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  35. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  36. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  37. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  38. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  39. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  40. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  41. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  42. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  43. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  44. def start_link do GenServer.start_link(__MODULE__, :ok, []) end # Callbacks def

    init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do receive do {:add, name} -> player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) loop(new_state) {:remove, name} -> new_state = Map.delete(state, name) loop(new_state) {:team, pid} -> send(pid, {:ok, state}) loop(state) end end
  45. defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do

    GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  46. defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do

    GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  47. defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do

    GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  48. defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do

    GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  49. defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do

    GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  50. defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do

    GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  51. defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do

    GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  52. defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do

    GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  53. defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do

    GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  54. defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do

    GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  55. defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do

    GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  56. defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do

    GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  57. defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do

    GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks def init(:ok) do {:ok, %{}} end def handle_cast({:add, name}, state) do player = FantasyTeam.Player.find(name) new_state = Map.put(state, name, player) {:noreply, new_state} end def handle_cast({:remove, name}, state) do new_state = Map.delete(state, name) {:noreply, new_state} end def handle_call(:team, _from, state) do {:reply, state, state} end end
  58. defmodule FantasyTeam.SingleServer do use GenServer @name __MODULE__ # API def

    start_link do GenServer.start_link(__MODULE__, :ok, name: @name) end def add(name) do GenServer.cast(@name, {:add, name}) end def remove(name) do GenServer.cast(@name, {:remove, name}) end def team do GenServer.call(@name, :team) end # Callbacks ... defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  59. defmodule FantasyTeam.SingleServer do use GenServer @name __MODULE__ # API def

    start_link do GenServer.start_link(__MODULE__, :ok, name: @name) end def add(name) do GenServer.cast(@name, {:add, name}) end def remove(name) do GenServer.cast(@name, {:remove, name}) end def team do GenServer.call(@name, :team) end # Callbacks ... defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  60. defmodule FantasyTeam.SingleServer do use GenServer @name __MODULE__ # API def

    start_link do GenServer.start_link(__MODULE__, :ok, name: @name) end def add(name) do GenServer.cast(@name, {:add, name}) end def remove(name) do GenServer.cast(@name, {:remove, name}) end def team do GenServer.call(@name, :team) end # Callbacks ... defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  61. defmodule FantasyTeam.SingleServer do use GenServer @name __MODULE__ # API def

    start_link do GenServer.start_link(__MODULE__, :ok, name: @name) end def add(name) do GenServer.cast(@name, {:add, name}) end def remove(name) do GenServer.cast(@name, {:remove, name}) end def team do GenServer.call(@name, :team) end # Callbacks ... defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  62. defmodule FantasyTeam.SingleServer do use GenServer @name __MODULE__ # API def

    start_link do GenServer.start_link(__MODULE__, :ok, name: @name) end def add(name) do GenServer.cast(@name, {:add, name}) end def remove(name) do GenServer.cast(@name, {:remove, name}) end def team do GenServer.call(@name, :team) end # Callbacks ... defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  63. defmodule FantasyTeam.SingleServer do use GenServer @name __MODULE__ # API def

    start_link do GenServer.start_link(__MODULE__, :ok, name: @name) end def add(name) do GenServer.cast(@name, {:add, name}) end def remove(name) do GenServer.cast(@name, {:remove, name}) end def team do GenServer.call(@name, :team) end # Callbacks ... defmodule FantasyTeam.MyGenServer do use GenServer # API def start_link do GenServer.start_link(__MODULE__, :ok, []) end def add(pid, name) do GenServer.cast(pid, {:add, name}) end def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  64. iex(1)> FantasyTeam.SingleServer.start_link {:ok, #PID<0.65.0>} iex(2)> FantasyTeam.SingleServer.add(“Russell Wilson”) :ok iex(3)> FantasyTeam.SingleServer.add(“Doug

    Baldwin”) :ok iex(4)> FantasyTeam.SingleServer.remove(“Doug Baldwin”) :ok iex(5)> FantasyTeam.SingleServer.team %{“Russell Wilson” => %{position: “QB”, team: “SEA”, ...}} iex(6)>
  65. defmodule FantasyTeam.MyAgent do @name __MODULE__ def start_link do Agent.start_link(fn ->

    %{} end) end def add(name) do player = FantasyTeam.Player.find(name) Agent.update(@name, fn(x) -> Map.put(x, name, player)} end) end def remove(name) do Agent.update(@name, fn(x) -> Map.delete(x, name) end) end def team do Agent.get(@name, fn(x) -> x end) end end
  66. defmodule FantasyTeam.MyAgent do @name __MODULE__ def start_link do Agent.start_link(fn ->

    %{} end) end def add(name) do player = FantasyTeam.Player.find(name) Agent.update(@name, fn(x) -> Map.put(x, name, player)} end) end def remove(name) do Agent.update(@name, fn(x) -> Map.delete(x, name) end) end def team do Agent.get(@name, fn(x) -> x end) end end
  67. defmodule FantasyTeam.MySupervisor do use Supervisor def start_link do Supervisor.start_link(__MODULE__, [])

    end def init([]) do children = [ worker(FantasyTeam.SingleServer, []) ] supervise(children, strategy: :one_for_one) end end
  68. defmodule FantasyTeam.MySupervisor do use Supervisor def start_link do Supervisor.start_link(__MODULE__, [])

    end def init([]) do children = [ worker(FantasyTeam.SingleServer, []) ] supervise(children, strategy: :one_for_one) end end
  69. defmodule FantasyTeam.MySupervisor do use Supervisor def start_link do Supervisor.start_link(__MODULE__, [])

    end def init([]) do children = [ worker(FantasyTeam.SingleServer, []) ] supervise(children, strategy: :one_for_one) end end
  70. defmodule FantasyTeam.MySupervisor do use Supervisor def start_link do Supervisor.start_link(__MODULE__, [])

    end def init([]) do children = [ worker(FantasyTeam.SingleServer, []) ] supervise(children, strategy: :one_for_one) end end
  71. defmodule FantasyTeam.MySupervisor do use Supervisor def start_link do Supervisor.start_link(__MODULE__, [])

    end def init([]) do children = [ worker(FantasyTeam.SingleServer, []) ] supervise(children, strategy: :one_for_one) end end
  72. defmodule FantasyTeam.MySupervisor do use Supervisor def start_link do Supervisor.start_link(__MODULE__, [])

    end def init([]) do children = [ worker(FantasyTeam.SingleServer, []) ] supervise(children, strategy: :one_for_one) end end
  73. defmodule FantasyTeam.MySupervisor do use Supervisor def start_link do Supervisor.start_link(__MODULE__, [])

    end def init([]) do children = [ worker(FantasyTeam.SingleServer, []) ] supervise(children, strategy: :one_for_one) end end
  74. defmodule FantasyTeam.MySupervisor do use Supervisor def start_link do Supervisor.start_link(__MODULE__, [])

    end def init([]) do children = [ worker(FantasyTeam.SingleServer, []) ] supervise(children, strategy: :one_for_one) end end
  75. defmodule FantasyTeam.MySupervisor do use Supervisor def start_link do Supervisor.start_link(__MODULE__, [])

    end def init([]) do children = [ worker(FantasyTeam.SingleServer, []) ] supervise(children, strategy: :one_for_one) end end
  76. defmodule FantasyTeam.MySupervisor do use Supervisor def start_link do Supervisor.start_link(__MODULE__, [])

    end def init([]) do children = [ worker(FantasyTeam.SingleServer, []) ] supervise(children, strategy: :one_for_one) end end
  77. defmodule FantasyTeam.MySupervisor do use Supervisor def start_link do Supervisor.start_link(__MODULE__, [])

    end def init([]) do children = [ worker(FantasyTeam.SingleServer, []) ] supervise(children, strategy: :one_for_one) end end
  78. defmodule FantasyTeam.MySupervisor do use Supervisor def start_link do Supervisor.start_link(__MODULE__, [])

    end def init([]) do children = [ worker(FantasyTeam.SingleServer, []) ] supervise(children, strategy: :one_for_one) end end
  79. defmodule FantasyTeam.MySupervisor do use Supervisor def start_link do Supervisor.start_link(__MODULE__, [])

    end def init([]) do children = [ worker(FantasyTeam.SingleServer, []) ] supervise(children, strategy: :one_for_one) end end
  80. r