-
Notifications
You must be signed in to change notification settings - Fork 315
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 split_unfused()
to Itertools.
#883
Conversation
It's actually working!
Hmm, I'm not sure how I feel about having a bunch of fancy "store stuff" logic for this.
How often do you need the "sometimes allocates, sometimes doesn't" part? Because if allocating is fine, might as well just collect the parts up-front, I'd think... At a meta level, it's not clear to me that itertools should have support for unfused iterators, since they're rather acanonical, and I don't know that we'd want to implicitly encourage them. |
Thanks for your consideration and the I think the sometimes allocates, sometimes not is purely trying to respect the out of order iterator like I'm happy to retract this pull request in light of the meta consideration. Maybe it can go in a deviant iterators crate unfusedtools or something. Thanks for the great crate! |
BTW, I'm just peanut gallery here, so wait for a real maintainer to respond before worrying about retracting :) |
I'm no peanut gallery (he is a VIP for me really) but the new kid (latest maintainer). I have on my TODO list (low priority) to make an issue about fusedness to clarify the situation for our iterators. One reference for me is #55. I'm not particularly fond of using unfused iterators after it yielded |
I started putting this functionality into a thing called frayed. |
Here is the API for the proposed addition.
API doc
Split an unfused iterator into a group of iterators.
Given an "unfused iterator"---that is an iterator contains elements after
next()
returnsNone
---thesplit_unfused()
function will provide eachNone
-terminated sequence as its own iterator. Iterators can be dropped and consumed out of order just like.group_by()
. If consumed in order, no memory is allocated.When the "unfused iterator" returns
None
twice consecutively, it is considered exhausted.Motivation
For some reason I can often write a unfused “None-terminated” iterator that represents multiple sequences. But they're unconventional and unwieldly.
split_unfused()
uses a lot of the machinery fromchunk_by()
to turn an unfused iterator into an iterator of iterators.How's the name? The only other name I thought of was
fray()
but that seems not descriptive enough.Tests
I added four tests as well.