std.range.chunks for InputRanges (buffered) #4060
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hey all,
this is very vague proposal to allow
chunksto be used on transient InputRanges that can't be saved (e.g. byLine). The current implementation has (at least) one major flaw, but I wanted to put this initial version up so we can start a discussion. Therefore I would like to know where you stand on such addition (if properly done)?Moreover there a couple of things that I would like your feedback on:
chunksfromstd.stdio(that's why it is namedchunkss- for some reason the overloading doesn't work so nicely as for the normalchunksin Range)Thanks for your input,
Here's a copy of the post from the forum, to give you the motivation
http://forum.dlang.org/post/oidujhauyroygigrqddx@forum.dlang.org
tl;dr: It would be nice if
chunkswould 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.
=> 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
chunksdoesn'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