Skip to content

Commit

Permalink
Add informative example to roundRobin
Browse files Browse the repository at this point in the history
  • Loading branch information
JackStouffer committed Apr 27, 2016
1 parent 0853d4b commit 98a1793
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions std/range/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,34 @@ if (Rs.length > 1 && allSatisfy!(isInputRange, staticMap!(Unqual, Rs)))
assert(equal(r, [ 1, 10, 2, 20, 3, 30, 40 ]));
}

/**
* roundRobin can be used to create "interleave" functionality which inserts
* an element between each element in a range.
*/
unittest
{
import std.algorithm.comparison : equal;

auto interleave(R, E)(R range, E element) if (isInputRange!R)
{
static if (hasLength!R)
{
immutable len = range.length;
}
else
{
immutable len = range.walkLength;
}

return roundRobin(
range,
element.repeat(len - 1)
);
}

assert(interleave([1, 2, 3], 0).equal([1, 0, 2, 0, 3]));
}

/**
Iterates a random-access range starting from a given point and
progressively extending left and right from that point. If no initial
Expand Down

0 comments on commit 98a1793

Please sign in to comment.