SlideShare a Scribd company logo
1 of 82
Download to read offline
OTP
• Erlang
• Tools
• Libraries
• Design Principles
• Other Stuff
Processes
iex(1)>
iex(1)> spawn(fn -> IO.puts “Hello Full Stack” end)
iex(1)> spawn(fn -> IO.puts “Hello Full Stack” end)
Hello Full Stack
#PID<0.64.0>
iex(2)>
iex(2)> pid = spawn(fn -> IO.puts “Hello Full Stack” end)
Hello Full Stack
#PID<0.65.0>
iex(3)> Process.alive?(pid)
false
iex(4)>
Error Handling
Supervisors
“The Zen of Erlang”
Messages
FantasyTeam
State
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
defmodule FantasyTeam.Basic do
def start_link do
spawn_link(__MODULE__, :loop, [%{}])
end
def loop(state) do
...
defmodule FantasyTeam.Basic do
def start_link do
spawn_link(__MODULE__, :loop, [%{}])
end
def loop(state) do
...
defmodule FantasyTeam.Basic do
def start_link do
spawn_link(__MODULE__, :loop, [%{}])
end
def loop(state) do
...
defmodule FantasyTeam.Basic do
def start_link do
spawn_link(__MODULE__, :loop, [%{}])
end
def loop(state) do
...
defmodule FantasyTeam.Basic do
def start_link do
spawn_link(__MODULE__, :loop, [%{}])
end
def loop(state) do
...
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(new_state)
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(new_state)
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(new_state)
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(new_state)
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(new_state)
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)
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)
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)
iex(1)>
iex(1)> pid = FantasyTeam.Basic.start_link
#PID<0.65.0>
iex(2)>
iex(1)> pid = FantasyTeam.Basic.start_link
#PID<0.65.0>
iex(2)> send(pid, {:add, “Russell Wilson”})
{:add, “Russell Wilson”}
iex(3)>
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)>
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)>
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)>
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”}}
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
GenServer
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
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
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
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
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
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
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
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
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
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
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
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
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})
def
def remove(pid, name) do
GenServer.cast(pid, {:remove, name})
end
def team(pid) do
GenServer.call(pid, :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})
def
def remove(pid, name) do
GenServer.cast(pid, {:remove, name})
end
def team(pid) do
GenServer.call(pid, :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})
def
def remove(pid, name) do
GenServer.cast(pid, {:remove, name})
end
def team(pid) do
GenServer.call(pid, :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})
def
def remove(pid, name) do
GenServer.cast(pid, {:remove, name})
end
def team(pid) do
GenServer.call(pid, :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})
def
def remove(pid, name) do
GenServer.cast(pid, {:remove, name})
end
def team(pid) do
GenServer.call(pid, :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})
def
def remove(pid, name) do
GenServer.cast(pid, {:remove, name})
end
def team(pid) do
GenServer.call(pid, :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})
def
def remove(pid, name) do
GenServer.cast(pid, {:remove, name})
end
def team(pid) do
GenServer.call(pid, :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})
def
def remove(pid, name) do
GenServer.cast(pid, {:remove, name})
end
def team(pid) do
GenServer.call(pid, :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})
def
def remove(pid, name) do
GenServer.cast(pid, {:remove, name})
end
def team(pid) do
GenServer.call(pid, :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})
def
def remove(pid, name) do
GenServer.cast(pid, {:remove, name})
end
def team(pid) do
GenServer.call(pid, :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})
def
def remove(pid, name) do
GenServer.cast(pid, {:remove, name})
end
def team(pid) do
GenServer.call(pid, :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})
def
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.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})
def
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})
def
def remove(pid, name) do
GenServer.cast(pid, {:remove, name})
end
def team(pid) do
GenServer.call(pid, :team)
end
# Callbacks
...
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})
def
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})
def
def remove(pid, name) do
GenServer.cast(pid, {:remove, name})
end
def team(pid) do
GenServer.call(pid, :team)
end
# Callbacks
...
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})
def
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})
def
def remove(pid, name) do
GenServer.cast(pid, {:remove, name})
end
def team(pid) do
GenServer.call(pid, :team)
end
# Callbacks
...
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})
def
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})
def
def remove(pid, name) do
GenServer.cast(pid, {:remove, name})
end
def team(pid) do
GenServer.call(pid, :team)
end
# Callbacks
...
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})
def
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})
def
def remove(pid, name) do
GenServer.cast(pid, {:remove, name})
end
def team(pid) do
GenServer.call(pid, :team)
end
# Callbacks
...
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})
def
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})
def
def remove(pid, name) do
GenServer.cast(pid, {:remove, name})
end
def team(pid) do
GenServer.call(pid, :team)
end
# Callbacks
...
iex(1)>
iex(1)> FantasyTeam.SingleServer.start_link
{:ok, #PID<0.65.0>}
iex(2)>
iex(1)> FantasyTeam.SingleServer.start_link
{:ok, #PID<0.65.0>}
iex(2)> FantasyTeam.SingleServer.add(“Russell Wilson”)
:ok
iex(3)>
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)>
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)>
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)>
Recap
Agents & Tasks
defmodule FantasyTeam.MyAgent do
def start_link do
Agent.start_link(fn -> %{} end)
end
def add(pid, name) do
player = FantasyTeam.Player.find(name)
Agent.get_and_update(pid, fn(x) -> {player, Map.put(x, name, player)} end)
:ok
end
def remove(pid, name) do
Agent.update(pid, fn(x) -> Map.delete(x, name) end)
end
def team(pid) do
Agent.get(pid, fn(x) -> x end)
end
end
bit.ly/fullstackotp
OTP / ELIXIR RESOURCES
@jessejanderson
FOLLOW ME

More Related Content

What's hot

groovy databases
groovy databasesgroovy databases
groovy databasesPaul King
 
The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212Mahmoud Samir Fayed
 
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)MongoSF
 
Gabriele Lana - The Magic of Elixir
Gabriele Lana - The Magic of ElixirGabriele Lana - The Magic of Elixir
Gabriele Lana - The Magic of ElixirCodemotion
 
Pre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPaweł Dawczak
 
Using New Web APIs For Your Own Pleasure – How I Wrote New Features For My Vi...
Using New Web APIs For Your Own Pleasure – How I Wrote New Features For My Vi...Using New Web APIs For Your Own Pleasure – How I Wrote New Features For My Vi...
Using New Web APIs For Your Own Pleasure – How I Wrote New Features For My Vi...GeilDanke
 
Clustering com numpy e cython
Clustering com numpy e cythonClustering com numpy e cython
Clustering com numpy e cythonAnderson Dantas
 
Palestra sobre Collections com Python
Palestra sobre Collections com PythonPalestra sobre Collections com Python
Palestra sobre Collections com Pythonpugpe
 
The Ring programming language version 1.10 book - Part 56 of 212
The Ring programming language version 1.10 book - Part 56 of 212The Ring programming language version 1.10 book - Part 56 of 212
The Ring programming language version 1.10 book - Part 56 of 212Mahmoud Samir Fayed
 
Elixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicitElixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicitTobias Pfeiffer
 
Elixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicitElixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicitTobias Pfeiffer
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2Raghu nath
 
python chapter 1
python chapter 1python chapter 1
python chapter 1Raghu nath
 
Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeBuilding Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeMongoDB
 

What's hot (19)

groovy databases
groovy databasesgroovy databases
groovy databases
 
The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212The Ring programming language version 1.10 book - Part 81 of 212
The Ring programming language version 1.10 book - Part 81 of 212
 
Corona sdk
Corona sdkCorona sdk
Corona sdk
 
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
 
Gabriele Lana - The Magic of Elixir
Gabriele Lana - The Magic of ElixirGabriele Lana - The Magic of Elixir
Gabriele Lana - The Magic of Elixir
 
Pre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to Elixir
 
Pdxpugday2010 pg90
Pdxpugday2010 pg90Pdxpugday2010 pg90
Pdxpugday2010 pg90
 
Using New Web APIs For Your Own Pleasure – How I Wrote New Features For My Vi...
Using New Web APIs For Your Own Pleasure – How I Wrote New Features For My Vi...Using New Web APIs For Your Own Pleasure – How I Wrote New Features For My Vi...
Using New Web APIs For Your Own Pleasure – How I Wrote New Features For My Vi...
 
Clustering com numpy e cython
Clustering com numpy e cythonClustering com numpy e cython
Clustering com numpy e cython
 
Palestra sobre Collections com Python
Palestra sobre Collections com PythonPalestra sobre Collections com Python
Palestra sobre Collections com Python
 
