anamorphism ( ), iterate . , - , :
def ana(build, predicate):
def h(x):
if predicate(x):
return
else:
a, b = build(x)
yield a
for i in h(b):
yield i
return h
iterate ana :
def iterate(f, x):
return ana(lambda x: (x, f(x)), lambda _: False)(x)
, itertools... , . .
UPDATE: , . :
def unfold(f, x):
while True:
w, x = f(x)
yield w
:
def iterate(f, x):
return unfold(lambda y: (y, f(y)), x)