Skip to content

Instantly share code, notes, and snippets.

@arcanemachine
Last active July 25, 2025 09:17
Show Gist options
  • Select an option

  • Save arcanemachine/ef70093765e38990dc0b062d7c266348 to your computer and use it in GitHub Desktop.

Select an option

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, agent_pid} = Agent.start_link(fn -> 0 end)
Process.register(agent_pid, 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