What Exactly are Dictionaries in Elixir, are they deprecated, and what was their use if so

Hello people of this beautiful language :smiling_face_with_three_hearts:
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 :blush:
And if you happen to have some study material on processes with examples (apart form the docs)
share also.
Thank you in advance.

The @dictionary is a module attribute (a compile-time constant available in the module Anagrammar) and not a primitive.

In your example it is a file path "/usr/share/dict/words" for a text file with words separated by newlines.

2 Likes

There is a Dict module… but it is deprecated in favor of Maps or Keywords

https://hexdocs.pm/elixir/Dict.html

And there is process dictionary… but it’s something different

2 Likes

ok Could you explain the process dictionary?

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 :smiley: but they are NOT an everyday coding tool.

Docs:

4 Likes

thanks

i now understand GenServer for the web :blush:…Thank you and if you have any Repo in which you’ve implemented GenServer especially for recurring tasks, you can send the link

That should be a separate forum topic IMO.

1 Like

Okay :white_check_mark: