itertools icon indicating copy to clipboard operation
itertools copied to clipboard

Laziness in tuple_windows

Open aobatact opened this issue 3 years ago • 1 comments

I was bit surprised by that Itertools::tuple_windows is not somewhat lazy and has side effect. It is consuming the first tuple when calling Itertools::tuple_windows .

https://doc.rust-lang.org/nightly/core/iter/index.html#laziness says that adapters in Iterator is lazy and the TupleWindows has #[must_use = "iterator adaptors are lazy and do nothing unless consumed] since #227

So the question is

  • Is this intended behavior?
  • Should (all) adapters in Itertools be lazy?
  • Should the laziness be documented?

aobatact avatar Feb 07 '22 15:02 aobatact

I think this is a bug to be fixed...

aobatact avatar Oct 12 '22 09:10 aobatact

I looked for (mut text in function definitions and found multiple functions I thought as lazy to actually consume one item before we actually consume them:

CoalesceBy and Product also uses #[must_use = "iterator adaptors are lazy and do nothing unless consumed"].

Now that I think about it, I suppose we could argue that "must_use" implies that the user is supposed to consume the iterator and consume one item at definition is not a big deal since it's "just a moment" before the rest?!

Philippe-Cholet avatar Dec 06 '22 11:12 Philippe-Cholet