Hi, I’m learning Elixir so for the moment keep in mind I have no idea what I’m talking about.
With that caveat out of the way…
I want to create a code example where a “user” is modeled as a process and each process has some data associated with it. The code I came up with is below. The problem I am having is that each Agent’s custom name is an atom. Because its an atom, it can not be dynamically created. Ideally I wanted to connect my code to a web server and when a user logs in they in effect “are” a dynamically created Agent. From the look of things, it seems this entire outlook is a complete dead end. Below is my code and I am curious if anyone has any advice for how I should be looking at this problem to set me straight. Thanks a bunch
Keep in mind these are just exercises for me to learn Elixir.
#____________________________________________________________BEGIN Module
defmodule App do
def makeUser(name) do
{:ok, item} = Agent.start_link fn -> [] end
pid = elem({:ok, item}, 1)
Process.register(pid, name)
end
end
#____________________________________________________________END module
App.makeUser(:Bob) #:::: Set new user
#____________________________________________________________BEGIN update data examples
Agent.update(:Bob, fn list -> [%{thing: "stuff"} | list] end)
Agent.update(:Bob, fn list -> ["capture the flag" | list] end)
#____________________________________________________________END update data examples
IO.inspect Agent.get(:Bob, fn list -> list end)
IO.inspect Process.whereis(:Bob) # use name to get PID
IO.inspect Process.registered # find all registered processes