Map (higher-order function): Difference between revisions

Content deleted Content added
SmackBot (talk | contribs)
m Date the maintenance tags or general fixes
No edit summary
Line 1:
In many [[programming language]]s, '''<code>map'''</code> is the name of a [[higher-order function]] that applies a [[Procedural parameter|given function]] to a sequence of elements (such as a [[linked list|list]]) and returns a sequence of results. They are examples of both [[catamorphism]]s and [[anamorphism]]s. <!-- ex, map f = foldr ((:) . f) [] -->
 
For example, if we define a function <code>square</code> as follows:
Line 5:
square x = x * x
 
Then calling (<code>map square [1,2,3,4,5])</code> will return <code>[1,4,9,16,25]</code>.
 
Map itself may be defined recursively, like this code in Haskell:
Line 13:
 
==Language comparison==
The map function is especially common in [[functional programming]] languages, but some [[high-level programming language|high-level]] [[procedural programming|procedural]] languages support it as well, and in others it may be defined. [[Common Lisp]] provides a whole family of map-like functions. The one that corresponds to the behavior described here is called <code>mapcar</code>, where the <code>-car</code> designation indicates access using the [[CAR and CDR|CAR operation]]. In [[C++]]'s [[Standard Template Library]], the map function is called <code>transform</code>. In C# (3.0), the map function is provided as an extension method called <code>selectSelect</code>. It is also frequently used as an array operation in scripting languages such as [[Perl]], [[Python (programming language)|Python]] and [[Ruby (programming language)|Ruby]]; the operation is called <code>map</code> in Perl and Python and <code>collect</code> in Ruby.
 
==Optimizations==
Line 30:
* <code>fmap (f . g) = fmap f . fmap g -- composition</code>
 
==Mapping in .NET Proceduralprocedural Languageslanguages==
The map function in .NET Proceduralprocedural languages like C# or VB.NET (as of C# 2.0 and VB 8.0{{Fact|date=June 2008}}) is supported through the <code>selectSelect</code> extension method. Here's an example that combines the use of select() with the use of [[anonymous functions]], support for which was added to C# in version 3.0 (November 2007).
 
<source lang="csharp">