Function std.range.enumerate
Iterate over with an attached index variable.
range
Each element is a std.typecons.Tuple containing the index
and the element, in that order, where the index member is named index
and the element member is named value.
The index starts at and is incremented by one on every iteration.
start
Prototype
auto enumerate(Enumerator, Range)( Range range, Enumerator start = 0 ) if (isIntegral!Enumerator && isInputRange!Range);
Overflow
If has length, then it is an error to pass a value for range
so that start is bigger than start + range.lengthEnumerator.max, thus it is
ensured that overflow cannot happen.
If does not have length, and rangepopFront is called when
front.index == Enumerator.max, the index will overflow and
continue from Enumerator.min.
Examples
Useful for using foreach with an index loop variable:
import std.stdio : stdin, stdout; import std.range : enumerate; foreach (lineNum, line; stdin.byLine().enumerate(1)) stdout.writefln("line #%s: %s", lineNum, line);
Example
Can start enumeration from a negative position:
import std.array : assocArray; import std.range : enumerate; bool[int] aa = true.repeat(3).enumerate(-1).assocArray(); assert(aa[-1]); assert(aa[0]); assert(aa[1]);
Authors
Andrei Alexandrescu, David Simcha, and Jonathan M Davis. Credit for some of the ideas in building this module goes to Leonardo Maffi.