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 12:45:16 PDT 2014


On Wednesday, 6 August 2014 at 08:00:32 UTC, Philippe Sigaud via 
Digitalmars-d-learn wrote:
>> 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.
>
> But in your case, your range is random-access, no?
Nope, splitter returns a forward range.

>
> Or else, you can always map array on the chunks...
Sure. But that would be a completely unnecessary allocation. I'm 
trying to avoid those, it's what ranges are supposed to be good 
at ;)

>
>>
>> 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.
>
> What about takeExactly?

Same problem. As long as the 'count' parameter is a runtime value 
instead of a compile time value, it's impossible to define 
opIndex efficiently. Therefore the Phobos ranges don't do it.


More information about the Digitalmars-d-learn mailing list