Using itertools for recursive function application

后端 未结 2 1503
时光取名叫无心
时光取名叫无心 2021-02-15 17:22

I need a Python function iterate(f, x) that creates an iterator returning the values x, f(x), f(f(x)), f(f(f(x))), etc (like, e.g., Clojure\'s iterate). First of a

2条回答
  •  既然无缘
    2021-02-15 18:13

    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.

提交回复
热议问题