Description
Hi! recently, I've been using unfold
a couple of times, and looks like it's rather tricky, at least in comparison with Kotlin's generateSequece.
I see two problems with current unfold
API:
-
There's two ways to maintain state: you can use the
initial_state
, or you can just close over some env (I think these are equivalent in power). -
There'a common case when the state and the
Item
of the iterator are the same. In this situation, you need to be careful to update the state, but return a previous value.
It seems to me that, instead of unfold
, ideally there should be a couple of function:
-
repeat_call
, for explicitly closing over some state and handling updates. -
generate<T>(first: Option<T>, step: impl FnMut(&T) -> Option<T>) -> impl Iterator<Item=T>
for the common case.
Here's an example usage, which compares unfold
and generate
:
The second one looks much clearer to me :)
Or am missing some property of generate, which makes it more powerful than repeat_call
or easier to use than generate
?