Repeat and chunks
Meta via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Oct 24 08:59:05 PDT 2016
On Monday, 24 October 2016 at 15:28:50 UTC, Saurabh Das wrote:
> The documentation of
> https://dlang.org/phobos/std_range.html#.chunks mentions
> something about evenly divisible by chunkSize – perhaps that is
> the cause of the assert fail. Not 100% sure why that's there
> though.
>
> Thanks,
> Saurabh
Yes, that's correct. This is the overload of `repeat` in question:
https://dlang.org/phobos/std_range.html#.repeat.2
Take!(Repeat!T) repeat(T)(T value, size_t n);
Repeats value exactly n times. Equivalent to take(repeat(value),
n).
Examples:
import std.algorithm : equal;
assert(equal(5.repeat(4), 5.repeat().take(4)));
The variant of repeat that takes a second argument returns a
range with a length; it is not an infinite range, unlike the
first overload of repeat. So for the OP's code:
repeat(8, 10).chunks(3).writeln();
This will throw an AssertError because 10 is not evenly divisible
by 3.
More information about the Digitalmars-d-learn
mailing list