Angie Jones

Posted on

Why is map called map?

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.

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 was even kind enough to let me know I wasn't a total failure for not knowing this.

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

Yawar Amin

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 n elements, you get back an array with n elements. 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.

Kevin Sullivan • Edited

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)`.

Also note that all the structures which can be mapped upon are called `functors`. That is a pretty important term in the fp world

Yawar Amin

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

Angie Jones

thanks for sharing this!

John Kazer

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'.

Casper

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.

Corey McCarty

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

Ye, fair enough, I shouldve also included a note about the laws

Waylon Walker

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

Emilio Font

I didn't know this, thanks for posting.