Created
August 22, 2017 21:26
-
-
Save aflores/50aff06d6b703cf530bf81efac04c3c8 to your computer and use it in GitHub Desktop.
Elixir Agent example
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 HitCountAgent do | |
use Agent | |
@docmodule """ | |
This is the agen based implementation of a counter | |
""" | |
@doc """ | |
hitPid = HitCountAgent.start() | |
HitCountAgent.record_hit(hitPid) | |
HitCountAgent.get_count(hitPid) | |
""" | |
def start() do | |
Agent.start_link(fn -> 0 end) | |
end | |
def record_hit(agent) do | |
Agent.update(agent, &(&1+1)) | |
end | |
def get_count(agent) do | |
Agent.get(agent,&(&1)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment