Skip to content

Instantly share code, notes, and snippets.

@aflores
Created August 22, 2017 21:26
Show Gist options
  • Save aflores/50aff06d6b703cf530bf81efac04c3c8 to your computer and use it in GitHub Desktop.
Save aflores/50aff06d6b703cf530bf81efac04c3c8 to your computer and use it in GitHub Desktop.
Elixir Agent example
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