Skip to content

Commit

Permalink
Merge pull request #4251 from JackStouffer/interleave
Browse files Browse the repository at this point in the history
Add informative example to roundRobin
  • Loading branch information
Hackerpilot committed Apr 28, 2016
2 parents 2175917 + 98a1793 commit e3ae496
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 e3ae496

Please sign in to comment.