I've been using the `map`

method in Java and JavaScript for a while now, and every time I use it I wonder why was it named "map". It wasn't obvious to me how the name corresponded to the actual functionality.

So, I went to my favorite research site, twitter.com (lol), and asked the community.

I understand what the `map` helper does (for the love of all things good, please don't try to explain this to me) but I've never understood why it's actually called map. That doesn't seem to be a fitting name. This has always nagged at me. Anyone have clear insight?00:57 AM - 31 Dec 2019

Within a matter of minutes, I received wonderful replies and all of a sudden, everything made sense!

It's not some relation to the data structure `Map`

that I've been using all my life. It's a math term!

Laurie Voss@seldo@techgirl1908 It comes from the mathematical sense of "map", which in turn is drawing an analogy from an actual map of the world. It's a function that relates all the points on one set to another set, the way a geographical map relates points on a piece of paper to points in the world.01:03 AM - 31 Dec 2019

Adeeyo Sulaiman A.@adeeyosulaiman@techgirl1908 "map" comes from Mathematics Set Theory. A map is a function that takes elements of a set (in here, an array) and creates an image set, known as codomain (in here, result array).01:14 AM - 31 Dec 2019

Laurie was even kind enough to let me know I wasn't a total failure for not knowing this.

Laurie Voss@seldo@techgirl1908 This isn't a super helpful term in mathematics to begin with, and adding another hop by taking it to programming makes it even more obscure. I don't think it's a very intuitive word for this action, something like "translate" or "convert" would be more meaningful to me.01:05 AM - 31 Dec 2019

So there you have it folks! Hopefully this was a "TIL" moment for you as well.

## Discussion (16)

The answers there are good, but I want to add one more point: the 'map' operation is one that, in mathematics, makes some guarantees. Calling it 'map' in a programming language is a kind of a shortcut way of saying 'we make those guarantees too'. The overarching guarantee of 'map' is that its output data has the same 'structure' as its input data. E.g., if you map over an array with

nelements, you get back an array withnelements. The structure is preserved.'Map' can also be defined for other data structures, like linked lists, trees, graphs, sets, and so on. So that's the nice thing about it–if you come across that name, you can be reasonably sure that it's structure-preserving.

Also note that all the structures which can be mapped upon are called

`functors`

. That is a pretty important term in the fp worldThey are only called

`functors`

if they obey the functor laws. In non-FP languages it's not uncommon to have "mappaples" that aren't functors.As an example, Ruby hashes are not functors, even though they have a

`map`

method, since they violate the first functor law (`fmap id = id`

). In other words they are not structure preserving:Ye, fair enough, I shouldve also included a note about the laws

I deliberately left out FP words :-)

Yep, not really necessary unless using a fp lang or a lib such as fp-ts, meant it to be more of a fun fact

It's structure preserving if its an exact functor. There are also what's called forgetful functors. Surjective mappings in set theory are not structure preserving, as say the domain of

`n mod 2`

is {0, 1}, so of a very different cardinality than ℕ.Which is weird to me, because I think of a physical map as changing the structure and keeping the data, and a mathematical/programming map to be keeping the structure and changing the data.

In my mind it would make more sense to map a linked list onto an array, or map an object onto an array,

`Object.values(someObject)`

.thanks for sharing this!

I would also add that programming adds to the original mathematics (without mentioning algebraic data structures) by enabling a data structure, such as an array, to be "mappable". That is, the data structure should support certain functions/methods which enable the map function to operate.

The details of course vary by language, but are crucial in enabling a generic map function to work with arrays, linked lists, trees, graphs, sets etc. So for Clojure, any data structure can be mapped if it implements 'first', 'rest' and 'cons'.

Interesting question. I was aware of the connection to mathematics, but I've never heard of the analogy of a geographical map.

The data structure Map is essentially also related to the same origin. It maps a key to a value and the same input will always give the same output. While the data structure and the function are two separate things in programming they still follow the same concept.

Thanks for sharing the results of your research with the rest of us.

I didn't know this, thanks for posting.

I don't think that it is completely divorced from the map data structure because that structure is a mapping from keys to values.

A map from A to B I had assumed.

Idk map looks like the most appropriate name for me. It was harder to learn but now that I know I like it this way