I’ve never needed the unfold function you describe, and it’s pretty easy to write for yourself. What would be the benefit of having it in the stdlib?
As a general point, while Python borrows a lot of ideas from functional programming (list comprehensions being a very obvious one), typical Python code is generally much more imperative than functional. For example, most Python programmers would write unfolds as loops, effectively inlining the unfold call.
And there’s a good chance they wouldn’t even know the term “unfold”. ↩︎
From my perspective it would make the story in functools more complete if both reduce and its dual unfold were there. True, unfold is not needed as often as reduce, but I already missed it in some occasions.
For me the function provided to unfold is shorter and easier to write as I only have to think about the current state and what to return next.
When I saw it correctly functools provides native implementations from _functools. So there would be a performance benefit over a manual implementation, I guess?
Other languages that include an unfold are Scala, F# and OCaml.
Good catch and yes in the direct comparison with my iterative version it’s probably easier to see. If you use a separate function instead of a lambda, both are probably comparable since the wrong use of start is closer to the beginning of the line.
The main benefits of unfold are that you can write smaller functions without explicit loops that can be tested separately. The drawbacks are that it’s not as efficient and pythonic.
Can you explain in words (not “study the implementation very carefully” or by example) what unfold does? What will the docs for this proposed function say? Assume that you are writing for people who are not familiar with Haskall or expert at functional programming idioms.
In other worlds, “unfold for dummies”, or “explain like I’m 5” if you prefer.
unfold takes two arguments 1) a function that takes a state and returns either a value together with a new state or None and 2) an initial state.
Initially, unfold applies the function to the initial state. If the function returns a value and a state it yields the value and updates the state. This process is repeated until the function returns None.
Since the function only cares about the current state, the value to return next and the new state it is usually easier to reason about. The iteration is automatically performed by unfold.