View source code
Display the source code in std/range/package.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
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.

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.shortest stop after the shortest range is empty. If the ranges are of different lengths and s == StoppingPolicy.requireSameLength, throw an exception. s may not be StoppingPolicy.longest, 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.requireSameLength, in order to preserve indexes. If an attempt is made at iterating in reverse when s == StoppingPolicy.shortest, an exception will be thrown.

By default StoppingPolicy is set to StoppingPolicy.shortest.

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

zip

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.shortest stop after the shortest range is empty. If the ranges are of different lengths and s == StoppingPolicy.requireSameLength, throw an exception. s may not be StoppingPolicy.longest, 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.requireSameLength, in order to preserve indexes. If an attempt is made at iterating in reverse when s == StoppingPolicy.shortest, an exception will be thrown.

By default StoppingPolicy is set to StoppingPolicy.shortest.

Constructors

NameDescription
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

zip

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.

License

Boost License 1.0.