Hello people of this beautiful language
I have recently been trying to learn about spawning of processes, sending and receiving messages and so on.
I came across this article
which describes processes but the article uses dictionaries which I have tried to research on but to no avail
‘’’
defmodule Anagrammar do
@dictionary "/usr/share/dict/words"
def build_list(accumulator_pid) do
words
|> Enum.each(&(add_anagram(accumulator_pid, &1)))
end
def get_list(accumulator_pid) do
send(accumulator_pid, {self, :list})
receive do
{:ok, list} ->
list
|> Enum.each(&IO.inspect/1)
end
end
defp words do
File.read!(@dictionary)
|> String.split("\n")
end
defp add_anagram(accumulator_pid, word) do
spawn(fn -> _add_anagram(accumulator_pid, word) end)
end
defp _add_anagram(accumulator_pid, word) do
send(accumulator_pid, {self, {:add, parse(word)}})
receive do
:ok -> :ok
end
end
defp parse(word) do
letters =
word
|> String.downcase()
|> String.split("")
|> Enum.sort(&(&1 <= &2))
|> Enum.join()
{letters, word}
end
end
If anyone happens to undersatnd them, please reply
And if you happen to have some study material on processes with examples (apart form the docs)
share also.
Thank you in advance.
Process dictionary is an implicit state that you don’t want to pass around as function arguments… BUT it’s only there for a singular OTP process (not to be mistaken by an OS process, hopefully you are aware of Erlang’s terminology at this point).
It’s used for configurations like logger handlers, OpenTelemetry span IDs, and many other such stateful info that people would be annoyed to have to constantly pass around.
Obviously it’s not something you want to abuse though, have that in mind. Process dictionaries are an admission that we live in a non-perfect world and sometimes we have to compromise but they are NOT an everyday coding tool.
i now understand GenServer for the web …Thank you and if you have any Repo in which you’ve implemented GenServer especially for recurring tasks, you can send the link