Last active
March 19, 2019 23:58
-
-
Save sofakingworld/668f726edb64cc41d2bae14d43afdfc0 to your computer and use it in GitHub Desktop.
Stack#2
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
defmodule Stack do | |
use GenServer | |
# Client | |
def start_link(default) when is_list(default) do | |
GenServer.start_link(__MODULE__, default) | |
end | |
def push(pid, item) do | |
GenServer.call(pid, {:push, [item]}) | |
end | |
def pop(pid) do | |
GenServer.call(pid, {:pop, []}) | |
end | |
# Server (callback) | |
def handle_call({function_name, arguments}, from, state) do | |
# Применяем функцию на нужном модуле | |
# и используем результат в качестве ответа и обновленного состояния | |
{reply, new_state} = apply(StackImp, function_name, [state] ++ arguments) | |
{:reply, reply, new_state} | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment