chunkBy implementation

Steven Schveighoffer schveiguy at gmail.com
Tue Sep 29 22:48:38 UTC 2020


On 9/29/20 6:08 PM, H. S. Teoh wrote:
> On Tue, Sep 29, 2020 at 05:33:35PM -0400, Steven Schveighoffer via Digitalmars-d wrote:
>> Here is the chunkBy implementation for an array: https://github.com/dlang/phobos/blob/4d7e5829cb60946fefad84d066de948e813eb6f5/std/algorithm/iteration.d#L2057
>>
>> Complete with RefCounted allocations and everything. The whole works!
> 
> I was the one who wrote the original (non-RefCounted) implementation of
> chunkBy. During PR review, however, we ran into the case of how the
> resulting range iterates the original range twice: once in the subranges
> as you .popFront through them, and once in the parent range's .popFront
> (which needs to find the end of the subrange).
> 
> To eliminate this redundancy, Andrei came up with the mothership idea:
> the subrange would keep a reference to the parent range, so that if the
> subrange has already been consumed, the parent range can just pick up
> where it left off instead of starting from the beginning of the subrange
> and scan until the next subrange.

Hm... well, what about eagerly deciding when to stop, and just returning 
a Take range with the right number of elements? Then you aren't running 
the predicates more than once per element.

In any case, I've solved my local problem, and I wanted to ask about it 
here, since it seemed so out of left field to have this bizarre 
"mothership" mechanism (I haven't seen that on any other ranges).

If something like my quick-and-dirty version was added, I'm assuming 
maybe we'd have to name it something else.

-Steve


More information about the Digitalmars-d mailing list