hunar

hunar

How to programatically name agents?

Hi I’m an Elixir beginner,

I have been experimenting with agents, basically I was able to create several agents, naming each with unique atoms and changing their internal states. However, now I’m trying to programmatically name the created agents as this would be useful if I were trying to create 1000 agents.
Initially my intention was to use something like Enum.range(1…1000) to creaete 1000 agents and then programatically assign the name of each Agent with the corresponding number so that I can call each Agent by their number: Agent.get(23, fn(x)-> x+1 end), for example to increment agent number 23.
I realised that Elixir doesn’t allow agents to be named by number!

Can someone please tell me how can I do the above? or if there is an easier way to programatically name 1000 agents and retrieve them? I don’t mind if this was doable by using atoms or even pids as well.

Thanks a lot in advance
Hunar

Most Liked

mariosangiorgio

mariosangiorgio

You could use a Registry and give explicit names to your agents.

Something like this will create you 1000 Agents, giving them an unique name.

Registry.start_link(keys: :unique, name: MyAgents)
1..1000
|> Enum.map(fn i -> Agent.start_link(fn -> 0 end, name: {:via, Registry, {MyAgents, i}}) end)

As explained in the documentation, you ca use the name to interact with them:

Agent.get({:via, Registry, {MyAgents, 1}}, & &1)
Agent.update({:via, Registry, {MyAgents, 1}}, & &1 + 1)
Agent.get({:via, Registry, {MyAgents, 1}}, & &1)

If you want a more in-depth explanation, I found this post to be quite good.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement