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.
Function std.range.roundRobin
roundRobin(r1, r2, r3)
yields r1
, then r2
,
then r3
, after which it pops off one element from each and
continues again from r1
. For example, if two ranges are involved,
it alternately yields elements off the two ranges. roundRobin
stops after it has consumed all ranges (skipping over the ones that
finish early).
auto auto roundRobin(Rs...)
(
Rs rs
)
if (Rs .length > 1 && allSatisfy!(isInputRange, staticMap!(Unqual, Rs)));
Example
import std .algorithm .comparison : equal;
int[] a = [ 1, 2, 3 ];
int[] b = [ 10, 20, 30, 40 ];
auto r = roundRobin(a, b);
assert(equal(r, [ 1, 10, 2, 20, 3, 30, 40 ]));
Example
roundRobin can be used to create "interleave" functionality which inserts an element between each element in a range.
import std .algorithm .comparison : equal;
auto interleave(R, E)(R range, E element)
if ((isInputRange!R && hasLength!R) || isForwardRange!R)
{
static if (hasLength!R)
immutable len = range .length;
else
immutable len = range .save .walkLength;
return roundRobin(
range,
element .repeat(len - 1)
);
}
assert(interleave([1, 2, 3], 0) .equal([1, 0, 2, 0, 3]));
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
Copyright © 1999-2018 by the D Language Foundation | Page generated by ddox.