# Map vs Reduce

**URL:** <https://forum.elixirforum.com/t/map-vs-reduce/979>\
**Category:** Discussions\
**Tags:** protocols, enumerables\
**Created:** [June 29, 2016, 10:08pm UTC](https://forum.elixirforum.com/t/map-vs-reduce/979 "2016-06-29T22:08:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qqwy](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/qqwy/32/1349_2.png) [@Qqwy](https://forum.elixirforum.com/u/Qqwy)\
**Post date:** [June 29, 2016, 10:08pm UTC](https://forum.elixirforum.com/t/map-vs-reduce/979/1 "2016-06-29T22:08:27Z")

</div>

Elixirs [Enumerable](http://elixir-lang.org/docs/stable/elixir/Enumerable.html#reduce/3) 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](http://dustin.sallings.org/2010/03/04/erlang-conc.html))
- 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…
