How to easily construct objects with multi-param constructors from lazy ranges?

Rene Zwanenburg via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 6 00:40:45 PDT 2014


On Tuesday, 5 August 2014 at 19:21:44 UTC, Philippe Sigaud via 
Digitalmars-d-learn wrote:
>> Some range which takes an at compile time known number of 
>> elements from an
>> input range and provides opIndex seems perfect to me, but as 
>> far as I know
>> there's no such thing in Phobos.
>
> There is chunks:
>
> http://dlang.org/phobos/std_range.html#chunks

Yea, but that won't work for forward ranges. It only provides 
opIndex if the underlying range provides it. Since the chunk size 
is a runtime parameter it can't implement opIndex efficiently for 
non-random access ranges.

staticChunks was a bit of a misnomer. staticTake would be a 
better name. The range would contains a static array and pops 
that number of elements from the input range. Then, opIndex can 
easily be defined.


More information about the Digitalmars-d-learn mailing list