Thread overview
std.range.chunks with only an InputRange
Mar 04, 2016
Seb
Mar 14, 2016
Tamas
Mar 14, 2016
Seb
March 04, 2016
tl;dr: It would be nice if `chunks` would accept only an InputRange too.

Let me show you a common pattern that I often use:

We want to iterate over two lines in a pair and compute the difference between them, e.g.

```
1 2
2 3
```
=> 2

stdin.byLine.map!(x => x.split(' ').map!(to!int)).chunks(2)).map(x =>
  abs(x.front.sum - x.dropOne.front.sum);
);

This currently doesn't compile, because `chunks` doesn't buffer, it saves the state of the forward range. However adding support for normal Input Ranges  can easily done using a buffer - it will of course require more memory as the .save implementation, however handling of ForwardRanges won't be affected.

I have seen this has been discussed more often [2,3]. The only argument against it was in [3] the missing manpower.
However I have already written such a simple buffering [4] in the past and would be happy to wrap this up nicely and (hopefully) bring it into a "phobos" shape - any thoughts/concerns?

[1] https://issues.dlang.org/show_bug.cgi?id=15759
[2] https://issues.dlang.org/show_bug.cgi?id=6621
[3] http://forum.dlang.org/post/omkpvigktgrgbzemhhuc@forum.dlang.org
[4] https://github.com/greenify/d-itertools/blob/4afbc804e8b50c797fa206969dc3b4934911a0b9/source/splitwise.d
March 14, 2016
I just run into this. Would be nice to have this! There's no reason why chunks should only work on ForwardRanges.
March 14, 2016
On Monday, 14 March 2016 at 17:38:45 UTC, Tamas wrote:
> I just run into this. Would be nice to have this! There's no reason why chunks should only work on ForwardRanges.

I opened a PR for this: https://github.com/D-Programming-Language/phobos/pull/4060
However I guess a proper library solution would be to extend `cache` to turn an InputRange into a ForwardRange (as suggested).