itertools icon indicating copy to clipboard operation
itertools copied to clipboard

chain_with for lazy second iterator construction

Open Ten0 opened this issue 4 years ago • 5 comments

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 avatar Sep 24 '19 12:09 Ten0

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

Ten0 avatar Oct 04 '19 12:10 Ten0

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

bb010g avatar Jul 06 '21 05:07 bb010g

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.

Philippe-Cholet avatar Feb 08 '24 11:02 Philippe-Cholet

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.)

phimuemue avatar Feb 09 '24 10:02 phimuemue

True, I'm now unsure this should be done. Your attempt of a generic lazy thing interest me related to my https://github.com/rust-itertools/itertools/issues/791#issuecomment-1822364744.

Philippe-Cholet avatar Feb 09 '24 10:02 Philippe-Cholet