Strange Enum.map behavior

How come

Enum.map([{:ok, nil}], fn x -> x end)

returns [:ok nil] instead of [{:ok, nil}] I thought it might be that it was treating the input as keyword args so I tried:

Enum.map([{:ok, nil}], fn {x, y} -> {x, y} end)

but that returns [:ok, nil] too. Am I missing something?

And

Enum.map([{:ok, nil}, {:ok, nil}], fn x -> x end)

returns: [ok: nil, ok: nil]

I could work around this using Enum.chunk_every but I am not sure why it is needed.

[{:ok, nil}] == [ok: nil] - lists of tuples of two elements where the first is an atom are keyword lists, there’s no way to distinguish one from the other. The printer always prints something that might be a keyword list as a keyword list - the data underneath is always the same, just a different way of writing it.

2 Likes

Lol! You’re right. Wow, do I feel silly. :slight_smile: