fbpx
Wikipedia

Map (higher-order function)

In many programming languages, map is a higher-order function that applies a given function to each element of a collection, e.g. a list or set, returning the results in a collection of the same type. It is often called apply-to-all when considered in functional form.

The concept of a map is not limited to lists: it works for sequential containers, tree-like containers, or even abstract containers such as futures and promises.

Examples: mapping a list edit

Suppose we have a list of integers [1, 2, 3, 4, 5] and would like to calculate the square of each integer. To do this, we first define a function to square a single number (shown here in Haskell):

square x = x * x 

Afterwards we may call

>>> map square [1, 2, 3, 4, 5] 

which yields [1, 4, 9, 16, 25], demonstrating that map has gone through the entire list and applied the function square to each element.

Visual example edit

Below, you can see a view of each step of the mapping process for a list of integers X = [0, 5, 8, 3, 2, 1] that we want to map into a new list X' according to the function   :

 
View of processing steps when applying map function on a list

The map is provided as part of the Haskell's base prelude (i.e. "standard library") and is implemented as:

map :: (a -> b) -> [a] -> [b] map _ [] = [] map f (x : xs) = f x : map f xs 

Generalization edit

In Haskell, the polymorphic function map :: (a -> b) -> [a] -> [b] is generalized to a polytypic function fmap :: Functor f => (a -> b) -> f a -> f b, which applies to any type belonging the Functor type class.

The type constructor of lists [] can be defined as an instance of the Functor type class using the map function from the previous example:

instance Functor [] where  fmap = map 

Other examples of Functor instances include trees:

-- a simple binary tree data Tree a = Leaf a | Fork (Tree a) (Tree a) instance Functor Tree where   fmap f (Leaf x) = Leaf (f x)  fmap f (Fork l r) = Fork (fmap f l) (fmap f r) 

Mapping over a tree yields:

>>> fmap square (Fork (Fork (Leaf 1) (Leaf 2)) (Fork (Leaf 3) (Leaf 4))) Fork (Fork (Leaf 1) (Leaf 4)) (Fork (Leaf 9) (Leaf 16)) 

For every instance of the Functor type class, fmap is contractually obliged to obey the functor laws:

fmap id  id  -- identity law fmap (f . g)  fmap f . fmap g -- composition law 

where . denotes function composition in Haskell.

Among other uses, this allows defining element-wise operations for various kinds of collections.

Category-theoretic background edit

In category theory, a functor   consists of two maps: one that sends each object   of the category to another object  , and one that sends each morphism   to another morphism  , which acts as a homomorphism on categories (i.e. it respects the category axioms). Interpreting the universe of data types as a category  , with morphisms being functions, then a type constructor F that is a member of the Functor type class is the object part of such a functor, and fmap :: (a -> b) -> F a -> F b is the morphism part. The functor laws described above are precisely the category-theoretic functor axioms for this functor.

Functors can also be objects in categories, with "morphisms" called natural transformations. Given two functors  , a natural transformation   consists of a collection of morphisms  , one for each object   of the category  , which are 'natural' in the sense that they act as a 'conversion' between the two functors, taking no account of the objects that the functors are applied to. Natural transformations correspond to functions of the form eta :: F a -> G a, where a is a universally quantified type variable – eta knows nothing about the type which inhabits a. The naturality axiom of such functions is automatically satisfied because it is a so-called free theorem, depending on the fact that it is parametrically polymorphic.[1] For example, reverse :: List a -> List a, which reverses a list, is a natural transformation, as is flattenInorder :: Tree a -> List a, which flattens a tree from left to right, and even sortBy :: (a -> a -> Bool) -> List a -> List a, which sorts a list based on a provided comparison function.

Optimizations edit

The mathematical basis of maps allow for a number of optimizations. The composition law ensures that both

  • (map f . map g) list and
  • map (f . g) list

lead to the same result; that is,  . However, the second form is more efficient to compute than the first form, because each map requires rebuilding an entire list from scratch. Therefore, compilers will attempt to transform the first form into the second; this type of optimization is known as map fusion and is the functional analog of loop fusion.[2]

Map functions can be and often are defined in terms of a fold such as foldr, which means one can do a map-fold fusion: foldr f z . map g is equivalent to foldr (f . g) z.

