Created
May 10, 2019 09:43
-
-
Save zkessin/f445fe363d4c5536232efd4a76d0a3c9 to your computer and use it in GitHub Desktop.
This file contains 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 Counter do | |
use GenServer | |
def increment() do | |
GenServer.cast(__MODULE__, :increment) | |
end | |
def decrement() do | |
GenServer.cast(__MODULE__, :decrement) | |
end | |
def value() do | |
GenServer.call(__MODULE__, :value) | |
end | |
def start_link(initial_value \\ 0) do | |
GenServer.start_link(__MODULE__, initial_value, name: __MODULE__) | |
end | |
def init(initial_value) do | |
{:ok, initial_value} | |
end | |
def handle_cast(:increment, val) do | |
{:noreply, val + 1} | |
end | |
def handle_cast(:decrement, val) do | |
{:noreply, val - 1} | |
end | |
def handle_call(:value, _from, val) do | |
{:reply, val, val} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment