minhajuddin

minhajuddin

Reduce vs Map, which is preferable?

While writing code, I always prefer using Enum.map instead of Enum.reduce, if possible, as the Enum.map version tends to be more readable, and I try to tell others to do the same, I wanted to know what you guys feel about this. Is this a good thing/bad thing?

Here are a few examples:

users = [
  %User{id:1, name: "Mujju"},
  %User{id:2, name: "Zainu"},
]

# reduce version
Enum.reduce(users, %{}, fn user, acc -> Map.put(user.id, user) end)

# map version
Enum.map(users, fn user -> {user.id, user} end) |> Map.new

Most Liked

NobbZ

NobbZ

Let me answer this question 2 times. One answer will be generic and the other one will be specialised to the question.


In general map and reduce over an Enum are totally different concepts.

To map means to apply a function to every element of an Enum and gives you a List which can be transformed into an Enum which has the same shape as the original data.

On the other hand we have reduce, which applies a function to each element and a moving accumulator to finaly reduce the complete Enum into a single value. There is no guarantee that you can reconstruct the original shape of data and not even a guarantee that your result is an Enum!

So choosing which one you choose totally depends on what kind of data you need after the iteration of the Enum.


Answering the question with the actual examples in mind is a bit harder, since we might tend here to rely on well known implementation details in the stdlib.

Since we want to retain the shape of the original data, the data |> Enum.map(f) |> Enum.into(%{}) (or Map.new/1) is preferable semantically. Currently it is in fact not as fast as reduceing directly, since it is reducing about two times due to implementation details.

So if you really need the speed, you should make sure, that you benchmark both versions with data sizes you do expect to be the average in production. Keep an eye on the result of the benchmarks and check if your choosen version is still the faster one with each elixir release, that also bumps the OTP version. There might happen optimisations in both of them, that suddenly make the semantically correct version the faster one all of a sudden.

12
Post #7
Qqwy

Qqwy

TypeCheck Core Team

Enum.map has been defined underwater in terms of Enum.reduce.
(or more astutely, Enum.map and Enum.reduce are both thin wrappers around Enumerable.reduce, which does the actual reducing.)

Enum.map is not a true map (the map that is part of the Functor Algebraic Data Type): If it would be, mapping over a Range would return a Range, mapping over a File would return a File and mapping over a Tree would return a Tree.

But alas, this is not the case: Enumerable is in fact a protocol that specifies the ADT frequently known as ‘Foldable’. Enum.map might thus more properly be named: Enum.to_list_then_map. The order of elements in this resulting list only matters if they mattered in the original enumerable (so for e.g. MapSet it does not, and when you convert a Tree into a List, there are multiple equally valid ways to do this, so pick one and stick with it).

The reason that Enum.map was built on top of Enumerable.reduce (and always converts to a list) is that it allows for easy use of data types that are not Functors, such as MapSet (and other Sets): If you’d do a true map on a set that results in multiple elements in the set having the same value, then it becomes invalid (so a Set cannot expose a true Functor map).

So:

  • Enum.map has an arguably confusing name, as it not only the given function over a data structure, it also always converts the data structure to a list.
  • Enum.map is built on top of reduce; it therefore is always possible to use Enum.reduce, but in cases where you don’t need access to the accumulator that is being built, there is no need to do so. This is simply personal preference. (You are always allowed reinvent the wheel if you really want to :stuck_out_tongue_winking_eye:)
  • If you do not want to lose the structure of your original data type, Enum.map is not the answer to your problem.
madeinussr

madeinussr

Well, I understood the example’s intent as “Get the list of users and construct a map based on it”, not “Transform each user in the list”. If you just want transform every item in a list - use map. If you want to get something new (different type) from an input - use reduce. IMHO :slight_smile:

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement