std.range.lockstep
- multiple declarations
- Function lockstep
- Struct Lockstep
Function lockstep
Iterate multiple ranges
in lockstep
using a foreach
loop. If only
a single
range is passed in, the
aliases itself away. If the
Lockstep
ranges
are of different lengths and
== s
stop after the shortest range is empty. If the StoppingPolicy.shortest
ranges
are of different
lengths and
== s
, throw an
exception. StoppingPolicy.requireSameLength
may not be s
, and passing this
will throw an exception.
StoppingPolicy.longest
By default
is set to StoppingPolicy
.
StoppingPolicy.shortest
Prototypes
Lockstep!Ranges lockstep(Ranges...)( Ranges ranges ) if (allSatisfy!(isInputRange, Ranges)); Lockstep!Ranges lockstep(Ranges...)( Ranges ranges, StoppingPolicy s ) if (allSatisfy!(isInputRange, Ranges));
BUGS
If a range does not offer lvalue access, but ref
is used in the
foreach
loop, it will be silently accepted but any modifications
to the variable will not be propagated to the underlying range.
// Lockstep
also supports iterating with an index variable:
Example
foreach(index, a, b; lockstep(arr1, arr2)) { writefln("Index %s: a = %s, b = %s", index, a, b); }
Example
auto arr1 = [1,2,3,4,5]; auto arr2 = [6,7,8,9,10]; foreach(ref a, ref b; lockstep(arr1, arr2)) { a += b; } assert(arr1 == [7,9,11,13,15]);
Struct Lockstep
Iterate multiple ranges in lockstep
using a foreach
loop. If only
a single
range is passed in, the
aliases itself away. If the
ranges are of different lengths and Lockstep
s
==
stop after the shortest range is empty. If the ranges are of different
lengths and StoppingPolicy.shortest
s
==
, throw an
exception. StoppingPolicy.requireSameLength
s
may not be
, and passing this
will throw an exception.
StoppingPolicy.longest
By default
is set to StoppingPolicy
.
StoppingPolicy.shortest
BUGS
If a range does not offer lvalue access, but ref
is used in the
foreach
loop, it will be silently accepted but any modifications
to the variable will not be propagated to the underlying range.
// Lockstep
also supports iterating with an index variable:
Example
foreach(index, a, b; lockstep(arr1, arr2)) { writefln("Index %s: a = %s, b = %s", index, a, b); }
Example
auto arr1 = [1,2,3,4,5]; auto arr2 = [6,7,8,9,10]; foreach(ref a, ref b; lockstep(arr1, arr2)) { a += b; } assert(arr1 == [7,9,11,13,15]);
Authors
Andrei Alexandrescu, David Simcha, and Jonathan M Davis. Credit for some of the ideas in building this module goes to Leonardo Maffi.