Operating on Iterators
Credit: Sami Hangaslammi
Problem
You need to operate on iterators (including normal sequences) with the same semantics as normal sequence operations, except that lazy evaluation is a must, because some of the iterators involved could represent unbounded sequences.
Solution
Python 2.2 iterators are easy to handle via higher-order functions,
and lazy evaluation (such as that performed by the
xrange built-in function) can be generalized. Here
are some elementary operations that include concatenating several
iterators, terminating iteration when a function becomes
false, terminating iteration after the first n
values, and returning every nth result of an
iterator:
from _ _future_ _ import generators
def itercat(*iterators):
""" Concatenate several iterators into one. """
for i in iterators:
i = iter(i)
for x in i:
yield x
def iterwhile(func, iterator):
""" Iterate for as long as func(value) returns true. """
iterator = iter(iterator)
while 1:
next = iterator.next( )
if not func(next):
raise StopIteration # or: return
yield next
def iterfirst(iterator, count=1):
""" Iterate through 'count' first values. """
iterator = iter(iterator)
for i in xrange(count):
yield iterator.next( )
def iterstep(iterator, n):
""" Iterate every nth value. """
iterator = iter(iterator)
while 1:
yield iterator.next( )
# Skip n-1 values
for dummy in range(n-1):
iterator.next( )A bit less elementary, but still generally useful, are functions that transform an iterator’s output, not just selecting ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access