Skip to content

Instantly share code, notes, and snippets.

@sofakingworld
Last active March 19, 2019 23:58
Show Gist options
  • Save sofakingworld/668f726edb64cc41d2bae14d43afdfc0 to your computer and use it in GitHub Desktop.
Save sofakingworld/668f726edb64cc41d2bae14d43afdfc0 to your computer and use it in GitHub Desktop.
Stack#2
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