-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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 Iterator::map_while
#65864
Add Iterator::map_while
#65864
Conversation
r? @rkruppe (rust_highfive has picked a reviewer for you, use r? to override) |
I have wanted an operation like this in the past, but I am unsure whether it's best placed in the standard library when However, I feel like the Likewise, if we don't end up adding either one to std, then it might not make so much sense to add Personally, I have a slight preference for |
@rkruppe Thanks for your insight! I wouldn't mind adding
Good point, and this is why I'm a bit hesitant about the name. Couldn't the same argument be made with |
It may just be familarity, but at the end of the day I would like to hear some other opinions, e.g. from @rust-lang/libs members or anyone else who happens to watch this PR. Otherwise, I still lean slightly towards requesting |
Sort of dubious about adding this to std, but I think the API proposed in the PR is more consistent with the existing filter_map API than adding while_some would be. |
ping from triage: |
I would personally also lean towards |
Ok, since others seem at best on the fence about putting this in std too, I'd go with @KodrAus's suggestion of getting this into itertools first. Still, thank you for the PR, @timvermeulen! |
@KodrAus That seems to be (somewhat) against
Keeping their policy in mind, I would prefer to not add this to |
Hm, that sounds like a decent enough reason for an RFC. There is some conflict here, though, since widespread usage in the wild is one good argument for std inclusion. On the other hand, I don't expect this will ever be a very widespread method, so perhaps that will never become a relevant factor? |
This PR adds the
map_while
iterator method, that transforms elements until a transformation fails:There are different ways of explaining what
map_while
is:take_while
, but instead of just taking elements, it maps them (hence the name)filter_map
except that it stops on the firstNone
while_some
from itertools:iter.map(f).while_some()
scan
:iter.scan((), |(), x| f(x))
An alternative name could be
take_while_map
because it is totake_while
whatfilter_map
is tofilter
and whatfind_map
is tofind
, but to memap_while
is more descriptive and less confusing.I'm submitting this PR because I semi-regularly see someone ask how to do this, and they usually end up with something like
If this is considered worth adding I'll create a tracking issue. If not, I'm happy to add it to itertools instead.