Using chunks with Generator

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 14 09:50:40 PDT 2017


On Tue, Mar 14, 2017 at 12:30:13PM +0000, Era Scarecrow via Digitalmars-d-learn wrote:
> On Tuesday, 14 March 2017 at 10:00:24 UTC, Tetiana wrote:
> > Build fails with the following error:
> 
> Just looking at the Documentation, Generator is an InputRange and
> chunks requires a ForwardRange (able to use save functionality).
> 
>  So the API doesn't match up more or less.
> 
> https://dlang.org/phobos/std_concurrency.html#.Generator
> https://dlang.org/phobos/std_range.html#chunks

You probably need to cache the generated elements so that they can be
reused.

Or you could try using std.algorithm.iteration.chunkBy instead, which
*can* work with mere input ranges (that aren't necessarily forward
ranges).  Note, though, that you would only be able to evaluate each
element once, and calling popFront on the outer range will discard all
elements in the current chunk.  This is generally OK for single-pass
algorithms, but if you ever need to backtrack or process chunk elements
out-of-order, it won't work and you'll need to cache.


T

-- 
Help a man when he is in trouble and he will remember you when he is in trouble again.


More information about the Digitalmars-d-learn mailing list