std.range.chunks with only an InputRange
Seb via Digitalmars-d
digitalmars-d at puremagic.com
Fri Mar 4 10:39:42 PST 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
More information about the Digitalmars-d
mailing list