itertools icon indicating copy to clipboard operation
itertools copied to clipboard

Feature request: Why don't implement DoubleEndedIterator for powerset?

Open mhfan opened this issue 3 years ago • 1 comments

mhfan avatar Jun 05 '22 12:06 mhfan

Whilst doable, I doubt this would be straightforward. Here's a couple considerations of the top of my head:

  • powerset as currently implemented uses combinations internally and so likely we'd need it to implement DoubleEndedIterator also (this is probably the trickiest part)
  • taking a single element from the 'rear' of a powerset would require fully consuming the wrapped iterator, this would be a problem when the wrapped iterator is unbounded in length. Requiring ExactSizeIterator for the wrapped iterator might be a solution (or some other way to distinguish bounded/unbounded iterators?)
  • both powerset and combinations adapters would need quite a bit more state and branching to manage the 'rear' items, this cost would be paid by users regardless of need for double-ended behaviour unless we used separate types for the new double-ended apaters

Out of interest, do you have particular use case(s)?

willcrozi avatar Jun 18 '22 14:06 willcrozi

Thanks to willcrozi for the detailed response, I would not have answered better.

Third point explain why some (most?) iterators are not double-ended (and why I'm closing this).

About the second point, if it consumes the entire iterator (by collecting elements to a vector) then maybe it would be better to also have combinatorial methods on slices (maybe outside of itertools scope, and yes it's not only powerset/combinations).

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