feat: replace unfold with from_fn for generate command

This commit is contained in:
Himadri Bhattacharjee 2024-07-05 10:43:12 +05:30
parent d5e00c0d5d
commit db7128f9b9
No known key found for this signature in database

View File

@ -81,7 +81,8 @@ used as the next argument to the closure, otherwise generation stops.
// A type of Option<S> is used to represent state. Invocation
// will stop on None. Using Option<S> allows functions to output
// one final value before stopping.
let iter = unfold(Some(initial), move |state| {
let mut state = Some(initial);
let iter = std::iter::from_fn(move || {
let arg = state.take()?;
let (output, next_input) = match closure.run_with_value(arg) {
@ -160,7 +161,7 @@ used as the next argument to the closure, otherwise generation stops.
// We use `state` to control when to stop, not `output`. By wrapping
// it in a `Some`, we allow the generator to output `None` as a valid output
// value.
*state = next_input;
state = next_input;
Some(output)
});