iterators and views of lists

B

Brendan Miller

I was trying to reimplement some of the c++ library of generic
algorithms in c++ in python, but I was finding that this is
problematic to do this in a generic way because there isn't any
equivalent of c++'s forward iterators, random access iterators, etc.
i.e. all python iterators are just input iterators that can't mutate
the sequence they iterate over nor move backwards or by an arbitrary
offset.

I'm wondering if anyone has done work towards creating more powerful
iterators for python, or creating some more pythonic equivalent.

In particular, I was thinking that slices are almost equivalent to a
range of random access iterators, except that they create an
unnecessary copy. If there was a version of slice that defined a
mutable view over the original list, you'd have something equivalent
to t a pair of random access iterators.

For instance, if you wanted to reverse a segment of a list in place
you would be able to do:

list = [1,2,3,4]

#pretend this is some variant of slice that produces views instead
#obviously this couldn't use the same syntax, and might be a method instead.
view = list[1:]
view.reverse()

print list
[1,4,3,2]

This doesn't really handle forward and bidirectional iterators...
which I guess would be good for algorithms that operator over disk
base datastructures...

Anyone else had similar thoughts?

Brendan
 
A

Anh Hai Trinh

I was trying to reimplement some of the c++ library of generic
algorithms in c++ in python, but I was finding that this is
problematic to do this in a generic way because there isn't any
equivalent of c++'s forward iterators, random access iterators, etc.
i.e. all python iterators are just input iterators that can't mutate
the sequence they iterate over nor move backwards or by an arbitrary
offset.

You might be interested in this library <http://pypi.python.org/pypi/
stream>.

You can easily create arbitrary "slice", for example

i = mylist >> takei(primes())

will return an iterator over the items of mylist with a prime number
index, given that primes() return an iterator over prime numbers.

I'm not familiar with forward/random-access/bidirectional iterators.
 
F

Francesco Bochicchio

You might be interested in this library <http://pypi.python.org/pypi/
stream>.

You can easily create arbitrary "slice", for example

  i = mylist >> takei(primes())

will return an iterator over the items of mylist with a prime number
index, given that primes() return an iterator over prime numbers.


Nice :)

I was starting to experiment data flow programming with python myself,
although I'm just playing with it..
I find the idea of data flow programming fascinatin, and wonder if it
can be considered a general-purpose program paradigm.

Ciao
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top