Qqwy

Qqwy

TypeCheck Core Team

Map vs Reduce

Elixirs Enumerable protocol is built upon the implementation of reduce for different kinds of data structures.

Today, I had a discussion with @benwilson512, di4na and @martin in Slack, about what would be possible if map was used as building block instead.

Basically, there are things that you cannot do with just map, and things you cannot do with just reduce. You can build a semi-working map on top of reduce, but this will not work in the following circumstances:

  • Concurrent access to elements. (see for instance conc lists)
  • Changing values inside complex trees without modifying the tree shape. (AFAIK there is no way for reduce to know the shape of the container it is working on)
    (there is possibly more; let us know)

As a side note, the current reduce algorithm and most of the other functions in Enum that use it internally always return the result as a list.

Of course, there are some things that are reducable but not mappable, such as Ranges.

There are other things that are mappable, but not reducable, such as for instance a ‘Maybe’ (that is either Some x or Nothing).

I think it is interesting to brainstorm about this; Maybe the addition of a Mappable protocol isn’t such a weird idea…

Where Next?

Popular in Discussions Top

AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31707 143
New
AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
New
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
Fl4m3Ph03n1x
Background This question comes mainly from my ignorance. Today is Black Friday, one of my favorite days of the year to buy books. One boo...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

We're in Beta

About us Mission Statement