Last active
March 28, 2018 21:04
-
-
Save blackode/ba7bf19c225848ba139c2d0f8042d71c to your computer and use it in GitHub Desktop.
Stack Implementation Using Genserver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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