The Ring programming language version 1.10 book - Part 56 of 212
The Ring programming language version 1.10 book - Part 56 of 212The Ring programming language version 1.10 book - Part 56 of 212
The Ring programming language version 1.10 book - Part 56 of 212
 
Lập trình Python cơ bản
Lập trình Python cơ bảnLập trình Python cơ bản
Lập trình Python cơ bản
 
Elixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicitElixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicit
 
Elixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicitElixir & Phoenix – fast, concurrent and explicit
Elixir & Phoenix – fast, concurrent and explicit
 
FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...
FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...
FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...
 
Introducing to Asynchronous Programming
Introducing to Asynchronous  ProgrammingIntroducing to Asynchronous  Programming
Introducing to Asynchronous Programming
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
 
python chapter 1
python chapter 1python chapter 1
python chapter 1
 
Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeBuilding Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at Stripe
 

Similar to Intro to OTP in Elixir

関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)riue
 
GenServer in Action – Yurii Bodarev
GenServer in Action – Yurii Bodarev   GenServer in Action – Yurii Bodarev
GenServer in Action – Yurii Bodarev Elixir Club
 
Elixir -Tolerância a Falhas para Adultos - GDG Campinas
Elixir  -Tolerância a Falhas para Adultos - GDG CampinasElixir  -Tolerância a Falhas para Adultos - GDG Campinas
Elixir -Tolerância a Falhas para Adultos - GDG CampinasFabio Akita
 
The Ring programming language version 1.5.2 book - Part 66 of 181
The Ring programming language version 1.5.2 book - Part 66 of 181The Ring programming language version 1.5.2 book - Part 66 of 181
The Ring programming language version 1.5.2 book - Part 66 of 181Mahmoud Samir Fayed
 
