Using itertools for recursive function application

佐手、 提交于 2019-12-04 05:53:26

There doesn't seem to be something in itertools that does what you want, but itertools is a deep treasure chest, so I could have missed something.

Your generator code looks great. I don't know why you'd write it with accumulate unless you were playing an absurd game of code golf, or you were trying to impress Haskell snobs. Write your function so that it is readable, understandable, and maintainable. No need to be overly clever.

You can use an anamorphism (or unfold) to simplify the definition of iterate, and to use only one starting value. Here's an implementation I once used, based on a quite well-known paper:

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
            # with newer syntax: 
            # yield from h(b)
    return h

Implementing iterate with ana then looks like this:

def iterate(f, x):
    return ana(lambda x: (x, f(x)), lambda _: False)(x)

No itertools, though... And I agree that this isn't the most readable variant. In fact, it is rather cryptic.


UPDATE: There's an easier version, which even looks quite nice. It's taken from here:

def unfold(f, x):
    while True:
        w, x = f(x)
        yield w

And that gives you:

def iterate(f, x):
    return unfold(lambda y: (y, f(y)), x)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!