User:Ushkin N/Comparison of programming languages/Functional/Map

The map function originated in functional programming languages.

The Lisp programming language introduced a map function called in 1959, with slightly different versions already appearing in 1958. This is the original definition for, mapping a function over successive rest lists:

The function  is still available in newer Lisps like Common Lisp, though functions like   would be preferred.

Squaring the elements of a list using  would be written in s-expression notation like this:

Using the function, above example would be written like this:

Today mapping functions are supported (or may be defined) in many procedural, object oriented, and multi-paradigm languages as well: In C++'s Standard Template Library, it is called, in C# (3.0)'s LINQ library, it is provided as an extension method called. Map is also a frequently used operation in high level languages such as CFML, Perl, Python and Ruby; the operation is called  in all four of these languages. A  alias for   is also provided in Ruby (from Smalltalk). Common Lisp provides a family of map-like functions; the one corresponding to the behavior described here is called  (  indicating access using the CAR operation). There are also languages with syntactic constructs providing the same functionality as the map function.

Map is sometimes generalized to accept dyadic (2-argument) functions that can apply a user-supplied function to corresponding elements from two lists; some languages use special names for this, such as map2 or zipWith. Languages using explicit variadic functions may have versions of map with variable arity to support variable-arity functions. Map with 2 or more lists encounters the issue of handling when the lists are of different lengths. Various languages differ on this; some raise an exception, some stop after the length of the shortest list and ignore extra items on the other lists; some continue on to the length of the longest list, and for the lists that have already ended, pass some placeholder value to the function indicating no value.

In languages which support first-class functions,  may be partially applied to lift a function that works on only one value to an element-wise equivalent that works on an entire container; for example,   is a Haskell function which squares each element of a list.