39 lines
701 B
Elixir
39 lines
701 B
Elixir
defmodule Stack do
|
|
use GenServer
|
|
|
|
# Client
|
|
|
|
def start_link(default) when is_binary(default) do
|
|
GenServer.start_link(__MODULE__, default)
|
|
end
|
|
|
|
def push(pid, element) do
|
|
GenServer.cast(pid, {:push, element})
|
|
end
|
|
|
|
def pop(pid) do
|
|
GenServer.call(pid, :pop)
|
|
end
|
|
|
|
# Server (callbacks)
|
|
|
|
@impl true
|
|
def init(elements) do
|
|
initial_state = String.split(elements, ",", trim: true)
|
|
{:ok, initial_state}
|
|
end
|
|
|
|
@impl true
|
|
def handle_call(:pop, _from, state) do
|
|
[to_caller | new_state] = state
|
|
{:reply, to_caller, new_state}
|
|
end
|
|
|
|
@impl true
|
|
def handle_cast({:push, element}, state) do
|
|
new_state = [element | state]
|
|
{:noreply, new_state}
|
|
end
|
|
end
|
|
|