class IterMap1: def __init__(self, seq, f): self.seq = seq self.f = f self.pos = 0 def __iter__(self): return IterMap1(self.seq, self.f) def next(self): if self.pos >= len(self.seq): raise StopIteration else: val = (self.f)(self.seq[self.pos]) self.pos = self.pos + 1 return val class IterMap2: def __init__(self, seq, f): self.seq = seq self.f = f self.iter = seq.__iter__() def __iter__(self): return IterMap2(self.seq, self.f) def next(self): return (self.f)(self.iter.next()) def quadrado(x): return x * x for i in IterMap1([1,2,3,4], quadrado): print i for i in IterMap2([1,2,3,4], quadrado): print i