Skip to content

Instantly share code, notes, and snippets.

@arcanemachine
Last active March 20, 2025 19:41
Show Gist options
  • Save arcanemachine/ef70093765e38990dc0b062d7c266348 to your computer and use it in GitHub Desktop.
Save arcanemachine/ef70093765e38990dc0b062d7c266348 to your computer and use it in GitHub Desktop.
Elixir - Simple agent-based counter
# Put this code inside a function to easily count the number of times the
# function is called.
# Create the agent
agent_process_name = :your_agent
if Process.whereis(agent_process_name) == nil do
{:ok, a} = Agent.start_link(fn -> 0 end)
Process.register(a, agent_process_name)
end
# Increment the counter
Agent.update(agent_process_name, fn state -> state + 1 end)
# To get the count, run: Agent.get(_your_agent_process_name = :your_agent, & &1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment