Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Itertools for containers

Considder the following interactive example

>>> l=imap(str,xrange(1,4))
>>> list(l)
['1', '2', '3']
>>> list(l)
[]

Does anyone know if there is already an implementation somewhere out there with a version of imap (and the other itertools functions) such that the second time list(l) is executed you get the same as the first. And I don't want the regular map because building the entire output in memory can be a waste of memory if you use larger ranges.

I want something that basically does something like

class cmap:
    def __init__(self, function, *iterators):
        self._function = function
        self._iterators = iterators

    def __iter__(self):
        return itertools.imap(self._function, *self._iterators)

    def __len__(self):
        return min( map(len, self._iterators) )

But it would be a waste of time to do this manually for all itertools if someone already did this.

ps. Do you think containers are more zen then iterators since for an iterator something like

for i in iterator:
    do something

implicitly empties the iterator while a container you explicitly need to remove elements.

like image 771
M.D. Avatar asked Aug 14 '11 22:08

M.D.


2 Answers

You do not have to build such an object for each type of container. Basically, you have the following:

mkimap = lambda: imap(str,xrange(1,4))
list(mkimap())
list(mkimap())

Now you onlky need a nice wrapping object to prevent the "ugly" function calls. This could work this way:

class MultiIter(object):
    def __init__(self, f, *a, **k):
        if a or k:
            self.create = lambda: f(*a, **k)
        else: # optimize
            self.create = f
    def __iter__(self):
        return self.create()

l = MultiIter(lambda: imap(str, xrange(1,4)))
# or
l = MultiIter(imap, str, xrange(1,4))
# or even
@MultiIter
def l():
    return imap(str, xrange(1,4))

# and then
print list(l)
print list(l)

(untested, hope it works, but you should get the idea)

For your 2nd question: Iterators and containers both have their uses. You should take whatever best fits your needs.

like image 192
glglgl Avatar answered Nov 08 '22 11:11

glglgl


You may be looking for itertools.tee()

like image 1
Steve Howard Avatar answered Nov 08 '22 12:11

Steve Howard