-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add take_while_inclusive
method to Iterator
#142
Comments
If |
Shouldn't we let it bake in itertools first before considering it for std? It doesn't seem immediately obvious to me how much code using for-in loops code be replaced with this, i.e. how useful it is in general.
The plan is to fix the underlying method resolution issues so we don't have to coordinate between crates anymore. |
Assuming you're referring to this guy. Doesn't look like a lot of progress has been made on the impl, but if we're okay with it blocking this kind of addition, I'm happy to drop this in favor of the itertools pr and maybe revisit when that's finished. @jswrenn what do you think? |
If I may add my opinion, I would expect this method to be called |
For the past few years, Itertools has had a policy (noted in its README) of not accepting additions which could also be plausibly made to I personally think this particular adapter is quite useful (particularly for parsing), and expect that it'll eventually make its way into the standard library. I don't want the presence of this adapter in Itertools to prevent its addition to |
What about adding a |
Exists in itertools, but afaict no traffic as yet on adding to std. https://docs.rs/itertools/latest/itertools/trait.Itertools.html#method.peeking_take_while |
We discussed this in the libs-api meeting today. Currently this is blocked on rust-lang/rfcs#3624 which will hopefully resolve the name resolution issues between the standard library and itertools. Once that RFC is accepted and implemented, we would like to review the entire API of itertools to see which methods are good candidates for uplifting to the standard library. As such, I will close this ACP for now. This will get re-considered when we perform the API review. |
Proposal
Problem statement
The existing
take_while
method onIterator
is useful for getting elements that satisfy a predicate. However, it has the notable drawback of removing the first element that doesn't satisfy the predicate, since it must take that element from the iterator to determine if the predicate is finished.This proposal adds a method
take_while_inclusive
that returns elements that satisfy a predicate, but also including that first element that didn't satisfy said predicate. The rest of the iterator is left unchanged and thus no elements are lost.Motivation, use-cases
Here's the use case in which I wanted this method (paraphrasing since it's closed source). I was parsing a list of parameters that can contain in itself a list of values, so I needed to conditionally take more values from an iterator to create that element.
In this case, since the first element not satisfying the predicate represents a delimiter, it should be included with the other elements. In other cases, you might need to split that last element off to add it back into the collection.
Also see the first Rust issue in which this was discussed for an example of a use case involving histogram bins: rust-lang/rust#62208
Playground link to above
Solution sketches
In the absence of this method, I initially used the
take_while_ref
method fromitertools
. However, this clones the first element that doesn't satisfy the predicate, which is less than ideal in my use case and impossible in others. My current code usespeeking_take_while
and manually adds that next element back.Links and related work
My PR to
itertools
implementing said method: rust-itertools/itertools#616First discussed addition to std in rust-lang/rust#62208
Issue for addition to
itertools
: rust-itertools/itertools#597The PR to itertools has been put on hold before investigating if this is an appropriate addition to
std
(to prevent another instance of collision likeintersperse
).The text was updated successfully, but these errors were encountered: