std.range.Lockstep/lockstep
- multiple declarations
Function lockstep
Iterate multiple ranges in lockstep using a foreach
loop. In contrast to
zip
it allows reference access to its elements. If only a single
range is passed in, the Lockstep
aliases itself away. If the
ranges are of different lengths and s
== StoppingPolicy
stop after the shortest range is empty. If the ranges are of different
lengths and s
== StoppingPolicy
, throw an
exception. s
may not be StoppingPolicy
, and passing this
will throw an exception.
Lockstep!Ranges lockstep(Ranges...)
(
Ranges ranges
)
if (allSatisfy!(isInputRange, Ranges));
Lockstep!Ranges lockstep(Ranges...)
(
Ranges ranges,
StoppingPolicy s
)
if (allSatisfy!(isInputRange, Ranges));
Iterating over Lockstep
in reverse and with an index is only possible
when s
== StoppingPolicy
, in order to preserve
indexes. If an attempt is made at iterating in reverse when s
==
StoppingPolicy
, an exception will be thrown.
By default StoppingPolicy
is set to StoppingPolicy
.
Limitations
The pure
, @safe
, @nogc
, or nothrow
attributes cannot be
inferred for lockstep
iteration. zip
can infer the first two due to
a different implementation.
See Also
lockstep
is similar to zip
, but zip
bundles its
elements and returns a range.
lockstep
also supports reference access.
Use zip
if you want to pass the result to a range function.
Example
auto arr1 = [1,2,3,4,5,100];
auto arr2 = [6,7,8,9,10];
foreach (ref a, b; lockstep(arr1, arr2))
{
a += b;
}
writeln(arr1); // [7, 9, 11, 13, 15, 100]
/// Lockstep also supports iterating with an index variable:
foreach (index, a, b; lockstep(arr1, arr2))
{
writeln(arr1[index]); // a
writeln(arr2[index]); // b
}
Struct Lockstep
Iterate multiple ranges in lockstep using a foreach
loop. In contrast to
zip
it allows reference access to its elements. If only a single
range is passed in, the Lockstep
aliases itself away. If the
ranges are of different lengths and s
== StoppingPolicy
stop after the shortest range is empty. If the ranges are of different
lengths and s
== StoppingPolicy
, throw an
exception. s
may not be StoppingPolicy
, and passing this
will throw an exception.
struct Lockstep(Ranges...)
if (Ranges .length > 1 && allSatisfy!(isInputRange, Ranges));
Iterating over Lockstep
in reverse and with an index is only possible
when s
== StoppingPolicy
, in order to preserve
indexes. If an attempt is made at iterating in reverse when s
==
StoppingPolicy
, an exception will be thrown.
By default StoppingPolicy
is set to StoppingPolicy
.
Constructors
Name | Description |
---|---|
this
(ranges, sp)
|
Limitations
The pure
, @safe
, @nogc
, or nothrow
attributes cannot be
inferred for lockstep
iteration. zip
can infer the first two due to
a different implementation.
See Also
lockstep
is similar to zip
, but zip
bundles its
elements and returns a range.
lockstep
also supports reference access.
Use zip
if you want to pass the result to a range function.
Example
auto arr1 = [1,2,3,4,5,100];
auto arr2 = [6,7,8,9,10];
foreach (ref a, b; lockstep(arr1, arr2))
{
a += b;
}
writeln(arr1); // [7, 9, 11, 13, 15, 100]
/// Lockstep also supports iterating with an index variable:
foreach (index, a, b; lockstep(arr1, arr2))
{
writeln(arr1[index]); // a
writeln(arr2[index]); // b
}
Authors
Andrei Alexandrescu, David Simcha, Jonathan M Davis, and Jack Stouffer. Credit for some of the ideas in building this module goes to Leonardo Maffi.