Skip to content
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

chain_with for lazy second iterator construction #370

Open
Ten0 opened this issue Sep 24, 2019 · 5 comments
Open

chain_with for lazy second iterator construction #370

Ten0 opened this issue Sep 24, 2019 · 5 comments

Comments

@Ten0
Copy link
Contributor

Ten0 commented Sep 24, 2019

It might be useful to add a chain_with function to the itertools that does the same thing as chain but lazily calling the function that builds the iterator.

This would make chaining with iterators computed in another thread easier.

The workaround described here is somewhat limited because map is not an FnOnce even though the way it is used provides the guarantee it will only be used once.

@Ten0
Copy link
Contributor Author

Ten0 commented Oct 4, 2019

This is pretty much .chain(std::iter::once_with(|| ...).flatten()), but more efficient (esp. since it could specialize size_hint and the like).
https://doc.rust-lang.org/std/iter/fn.once_with.html

@bb010g
Copy link

bb010g commented Jul 6, 2021

Ran into this pattern while chaining an OnceCell<impl Iterator>; using Iterator::chain caused evaluation to happen too early.

@Philippe-Cholet
Copy link
Member

I think it has some value and I see it received some likes.
I thought I would do it myself but other parts of itertools took my attention so help wanted.

@phimuemue
Copy link
Member

I'm unsure if this is really tied to Chain (what about a lazy Interleave?) - or even to Iterator (what about other things that should be evaluated lazily). (See here for a weak attempt of a generic lazy thing.)

@Philippe-Cholet
Copy link
Member

True, I'm now unsure this should be done.
Your attempt of a generic lazy thing interest me related to my #791 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants