Programming: A higher-order function which, when applied to a function (or a block or expression) and a list (or array), iterates over the list, applies the function to each element, and returnds a list composed of the results. For example, if you map a list containing the numbers 1, 2, and 3, using a function that returned the value of its argument plus 1, then you would get a list containing the numbers 2, 3, and 4. In many programming languages, there is a map function which is either part of the standard library (as in Haskell - map is defined in the Prelude) or defined internally (as in Perl).
map can be defined in Haskell as
map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (item:list) = f item : map f list
In imperative langauges, map is replacing iterative loops as a way of processing lists of data. For example, in Perl, things like
for (my $i = 0; $i < @m; $i++) {
$usernames[$i] = getpwuid $uids[$i];
}
which is the way it would be done in C (and is remarkably similar to the corresponding C code), can be written more nicely as
@usernames = map { getpwuid $_ } @uids;
instead. This elegance becomes even more apparent when multiple maps are strung together, as in the Schwartzian transform. Examples of usage:
Haskell
map (+1) [1, 2, 3]
-- result: [2, 3, 4]
Perl
In Perl, array elements are associated to $_ inside the block passed to map. Modifying $_ inside the block will modify the corresponding array element (which is an error if the array is a constant).
map { $_ + 1 } qw(1 2 3);
# result: qw(2 3 4)
A nifty trick is to have the map block return pairs which you then assign to a hash, e.g. creating a hash which maps letters to numbers:
%h = map { $_ => ord($_) - ord('a') + 1 } 'a' .. 'z';
Scheme
(map (lambda (n) (+ 1 n)) '(1 2 3))
; result: '(2 3 4)
Ruby
In Ruby, map is a method of the Array class, spelt "collect". You can use the collect! method to modify elements with the result of the block. Both can be used on constant arrays.
[1, 2, 3].collect { |x| x + 1 }
# result: [2, 3, 4]
Also, when discussing code, "map" can be used as a verb, meaning to process some data using the map function, e.g. "The user ids then get mapped to usernames".