multirember&co

Anton Vredegoor anton.vredegoor at gmail.com
Wed Apr 18 07:36:56 EDT 2007


bearophileHUGS at lycos.com wrote:

> I have modified, simplified and (hopefully) improved Steven's code
> like this (but it may be a bit slower, because the class It is inside
> the function?):

Here is a yet more simple version, I wonder if it still does the same 
thing, whatever it is you are looking for :-)

def xsplitter(iseq, pred):

     class It(object):
         def __init__(self, fun, parity):
             self.divert = fun
             self.parity = parity
             self.queue = collections.deque()

         def append(self, item):
             self.queue.append(item)

         def __iter__(self):
             while self.queue:
                 yield self.queue.popleft()
             for item in iseq:
                 if pred(item) == self.parity:
                     yield item
                 else:
                     self.divert(item)
                     for x in self:
                         yield x

     it1 = It(lambda y: it2.append(y), True)
     it2 = It(lambda y: it1.append(y), False)
     return it1, it2

A.



More information about the Python-list mailing list