(first '(Clojure.))
(first '(Clojure.))(first '(Clojure.))
(first '(Clojure.))niklal
 
ES6 patterns in the wild
ES6 patterns in the wildES6 patterns in the wild
ES6 patterns in the wildJoe Morgan
 
Selective codes
Selective codesSelective codes
Selective codesSiva Gopal
 
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기Swift 함수 커링 사용하기
Swift 함수 커링 사용하기진성 오
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語ikdysfm
 
API Python Chess: Distribution of Chess Wins based on random moves
API Python Chess: Distribution of Chess Wins based on random movesAPI Python Chess: Distribution of Chess Wins based on random moves
API Python Chess: Distribution of Chess Wins based on random movesYao Yao
 
The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189Mahmoud Samir Fayed
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingMuthu Vinayagam
 
7 Python udf.pptx
7 Python udf.pptx7 Python udf.pptx
7 Python udf.pptxSUJALORAON
 
Reactive programming with RxJS - ByteConf 2018
Reactive programming with RxJS - ByteConf 2018Reactive programming with RxJS - ByteConf 2018
Reactive programming with RxJS - ByteConf 2018Tracy Lee
 

Similar to Intro to OTP in Elixir (20)

関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
 
GenServer in action
GenServer in actionGenServer in action
GenServer in action
 
GenServer in Action – Yurii Bodarev
GenServer in Action – Yurii Bodarev   GenServer in Action – Yurii Bodarev
GenServer in Action – Yurii Bodarev
 
Elixir -Tolerância a Falhas para Adultos - GDG Campinas
Elixir  -Tolerância a Falhas para Adultos - GDG CampinasElixir  -Tolerância a Falhas para Adultos - GDG Campinas
Elixir -Tolerância a Falhas para Adultos - GDG Campinas
 
The Ring programming language version 1.5.2 book - Part 66 of 181
The Ring programming language version 1.5.2 book - Part 66 of 181The Ring programming language version 1.5.2 book - Part 66 of 181
The Ring programming language version 1.5.2 book - Part 66 of 181
 
(first '(Clojure.))
(first '(Clojure.))(first '(Clojure.))
(first '(Clojure.))
 
ES6 patterns in the wild
ES6 patterns in the wildES6 patterns in the wild
ES6 patterns in the wild
 
Javascript
JavascriptJavascript
Javascript
 
Selective codes
Selective codesSelective codes
Selective codes
 
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
 
ES6(ES2015) is beautiful
ES6(ES2015) is beautifulES6(ES2015) is beautiful
ES6(ES2015) is beautiful
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
PythonOOP
PythonOOPPythonOOP
PythonOOP
 
API Python Chess: Distribution of Chess Wins based on random moves
API Python Chess: Distribution of Chess Wins based on random movesAPI Python Chess: Distribution of Chess Wins based on random moves
API Python Chess: Distribution of Chess Wins based on random moves
 
Basics
BasicsBasics
Basics
 
Elm: give it a try
Elm: give it a tryElm: give it a try
Elm: give it a try
 
The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
7 Python udf.pptx
7 Python udf.pptx7 Python udf.pptx
7 Python udf.pptx
 
Reactive programming with RxJS - ByteConf 2018
Reactive programming with RxJS - ByteConf 2018Reactive programming with RxJS - ByteConf 2018
Reactive programming with RxJS - ByteConf 2018
 

Recently uploaded

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 

Recently uploaded (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

Intro to OTP in Elixir

  • 1.
  • 2.
  • 3. OTP • Erlang • Tools • Libraries • Design Principles • Other Stuff
  • 6. iex(1)> spawn(fn -> IO.puts “Hello Full Stack” end)
  • 7. iex(1)> spawn(fn -> IO.puts “Hello Full Stack” end) Hello Full Stack #PID<0.64.0> iex(2)>
  • 8. iex(2)> pid = spawn(fn -> IO.puts “Hello Full Stack” end) Hello Full Stack #PID<0.65.0> iex(3)> Process.alive?(pid) false iex(4)>
  • 11.
  • 12. “The Zen of Erlang”
  • 13.
  • 14.
  • 15.
  • 16.
  • 19. State
  • 20. 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
  • 21. defmodule FantasyTeam.Basic do def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do ...
  • 22. defmodule FantasyTeam.Basic do def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do ...
  • 23. defmodule FantasyTeam.Basic do def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do ...
  • 24. defmodule FantasyTeam.Basic do def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do ...
  • 25. defmodule FantasyTeam.Basic do def start_link do spawn_link(__MODULE__, :loop, [%{}]) end def loop(state) do ...
  • 26. 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(new_state)
  • 27. 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(new_state)
  • 28. 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(new_state)
  • 29. 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(new_state)
  • 30. 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(new_state)
  • 31. 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)
  • 32. 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)
  • 33. 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)
  • 35. iex(1)> pid = FantasyTeam.Basic.start_link #PID<0.65.0> iex(2)>
  • 36. iex(1)> pid = FantasyTeam.Basic.start_link #PID<0.65.0> iex(2)> send(pid, {:add, “Russell Wilson”}) {:add, “Russell Wilson”} iex(3)>
  • 37. 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)>
  • 38. 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)>
  • 39. 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)>
  • 40. 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”}}
  • 41. 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
  • 43. 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
  • 44. 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
  • 45. 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
  • 46. 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
  • 47. 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
  • 48. 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
  • 49. 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
  • 50. 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
  • 51. 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
  • 52. 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
  • 53. 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
  • 54. 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
  • 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}) def 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}) def 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}) def def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  • 58. 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}) def def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  • 59. 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}) def def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  • 60. 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}) def def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  • 61. 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}) def def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  • 62. 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}) def def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  • 63. 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}) def def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  • 64. 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}) def def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  • 65. 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}) def def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  • 66. 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}) def 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
  • 67. 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}) def 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}) def def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  • 68. 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}) def 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}) def def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  • 69. 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}) def 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}) def def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  • 70. 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}) def 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}) def def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  • 71. 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}) def 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}) def def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  • 72. 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}) def 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}) def def remove(pid, name) do GenServer.cast(pid, {:remove, name}) end def team(pid) do GenServer.call(pid, :team) end # Callbacks ...
  • 75. iex(1)> FantasyTeam.SingleServer.start_link {:ok, #PID<0.65.0>} iex(2)> FantasyTeam.SingleServer.add(“Russell Wilson”) :ok iex(3)>
  • 76. 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)>
  • 77. 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)>
  • 78. 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)>
  • 79. Recap
  • 81. defmodule FantasyTeam.MyAgent do def start_link do Agent.start_link(fn -> %{} end) end def add(pid, name) do player = FantasyTeam.Player.find(name) Agent.get_and_update(pid, fn(x) -> {player, Map.put(x, name, player)} end) :ok end def remove(pid, name) do Agent.update(pid, fn(x) -> Map.delete(x, name) end) end def team(pid) do Agent.get(pid, fn(x) -> x end) end end
  • 82. bit.ly/fullstackotp OTP / ELIXIR RESOURCES @jessejanderson FOLLOW ME