Skip to content

Instantly share code, notes, and snippets.

@blackode
Last active March 28, 2018 21:04
Show Gist options
  • Save blackode/ba7bf19c225848ba139c2d0f8042d71c to your computer and use it in GitHub Desktop.
Save blackode/ba7bf19c225848ba139c2d0f8042d71c to your computer and use it in GitHub Desktop.
Stack Implementation Using Genserver
# stack.ex
defmodule Stack do
use GenServer
# server callbacks
def init(args), do: {:ok, args}
#used to handle synchronous requests
def handle_call(:get, _from, state) do
{:reply, state, state}
end
#used to handle asynchronous requests
def handle_cast(:pop, state) do
{_popelement, new_state} = List.pop_at(state, -1)
{:noreply, new_state}
end
def handle_cast({:put, new_elem}, state) do
{:noreply, state ++ [new_elem]}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment