On Wed, Dec 5, 2012 at 12:17 PM, Nick Mellor <thebalance...@gmail.com> wrote:
>
> takewhile mines for gold at the start of a sequence, dropwhile drops the 
> dross at the start of a sequence.

When you're using both over the same sequence and with the same
condition, it seems odd that you need to iterate over it twice.
Perhaps a partitioning iterator would be cleaner - something like
this:

def partitionwhile(predicate, iterable):
    iterable = iter(iterable)
    while True:
        val = next(iterable)
        if not predicate(val): break
        yield val
    raise StopIteration # Signal the end of Phase 1
    for val in iterable: yield val # or just "yield from iterable", I think

Only the cold hard boot of reality just stomped out the spark of an
idea. Once StopIteration has been raised, that's it, there's no
"resuming" the iterator. Is there a way around that? Is there a clean
way to say "Done for now, but next time you ask, there'll be more"?

I tested it on Python 3.2 (yeah, time I upgraded, I know).

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to