Elixir Enum.group_by converts integers to ASCII Chars

The following code returns

%{“file1” => ~c"\t", “file2” => ~c"\v"}

Why do number 9 and 11 get converted to the corresponding ASCII code ~c"\t" and ~c"\v"? And how can avoid that conversion? (leaving 9 and 11 in the output?

diagnostics = [
  %{file: "file1", position: 9},
  %{file: "file2", position: 11}
]

diagnostics
|> Enum.group_by(&Map.get(&1, :file), &Map.get(&1, :position))

It’s only a visual aid, the numbers are exactly the same underneath.

There is also an option for iex to always show numbers and never attempt to show them as printable characters. Make an .iex.exs file at the root of your project and put the IEx.configure line from below in it.

Dynamic demo (changing the config of an already running iex session, though IMO you should opt for the .iex.exs file approach):

iex(1)> [9, 11]
~c"\t\v"
iex(2)> IEx.configure(inspect: [charlists: :as_lists])
:ok
iex(3)> [9, 11]
[9, 11]
3 Likes

I think we have a new record - this issue being raised twice within 12 hours!

Elixir converting [55] into '7' (one of the responses on this thread provides excellent background & context into this weird & wonderful behaviour)

Don’t worry @jason.o - this is a rite of passage into Elixir. It gets everyone sooner or later!

4 Likes