View source code
Display the source code in std/algorithm/iteration.d from which this page was generated on
github.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Page wiki
View or edit the community-maintained wiki page associated with this page.
Template std.algorithm.iteration.map
auto
map
(Range)(Range r) if (isInputRange!(Unqual!Range));
Implements the homonym function (also known as transform
) present
in many languages of functional flavor. The call
returns a range of which elements are obtained by applying map
!(fun)(range)fun(a)
left to right for all elements a
in range
. The original ranges are
not changed. Evaluation is done lazily.
Arguments
template map(fun...);
Functions
Function name | Description |
---|---|
map |
See Also
Example
import std.algorithm.comparison : equal; import std.range : chain; int[] arr1 = [ 1, 2, 3, 4 ]; int[] arr2 = [ 5, 6 ]; auto squares = map!(a => a * a)(chain(arr1, arr2)); assert(equal(squares, [ 1, 4, 9, 16, 25, 36 ]));
Example
Multiple functions can be passed to
. In that case, the
element type of map
is a tuple containing one element for map
each
function.
auto sums = [2, 4, 6, 8]; auto products = [1, 4, 9, 16]; size_t i = 0; foreach (result; [ 1, 2, 3, 4 ].map!("a + a", "a * a")) { assert(result[0] == sums[i]); assert(result[1] == products[i]); ++i; }
Example
You may alias
with some function(s) to a symbol and use
it separately:
map
import std.algorithm.comparison : equal; import std.conv : to; alias stringize = map!(to!string); assert(equal(stringize([ 1, 2, 3, 4 ]), [ "1", "2", "3", "4" ]));