-
Notifications
You must be signed in to change notification settings - Fork 270
Added peekn #381
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
Added peekn #381
Conversation
|
Hi @MitalAshok, thanks for the PR, and I'm sorry for my excessive delay. I like the name I'll give others a chance to chime in too. @groutr, what do you think? |
|
No worries @eriknw. That makes sense, so I added a |
|
I agree with @eriknw, Compare with the following implementation which is faster and uses noticeably less memory for large n. def peekn(n, seq):
seq = iter(seq)
peeked = tuple(take(n, seq))
return peeked, itertools.chain(peeked, seq)I would prefer the EDIT: Overall, I'm favor of having a generalized peek function. 👍 Thanks for the PR! |
|
@groutr The main problem I had with chaining a tuple was that, even if the original tuple that was chained to the iterator had fallen out of scope, the chain has a reference to an iterator, which has a reference to the tuple, so every element will be kept alive: Where the def peekn(n, seq):
seq = iter(seq)
peeked = tuple(take(n, seq))
return peeked, itertools.chain(iter(peeked), seq)You can see the problem where no memory is released until the peeked section has been completely iterated over. Where does that ~8MiB go in the The reason the |
|
@MitalAshok great job on the memory analysis! I was unaware of this behavior of itertools.chain. I wonder if this behavior is popping up in other places where chain is used in toolz. The iter trick looks good to me. @eriknw LGTM |
|
This is in! Everything about this PR is excellent. Thanks @MitalAshok! |
This function is like
toolz.itertoolz.peek, but for multiple items.