Skip to content

Instantly share code, notes, and snippets.

@edubkendo
Created September 26, 2014 03:37
Show Gist options
  • Save edubkendo/2affb87911916e96d598 to your computer and use it in GitHub Desktop.
Save edubkendo/2affb87911916e96d598 to your computer and use it in GitHub Desktop.
Cooler elixir stack: Now with push
defmodule Stack.Server do
use GenServer
def handle_call(:pop, _from, stack) do
[ head | tail] = stack
{ :reply, head, tail }
end
def handle_cast({:push, data_to_push}, stack) do
{ :noreply, [ data_to_push | stack ] }
end
end
@edubkendo
Copy link
Author

iex(35)> { :ok, pid } = GenServer.start_link(Stack.Server, [5, "cat", 9])
{:ok, #PID<0.146.0>}
iex(36)> clear
iex(37)> GenServer.call(pid, :pop)
5
iex(38)> GenServer.cast(pid, {:push, "dog"})
:ok
iex(39)> GenServer.call(pid, :pop)
"dog"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment