Map (higher-order function): Difference between revisions

Content deleted Content added
Mtzguido (talk | contribs)
Language comparison: revise Rust: better to use trait method into_iter rather than conventional method iter
Line 125:
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 function]]s 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 function]]s and [[currying]], <code>map</code> may be [[partial application|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, <code>map square</code> is a Haskell function which squares each element of a list.
 
{| class="wikitable" style="font-size: 85%"
Line 326:
|-
|[[Rust (programming language)|Rust]]
| <code>''list1''.into_iter().map(''func'')</code>
| <code>''list1''.iterinto_iter().zip(''list2.iter()'').map(''func'')</code>
|
| the <code>Iterator::map</code> and <code>Iterator::zip</code> methods both take ownership of the original iterator and return a new one; the <code>Iterator::zip</code> method internally calls the <code>IntoIterator::into_iter</code> method on <code>''list2''</code>
list1.iter().map(func)
| stops after the shorter list ends
|
list1.iter().zip(list2.iter()).map(func)
|
|map() returns an iterator.
|stops after the shorter list ends
|- valign="top"
| [[S (programming language)|S]]-[[R (programming language)|R]]