Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #58212 > unrolled thread
| Started by | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| First post | 2013-10-31 18:35 -0400 |
| Last post | 2013-10-31 18:35 -0400 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: "Backward"-Iterator - Beginners question Terry Reedy <tjreedy@udel.edu> - 2013-10-31 18:35 -0400
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2013-10-31 18:35 -0400 |
| Subject | Re: "Backward"-Iterator - Beginners question |
| Message-ID | <mailman.1893.1383258918.18130.python-list@python.org> |
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
Back to top | Article view | comp.lang.python
csiph-web