Last active
March 20, 2025 19:41
-
-
Save arcanemachine/ef70093765e38990dc0b062d7c266348 to your computer and use it in GitHub Desktop.
Elixir - Simple agent-based counter
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
# 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