The implementation of map above on singly linked lists is not tail-recursive, so it may build up a lot of frames on the stack when called with a large list. Many languages alternately provide a "reverse map" function, which is equivalent to reversing a mapped list, but is tail-recursive. Here is an implementation which utilizes the fold-left function.

reverseMap f = foldl (\ys x -> f x : ys) [] 

Since reversing a singly linked list is also tail-recursive, reverse and reverse-map can be composed to perform normal map in a tail-recursive way, though it requires performing two passes over the list.

Language comparison edit

The map function originated in functional programming languages.

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

maplist[x;f] = [null[x] -> NIL;T -> cons[f[x];maplist[cdr[x];f]]] 

The function maplist is still available in newer Lisps like Common Lisp,[5] though functions like mapcar or the more generic map would be preferred.

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

(maplist (lambda (l) (sqr (car l))) '(1 2 3 4 5)) 

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

(mapcar (function sqr) '(1 2 3 4 5)) 

Today mapping functions are supported (or may be defined) in many procedural, object-oriented, and multi-paradigm languages as well: In C++'s Standard Library, it is called std::transform, in C# (3.0)'s LINQ library, it is provided as an extension method called Select. Map is also a frequently used operation in high level languages such as ColdFusion Markup Language (CFML), Perl, Python, and Ruby; the operation is called map in all four of these languages. A collect alias for map 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 mapcar (-car 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 and currying, map 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, map square is a Haskell function which squares each element of a list.

Map in various languages
Language Map Map 2 lists Map n lists Notes Handling lists of different lengths
APL func list list1 func list2 func/ list1 list2 list3 list4 APL's array processing abilities make operations like map implicit length error if list lengths not equal or 1
Common Lisp (mapcar func list) (mapcar func list1 list2) (mapcar func list1 list2 ...) stops after the length of the shortest list
C++ std::transform(begin, end, result, func) std::transform(begin1, end1, begin2, result, func) in header <algorithm>
begin, end, and result are iterators
result is written starting at result
C# ienum.Select(func)
or
The select clause
ienum1.Zip(ienum2, func) Select is an extension method
ienum is an IEnumerable
Zip is introduced in .NET 4.0
Similarly in all .NET languages
stops after the shortest list ends
CFML obj.map(func) Where obj is an array or a structure. func receives as arguments each item's value, its index or key, and a reference to the original object.
Clojure (map func list) (map func list1 list2) (map func list1 list2 ...) stops after the shortest list ends
D list.map!func zip(list1, list2).map!func zip(list1, list2, ...).map!func Specified to zip by StoppingPolicy: shortest, longest, or requireSameLength
Erlang lists:map(Fun, List) lists:zipwith(Fun, List1, List2) zipwith3 also available Lists must be equal length
Elixir Enum.map(list, fun) Enum.zip(list1, list2) |> Enum.map(fun) List.zip([list1, list2, ...]) |> Enum.map(fun) stops after the shortest list ends
F# List.map func list List.map2 func list1 list2 Functions exist for other types (Seq and Array) Throws exception
Groovy list.collect(func) [list1 list2].transpose().collect(func) [list1 list2 ...].transpose().collect(func)
Haskell map func list zipWith func list1 list2 zipWithn func list1 list2 ... n corresponds to the number of lists; predefined up to zipWith7 stops after the shortest list ends
Haxe array.map(func)
list.map(func)
Lambda.map(iterable, func)
J func list list1 func list2 func/ list1, list2, list3 ,: list4 J's array processing abilities make operations like map implicit length error if list lengths not equal
Java 8+ stream.map(func)
JavaScript 1.6
ECMAScript 5
array#map(func) List1.map(function (elem1, i) {
return func(elem1, List2[i]); })
List1.map(function (elem1, i) {
return func(elem1, List2[i], List3[i], ...); })
Array#map passes 3 arguments to func: the element, the index of the element, and the array. Unused arguments can be omitted. Stops at the end of List1, extending the shorter arrays with undefined items if needed.
Julia map(func, list) map(func, list1, list2) map(func, list1, list2, ..., listN) ERROR: DimensionMismatch
Logtalk map(Closure, List) map(Closure, List1, List2) map(Closure, List1, List2, List3, ...) (up to seven lists) Only the Closure argument must be instantiated. Failure
Mathematica func /@ list
Map[func, list]
MapThread[func, {list1, list2}] MapThread[func, {list1, list2, ...}] Lists must be same length
Maxima map(f, expr1, ..., exprn)
maplist(f, expr1, ..., exprn)
map returns an expression which leading operator is the same as that of the expressions;
maplist returns a list
OCaml List.map func list
Array.map func array
List.map2 func list1 list2 raises Invalid_argument exception
PARI/GP apply(func, list)
Perl map block list
map expr, list
In block or expr special variable $_ holds each value from list in turn. Helper List::MoreUtils::each_array combines more than one list until the longest one is exhausted, filling the others with undef.
PHP array_map(callable, array) array_map(callable, array1,array2) array_map(callable, array1,array2, ...) The number of parameters for callable
should match the number of arrays.
extends the shorter lists with NULL items
Prolog maplist(Cont, List1, List2). maplist(Cont, List1, List2, List3). maplist(Cont, List1, ...). List arguments are input, output or both. Subsumes also zipWith, unzip, all Silent failure (not an error)
Python map(func, list) map(func, list1, list2) map(func, list1, list2, ...) Returns a list in Python 2 and an iterator in Python 3. zip() and map() (3.x) stops after the shortest list ends, whereas map() (2.x) and itertools.zip_longest() (3.x) extends the shorter lists with None items
Ruby enum.collect {block}
enum.map {block}
enum1.zip(enum2).map {block} enum1.zip(enum2, ...).map {block}
[enum1, enum2, ...].transpose.map {block}
enum is an Enumeration stops at the end of the object it is called on (the first list); if any other list is shorter, it is extended with nil items
Rust list1.into_iter().map(func) list1.into_iter().zip(list2).map(func) the Iterator::map and Iterator::zip methods both take ownership of the original iterator and return a new one; the Iterator::zip method internally calls the IntoIterator::into_iter method on list2 stops after the shorter list ends
S-R lapply(list, func) mapply(func, list1, list2) mapply(func, list1, list2, ...) Shorter lists are cycled
Scala list.map(func) (list1, list2).zipped.map(func) (list1, list2, list3).zipped.map(func) note: more than 3 not possible. stops after the shorter list ends
Scheme (including Guile and Racket) (map func list) (map func list1 list2) (map func list1 list2 ...) lists must all have same length (SRFI-1 extends to take lists of different length)
Smalltalk aCollection collect: aBlock aCollection1 with: aCollection2 collect: aBlock Fails
Standard ML map func list ListPair.map func (list1, list2)
ListPair.mapEq func (list1, list2)
For 2-argument map, func takes its arguments in a tuple ListPair.map stops after the shortest list ends, whereas ListPair.mapEq raises UnequalLengths exception
Swift sequence.map(func) zip(sequence1, sequence2).map(func) stops after the shortest list ends
XPath 3
XQuery 3
list ! block
for-each(list, func)
for-each-pair(list1, list2, func) In block the context item . holds the current value stops after the shortest list ends

See also edit

References edit

  1. ^ In a non-strict language that permits general recursion, such as Haskell, this is only true if the first argument to fmap is strict. Wadler, Philip (September 1989). Theorems for free! (PDF). 4th International Symposium on Functional Programming Languages and Computer Architecture. London: Association for Computing Machinery.
  2. ^ "Map fusion: Making Haskell 225% faster"
  3. ^ J. McCarthy, K. Maling, S. Russell, N. Rochester, S. Goldberg, J. Slagle. LISP Programmer's Manual. March-April, 1959
  4. ^ J. McCarthy: Symbol Manipulating Language - Revisions of the Language. AI Memo No. 4, October 1958
  5. ^ Function MAPC, MAPCAR, MAPCAN, MAPL, MAPLIST, MAPCON in ANSI Common Lisp

higher, order, function, abstract, data, type, same, name, data, structure, this, article, relies, largely, entirely, single, source, relevant, discussion, found, talk, page, please, help, improve, this, article, introducing, citations, additional, sources, fi. For the abstract data type of the same name see Map data structure This article relies largely or entirely on a single source Relevant discussion may be found on the talk page Please help improve this article by introducing citations to additional sources Find sources Map higher order function news newspapers books scholar JSTOR November 2012 In many programming languages map is a higher order function that applies a given function to each element of a collection e g a list or set returning the results in a collection of the same type It is often called apply to all when considered in functional form The concept of a map is not limited to lists it works for sequential containers tree like containers or even abstract containers such as futures and promises Contents 1 Examples mapping a list 1 1 Visual example 2 Generalization 2 1 Category theoretic background 3 Optimizations 4 Language comparison 5 See also 6 ReferencesExamples mapping a list editSuppose we have a list of integers 1 2 3 4 5 and would like to calculate the square of each integer To do this we first define a function to square a single number shown here in Haskell square x x x Afterwards we may call gt gt gt map square 1 2 3 4 5 which yields 1 4 9 16 25 demonstrating that map has gone through the entire list and applied the function square to each element Visual example edit Below you can see a view of each step of the mapping process for a list of integers X 0 5 8 3 2 1 that we want to map into a new list X according to the function f x x 1 displaystyle f x x 1 nbsp nbsp View of processing steps when applying map function on a list The map is provided as part of the Haskell s base prelude i e standard library and is implemented as map a gt b gt a gt b map map f x xs f x map f xsGeneralization editSee also Functor and Category theory In Haskell the polymorphic function map a gt b gt a gt b is generalized to a polytypic function fmap Functor f gt a gt b gt f a gt f b which applies to any type belonging the Functor type class The type constructor of lists can be defined as an instance of the Functor type class using the map function from the previous example instance Functor where fmap map Other examples of Functor instances include trees a simple binary tree data Tree a Leaf a Fork Tree a Tree a instance Functor Tree where fmap f Leaf x Leaf f x fmap f Fork l r Fork fmap f l fmap f r Mapping over a tree yields gt gt gt fmap square Fork Fork Leaf 1 Leaf 2 Fork Leaf 3 Leaf 4 Fork Fork Leaf 1 Leaf 4 Fork Leaf 9 Leaf 16 For every instance of the Functor type class fmap is contractually obliged to obey the functor laws fmap id id identity law fmap f g fmap f fmap g composition law where denotes function composition in Haskell Among other uses this allows defining element wise operations for various kinds of collections Category theoretic background edit In category theory a functor F C D displaystyle F C rightarrow D nbsp consists of two maps one that sends each object A displaystyle A nbsp of the category to another object F A displaystyle FA nbsp and one that sends each morphism f A B displaystyle f A rightarrow B nbsp to another morphism F f F A F B displaystyle Ff FA rightarrow FB nbsp which acts as a homomorphism on categories i e it respects the category axioms Interpreting the universe of data types as a category T y p e displaystyle Type nbsp with morphisms being functions then a type constructor F that is a member of the Functor type class is the object part of such a functor and fmap a gt b gt F a gt F b is the morphism part The functor laws described above are precisely the category theoretic functor axioms for this functor Functors can also be objects in categories with morphisms called natural transformations Given two functors F G C D displaystyle F G C rightarrow D nbsp a natural transformation h F G displaystyle eta F rightarrow G nbsp consists of a collection of morphisms h A F A G A displaystyle eta A FA rightarrow GA nbsp one for each object A displaystyle A nbsp of the category D displaystyle D nbsp which are natural in the sense that they act as a conversion between the two functors taking no account of the objects that the functors are applied to Natural transformations correspond to functions of the form eta F a gt G a where a is a universally quantified type variable eta knows nothing about the type which inhabits a The naturality axiom of such functions is automatically satisfied because it is a so called free theorem depending on the fact that it is parametrically polymorphic 1 For example reverse List a gt List a which reverses a list is a natural transformation as is flattenInorder Tree a gt List a which flattens a tree from left to right and even sortBy a gt a gt Bool gt List a gt List a which sorts a list based on a provided comparison function Optimizations editThe mathematical basis of maps allow for a number of optimizations The composition law ensures that both map f map g list and map f g list lead to the same result that is map f map g map f g displaystyle operatorname map f circ operatorname map g operatorname map f circ g nbsp However the second form is more efficient to compute than the first form because each map requires rebuilding an entire list from scratch Therefore compilers will attempt to transform the first form into the second this type of optimization is known as map fusion and is the functional analog of loop fusion 2 Map functions can be and often are defined in terms of a fold such as foldr which means one can do a map fold fusion foldr f z map g is equivalent to foldr f g z The implementation of map above on singly linked lists is not tail recursive so it may build up a lot of frames on the stack when called with a large list Many languages alternately provide a reverse map function which is equivalent to reversing a mapped list but is tail recursive Here is an implementation which utilizes the fold left function reverseMap f foldl ys x gt f x ys Since reversing a singly linked list is also tail recursive reverse and reverse map can be composed to perform normal map in a tail recursive way though it requires performing two passes over the list Language comparison editThe map function originated in functional programming languages The language Lisp introduced a map function called maplist 3 in 1959 with slightly different versions already appearing in 1958 4 This is the original definition for maplist mapping a function over successive rest lists maplist x f null x gt NIL T gt cons f x maplist cdr x f The function maplist is still available in newer Lisps like Common Lisp 5 though functions like mapcar or the more generic map would be preferred Squaring the elements of a list using maplist would be written in S expression notation like this maplist lambda l sqr car l 1 2 3 4 5 Using the function mapcar above example would be written like this mapcar function sqr 1 2 3 4 5 Today mapping functions are supported or may be defined in many procedural object oriented and multi paradigm languages as well In C s Standard Library it is called std transform in C 3 0 s LINQ library it is provided as an extension method called Select Map is also a frequently used operation in high level languages such as ColdFusion Markup Language CFML Perl Python and Ruby the operation is called map in all four of these languages A collect alias for map 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 mapcar car 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 and currying map 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 map square is a Haskell function which squares each element of a list Map in various languages Language Map Map 2 lists Map n lists Notes Handling lists of different lengths APL i func i i list i i list1 i i func i i list2 i i func i i list1 i i list2 i i list3 i i list4 i APL s array processing abilities make operations like map implicit length error if list lengths not equal or 1 Common Lisp mapcar i func i i list i mapcar i func i i list1 i i list2 i mapcar i func i i list1 i i list2 i stops after the length of the shortest list C std transform wbr wbr i begin i i end i i result i i func i std transform wbr wbr i begin1 i i end1 i i begin2 i i result i i func i in header lt algorithm gt begin end and result are iterators result is written starting at result C i ienum i Select i func i orThe select clause i ienum1 i Zip i ienum2 i i func i Select is an extension method ienum is an IEnumerableZip is introduced in NET 4 0Similarly in all NET languages stops after the shortest list ends CFML obj map func Where obj is an array or a structure func receives as arguments each item s value its index or key and a reference to the original object Clojure map i func i i list i map i func i i list1 i i list2 i map i func i i list1 i i list2 i stops after the shortest list ends D i list i map i func i zip i list1 i i list2 i map i func i zip i list1 i i list2 i map i func i Specified to zip by StoppingPolicy shortest longest or requireSameLength Erlang lists map i Fun i i List i lists zipwith i Fun i i List1 i i List2 i i zipwith3 i also available Lists must be equal length Elixir Enum map i list i i fun i Enum zip i list1 i i list2 i gt Enum map fun List zip i list1 i i list2 i gt Enum map fun stops after the shortest list ends F List map i func i i list i List map2 i func i i list1 i i list2 i Functions exist for other types Seq and Array Throws exception Groovy list collect func list1 list2 wbr wbr transpose wbr wbr collect func list1 list2 wbr wbr transpose wbr wbr collect func Haskell map i func i i list i zipWith i func i i list1 i i list2 i zipWith i n i i func i i list1 i i list2 i i n i corresponds to the number of lists predefined up to i zipWith7 i stops after the shortest list ends Haxe i array i map i func i br i list i map i func i br Lambda map i iterable i i func i J i func i i list i i list1 i i func i i list2 i i func i i list1 i i list2 i i list3 i i list4 i J s array processing abilities make operations like map implicit length error if list lengths not equal Java 8 i stream i map i func i JavaScript 1 6ECMAScript 5 i array i map i func i i List1 i map function elem1 i br return i func i elem1 i List2 i i i List1 i map function elem1 i br return i func i elem1 i List2 i i i List3 i i Array map passes 3 arguments to func the element the index of the element and the array Unused arguments can be omitted Stops at the end of List1 extending the shorter arrays with undefined items if needed Julia map i func i i list i map i func i i list1 list2 i map i func i i list1 list2 listN i ERROR DimensionMismatch Logtalk map i Closure i i List i map i Closure i i List1 i i List2 i map i Closure i i List1 i i List2 i i List3 i up to seven lists Only the Closure argument must be instantiated Failure Mathematica i func i i list i br Map i func i i list i MapThread i func i i list1 i i list2 i MapThread i func i i list1 i i list2 i Lists must be same length Maxima map i f i i expr sub 1 sub i i expr sub n sub i br maplist i f i i expr sub 1 sub i i expr sub n sub i map returns an expression which leading operator is the same as that of the expressions maplist returns a list OCaml List map i func i i list i br Array map i func i i array i List map2 i func i i list1 i i list2 i raises Invalid argument exception PARI GP apply i func i i list i Perl map i block i i list i br map i expr i i list i In block or expr special variable holds each value from list in turn Helper i List MoreUtils each array i combines more than one list until the longest one is exhausted filling the others with i undef i PHP array map i callable i i array i array map i callable i i array1 i i array2 i array map i callable i i array1 i i array2 i The number of parameters for callableshould match the number of arrays extends the shorter lists with NULL items Prolog maplist i Cont i i List1 i i List2 i maplist i Cont i i List1 i i List2 i i List3 i maplist i Cont i i List1 i i i List arguments are input output or both Subsumes also zipWith unzip all Silent failure not an error Python map i func i i list i map i func i i list1 i i list2 i map i func i i list1 i i list2 i Returns a list in Python 2 and an iterator in Python 3 i zip i and i map i 3 x stops after the shortest list ends whereas i map i 2 x and i itertools zip longest i 3 x extends the shorter lists with i None i items Ruby i enum i collect i block i br i enum i map i block i i enum1 i zip i enum2 i wbr wbr map i block i i enum1 i zip i enum2 i wbr wbr map i block i br i enum1 i i enum2 i wbr wbr transpose map i block i i enum i is an Enumeration stops at the end of the object it is called on the first list if any other list is shorter it is extended with nil items Rust i list1 i into iter map i func i i list1 i into iter zip i list2 i map i func i the Iterator map and Iterator zip methods both take ownership of the original iterator and return a new one the Iterator zip method internally calls the IntoIterator into iter method on i list2 i stops after the shorter list ends S R lapply i list i i func i mapply i func i i list1 i i list2 i mapply i func i i list1 i i list2 i Shorter lists are cycled Scala i list i map i func i i list1 i i list2 i wbr wbr zipped map i func i i list1 i i list2 i i list3 i wbr wbr zipped map i func i note more than 3 not possible stops after the shorter list ends Scheme including Guile and Racket map i func i i list i map i func i i list1 i i list2 i map i func i i list1 i i list2 i lists must all have same length SRFI 1 extends to take lists of different length Smalltalk i aCollection i collect i aBlock i i aCollection1 i with i aCollection2 i collect i aBlock i Fails Standard ML map i func i i list i ListPair map i func i i list1 i i list2 i br ListPair mapEq i func i i list1 i i list2 i For 2 argument map func takes its arguments in a tuple i ListPair map i stops after the shortest list ends whereas i ListPair mapEq i raises UnequalLengths exception Swift i sequence i map i func i zip i sequence1 i i sequence2 i map i func i stops after the shortest list ends XPath 3XQuery 3 list block for each list func for each pair list1 list2 func In block the context item holds the current value stops after the shortest list endsSee also editFunctor functional programming Zipping computer science or zip mapping list over multiple lists Filter higher order function Fold higher order function foreach loop Free monoid Functional programming Higher order function List comprehension Map parallel pattern References edit In a non strict language that permits general recursion such as Haskell this is only true if the first argument to fmap is strict Wadler Philip September 1989 Theorems for free PDF 4th International Symposium on Functional Programming Languages and Computer Architecture London Association for Computing Machinery Map fusion Making Haskell 225 faster J McCarthy K Maling S Russell N Rochester S Goldberg J Slagle LISP Programmer s Manual March April 1959 J McCarthy Symbol Manipulating Language Revisions of the Language AI Memo No 4 October 1958 Function MAPC MAPCAR MAPCAN MAPL MAPLIST MAPCON in ANSI Common Lisp Retrieved from https en wikipedia org w index php title Map higher order function amp oldid 1192307233, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.