I was wondering why Enum.map returns a list and not the enumerable type provided. eg:
iex> Enum.map(%{a: 1, b: 2, c: 3}, fn x -> x end)
[a: 1, b: 2, c: 3]
Wheres I would expect:
iex> Enum.map(%{a: 1, b: 2, c: 3}, fn x -> x end)
%{a: 1, b: 2, c: 3}
For me this seems un intuitive. The only thing I can think of is that this might not be a problem in elixir due to a preference for lists over other enumerable types?
I’m very new to elixir and BEAM architecture in general so sorry if this is a naive or redundant question.
Using a map was just an example. I’m more interested in the case with a general data structure that implements the Enumerable protocol. I guess what I’m interested in is why the Enum module was designed like this, it is different to how other programming languages I’ve seen behave.
I guess I probably change Enum.map to preserve the keys and just act on the values. Other approaches could be to just return a list or throw an error.
I’m not entirely sure how a Map works in elixir under the hood so there may be a much better solution or maybe something in the wider design means this has to be the way things are done. I’m just interested why really. Again it seems to me not to be the standard in other languages like Erlang as @kokolegorille pointed out.
The reason why Enum always returns a list is because there are many Enumerable.t types, which invalidate their own constraints when mapped, like ranges or various streamed enumerables. E.g. what would Enum.map(1..10, & &1 * 10) return.
As alternative in elixir you have the Collectable protocol as well as all the erlang/datatype specific functions like the already mentioned Map.new.