loading...

Why is map called map?

techgirl1908 profile image Angie Jones ・1 min read

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.

Discussion

pic
Editor guide
Collapse
yawaramin profile image
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.

Collapse
wolverineks profile image
Kevin Sullivan

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

Collapse
citizen428 profile image
Michael Kohl

the 'map' operation is one that, in mathematics, makes some guarantees.

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

Collapse
techgirl1908 profile image
Angie Jones Author

thanks for sharing this!

Collapse
johnkazer profile image
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'.

Collapse
mateiadrielrafael profile image
Matei Adriel

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

Collapse
citizen428 profile image
Michael Kohl

They 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:

{ a: 1, b: 2 }.itself
#=> {:a=>1, :b=>2}
{ a: 1, b: 2 }.map(&:itself)
#=> [[:a, 1], [:b, 2]]
Thread Thread
mateiadrielrafael profile image
Matei Adriel

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

Collapse
yawaramin profile image
Yawar Amin

I deliberately left out FP words :-)

Thread Thread
mateiadrielrafael profile image
Matei Adriel

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

Collapse
cappe987 profile image
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.

Collapse
emiifont profile image
Emilio Font

I didn't know this, thanks for posting.

Collapse
xanderyzwich profile image
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.

Collapse
waylonwalker profile image
Waylon Walker

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

Collapse
adam_cyclones profile image
Adam Crockett

A map from A to B I had assumed.

Collapse
polyterative profile image
Vlady

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