Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'class,': 0.07; 'encoded': 0.07; 'memory.': 0.07; 'utf-8': 0.07; 'function,': 0.09; 'item.': 0.09; 'method,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:question': 0.10; 'jan': 0.12; 'mostly': 0.14; 'random': 0.14; 'compute': 0.16; 'goebel': 0.16; 'prev': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'sentences.': 0.16; 'underlying': 0.16; 'appropriate': 0.16; 'so.': 0.16; 'wrote:': 0.18; 'example': 0.22; 'header:User-Agent:1': 0.23; 'bytes': 0.24; 'case.': 0.24; '(for': 0.26; 'header:X-Complaints- To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'easier': 0.31; "skip:' 10": 0.31; 'end,': 0.31; 'beginning': 0.33; 'implemented': 0.33; 'actual': 0.34; 'but': 0.35; 'there': 0.35; 'sequence': 0.36; 'next': 0.36; 'virtual': 0.37; 'too': 0.37; 'two': 0.37; 'list': 0.37; 'represent': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'easy': 0.60; 'access,': 0.60; 'free': 0.61; 'received:173': 0.61; 'simply': 0.61; 'first': 0.61; 'such': 0.63; 'relatively': 0.65; 'directions,': 0.84; 'end.': 0.84; 'forward,': 0.84; 'received:fios.verizon.net': 0.84; 'realistic': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: "Backward"-Iterator - Beginners question Date: Thu, 31 Oct 2013 18:35:03 -0400 References: <5272CBA8.3070800@fam-goebel.de> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-59-117-133.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 In-Reply-To: <5272CBA8.3070800@fam-goebel.de> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1383258918 news.xs4all.nl 15877 [2001:888:2000:d::a6]:40106 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:58212 On 10/31/2013 5:29 PM, Ulrich Goebel wrote: > I'm locking for an "iterator" type with not only the .next() method, but > with a .previous(), .first() and .last() method, so that I can through > it from the beginning or from the end, and in both directions, even > alternately (for example two steps forward, one backward, two steps > forward). You are free to write such a class, if it is appropriate for your actual use case. If you have a concrete sequence object seq with random access, there is no reason to do so. First and last are seq[0] and seq[-1]. Given 'cursor' i, prev and next are 'i-=1;seq[i]' and 'i+=1;seq[i]'. There *are* virtual sequences where first and last are known or relatively easy to compute and for which prev and next are much easier to compute than a random nth item. Note that if you start with first and mostly move forward, prev might best be implemented using a list of items already seen. The list and the prev function might be limited to the last N items seen. It you give up the last function, the underlying sequence does not even have to have a definite end. A somewhat realistic (useful) example might be the following. You have a very long sequence of bytes that represent utf-8 encoded characters. You want to view the sequence as a sequence of sentences. The sequence is too long to simply create a list of (decoded) sentences in memory. -- Terry Jan Reedy