DEV Community

Discussion on: Why is map called map?

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
 
techgirl1908 profile image
Angie Jones

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
 
wolverineks profile image
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).

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