itertools
is reasonably simple, it mostly (entirely?) doesn't do different things depending on the iterable/multiply-iterable/sequence-ness of its input. imap
doesn't know or care that you've passed it an iterable that happens not to be an iterator.
class MyMap(object):
def __init__(self, func, *iterables):
self.func = func
self.iterables = iterables
def __iter__(self):
return iter(itertools.imap(self.func, *self.iterables))
Or something along those lines. I haven't tested it.
It's difficult (impossible?) to do this automagically, since the Python iterator protocol doesn't tell you whether or not an iterable can be iterated more than once. You can assume that if iter(i) is i
then it can't, but I don't think you can safely assume that if iter(i) is not i
then it can.
Basically an iterable that can be iterated multiple times (analogous to what C++ calls a ForwardIterator as opposed to a mere InputIterator) is not a concept commonly demanded by Python programmers AFAIK. So I think you might have to write your own wrapper for itertools.