itertools
itertools copied to clipboard
Feature request: Why don't implement DoubleEndedIterator for powerset?
Whilst doable, I doubt this would be straightforward. Here's a couple considerations of the top of my head:
-
powersetas currently implemented usescombinationsinternally and so likely we'd need it to implementDoubleEndedIteratoralso (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
ExactSizeIteratorfor the wrapped iterator might be a solution (or some other way to distinguish bounded/unbounded iterators?) - both
powersetandcombinationsadapters 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)?
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).