Prevent Enum.map from converting to Keyword-List

Enum.map converts results with 2-item-tuples where the first item is an atom to a keyword-list. This has it’s use cases, but when you want to keep the tuples, what is the idiomatic way to achieve this?

For example:
[1, 2, 3] |> Enum.map(& if &1 < 2, do: {:ok, "< 2"}, else: {:errror, "not < 2"})

returns

  ok: "< 2",
  errror: "not < 2",
  errror: "not < 2"
]

But I’d like to get this:

 {:ok, "< 2"},
 {:errror, "not < 2"},
 {:errror, "not < 2"}
]
1 Like

They are the same thing!

https://hexdocs.pm/elixir/1.12/Keyword.html#module-examples

2 Likes

If you scroll a little bit up from the permalink shared by @lubien you will see the definition of a keyword list:

A keyword list is a list that consists exclusively of two-element tuples.

To hit it home: a keyword list is literally just a list like any other, the only thing “special” about is its very specific contents.

They are exactly the same, the former is just syntactic sugar.

Also you might want to correct “errror” (3 “r”-s) to “error” (2 “r”-s).

Oh yes, the docs are pretty clear at that.

Thnak you all for the quick repies!

1 Like