[Python-ideas] Possible new itertool: comm()

Chris Angelico rosuav at gmail.com
Tue Jan 6 17:46:21 CET 2015


On Wed, Jan 7, 2015 at 3:33 AM, Ian Cordasco <graffatcolmingov at gmail.com> wrote:
> So my question is how well with this work with generators/iterators?
> Your examples use lists, but it would be impossible to use this with
> anything that isn't finite right?

I think it ought to work fine on infinite iterators, based on my
reading of the code. Obviously it would itself be infinite in that
case. With one infinite and one finite iterator, it'll eventually get
into one of the loops at the end, and forever yield from the infinite
iterator; if both are infinite, it'll never break out of the primary
loop, and continue consuming values from one or the other and yielding
tuples:

>>> def count_by(x):
    n = 0
    while True:
        n += x
        yield n
>>> it = comm(count_by(2), count_by(3))
>>> next(it)
('<', 2)
>>> next(it)
('>', 3)
>>> next(it)
('<', 4)
>>> next(it)
('=', 6)
... etc etc etc ...

ChrisA


More information about the Python-ideas mailing list