A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2001-July/016377.html below:

[Python-Dev] Iterator addition?

[Python-Dev] Iterator addition?Moshe Zadka m@moshez.org
Mon, 30 Jul 2001 11:38:45 +0300
On Mon, 30 Jul 2001, Sami Hangaslammi <shang@cc.jyu.fi> wrote:

> Since iterator objects work like sequences in several contexts, maybe they
> could support sequence-like operations such as addition. This would let
> you write
> 
>   for x in iter1 + iter2:
>       do_something(x)
> 
> instead of
> 
>   for x in iter1:
>       do_something(x)
> 
>   for x in iter2:
>       do_something(x)
> 
> or the slightly better
> 
>   for i in iter1,iter2:
>       for x in i:
>           do_something(x)

No, instead of:


class concat:

    def __init__(self, *iterators):
        self.iterators = list(iterators)

    def __iter__(self): return self

    def next(self):
        while self.iterators:
            try:
                return self.iterators[0].next() 
            except StopIteration:
                del self.iterators[0]
        else:
            raise StopIteration


for x in concat(iter1, iter2):
    do_something(x)

(Note that the first n-2 lines can be refactored. Wasn't there talk
about having an iterator module with useful stuff like that?)

-- 
gpg --keyserver keyserver.pgp.com --recv-keys 46D01BD6 54C4E1FE
Secure (inaccessible): 4BD1 7705 EEC0 260A 7F21  4817 C7FC A636 46D0 1BD6
Insecure (accessible): C5A5 A8FA CA39 AB03 10B8  F116 1713 1BCF 54C4 E1FE
Learn Python! http://www.ibiblio.org/obp/thinkCSpy



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4