You do not need to know or care that std.algorithm.map
goes back beyond the fact that it is a range of the same genre as the one that went through (forward, bidirectional, random, etc.). Thus, most range-based functions. They almost always return either a new range that wraps the one that passed, or the same type of range that was passed (for example, map
does the first, find
is the last). Use auto
:
ulong[] x = [1, 2, 3]; auto y = map!"a"(x);
The range returned by map
is lazy. It does nothing until you iterate over it (then it calls this function in each subsequent front
base range). It is more efficient this way (and also allows endless ranges). The exact type of return depends on the type of range in which you traveled and is local to map
so that you cannot create it directly. You need to either use auto
to type or typeof
to get the type:
typeof(map!"a"(x)) y = map!"a"(x);
However, you usually use typeof
when you need a variable that you cannot initialize directly. auto
is almost always the way to go.
If you need to create an array from the map
result (or from any other range), use std.array.array
:
ulong[] y = array(map!"a"(x));
If you know little about ranges, you should probably read this . Unfortunately, there is currently no article on dlang.org explaining ranges, but this link is for a chapter in a book that one of the D community members wrote in Turkish and translated into English, and it covers ranges quite well.
EDIT
Walter Bright recently wrote an article about types that are local to a function, but returns a function that can also help you enlighten. They even get a cool name: Voldemort Types in D
source share