nhpip

nhpip

I know this is a really minor peeve, but why is this the case?:

iex(cs@localhost)54> %{key1: :i_am_a_map_whooot} |> Enum.map(&(&1))
[key1: :i_am_a_maaa___list__what_the?]

Yes I know that all I need to do is |> Enum.into(%{}) but why? Why shouldn’t the ouput type be the same as the input type?

I’m sure this has been asked 1000 times before.

Showing Posts 1 to 10

Eiji

Eiji

You don’t have to. You can call reduce over new map like this:

iex> Enum.reduce(%{a: 1, b: 2, c: 3}, %{}, fn {key, value}, acc ->
  Map.put(acc, key, value * 2)
end)
%{a: 2, b: 4, c: 6}

Note: The order of map elements is not guaranteed to be sorted.

nhpip

nhpip OP

But, I don’t know, seems like extra work. I’m so used to it that it doesn’t bother me that much, I just thought that it was an odd implementation detail.

Maybe we need an Enum.fmap/2 like Haskell?

dimitarvp

dimitarvp

Using Enum.map or Enum.reduce and working with two-tuples and then just doing Map.new does not justify the need for a function that does the same IMO. Just do your stuff and slap a Map.new at the end.

sodapopcan

sodapopcan

One thing is that the result of your map operation is in a predictable order. I don’t have a real world example of where this would particularly useful, but in a pipeline it could be. Having each result getting converted back to a map would be pretty chaotic.

Eiji

Eiji

Oh, right I forgot about it. Unfortunately we do not have a way to map the Map without converting it first to a list, see:

https://github.com/elixir-lang/elixir/blob/349321117ebb598cd10a4f22e8ea8e1c4b13b6c8/lib/elixir/lib/enum.ex#L4879-L4881

https://github.com/elixir-lang/elixir/blob/41f6ee3f2e605f7066b23d96362e3b0c620c565b/lib/elixir/lib/map.ex#L248-L264

So at the very end we do exactly the same. :slightly_frowning_face:

dimitarvp

dimitarvp

Yep, I also checked the Elixir source a while ago. IMO doing Map.new only once is more performant than doing N times Map.put so I am always doing that.

Marcus

Marcus

As mentioned before, Enum.map on a map returns a list because this would fit better to pipes without the need to convert to a list and back behind the scenes in every step.

There used to be the Map.map/2 function in Elixir, which is now deprecated. But you get the same result with Map.new/2.

iex(1)> map = %{a: 1, b: 2, c: 3}
%{c: 3, a: 1, b: 2}
iex(2)> Map.map(map, fn {key, value} -> {key, value * value} end)
warning: Map.map/2 is deprecated. Use Map.new/2 instead (invoke Map.from_struct/1 before if you have a struct)
└─ iex:2

%{c: {:c, 9}, a: {:a, 1}, b: {:b, 4}}
iex(3)> Map.new(map, fn {key, value} -> {key, value * value} end)
%{c: 9, a: 1, b: 4}
BartOtten

BartOtten

Except when the input is a map, there is no guarantee about order in the first place. So this argument doesn’t seem to hold.

For the argument that conversion would take place between steps in a pipe; why would it? Elixir can detect it is in a pipeline and not convert in between as an optimization.

Once you know the ‘conversion’ it does not matter that much anymore. But I see how returning the same type as the input makes sense from a developer experience.

wojtekmach

wojtekmach

Hex Core Team

The output type of Enum.map (and most Enum functions) cannot be the same as the input type because Enum works on a variety of data types through the Enumerable protocol. A simple example is:

iex> Enum.map(1..3, & &1 ** 2)
[1, 4, 9]

We couldn’t possibly keep the shape of the input, %Range{}, because the end result would be nonsensical. Some other things that implement Enumerable protocol are file streams and even infinite streams.

As was mentioned before there used to be a Map.map/2 but it got deprecated because a similar function existed all along, Map.new/2:

iex> Map.new(1..3, fn i -> {i, i ** 2} end)
%{1 => 1, 2 => 4, 3 => 9}

so yeah, long story short, in your particular case instead of Enum.map/2 use Map.new/2.

13
Post #9
BartOtten

BartOtten

Seems an implementation detail in the case of Range. From an outside perspective the range simply expands to a list so a list in return makes sense. As far as I can see the sole reason for having a ‘Range’ struct during processing is optimization, and explicit use of the struct is deprecated.

—-snip this part: Too early in the morning—-
—- and the start was incorrect too, see below —-

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews