Skip to content

Instantly share code, notes, and snippets.

@lkaczanowski
Forked from palladin/UnFold.cs
Created June 19, 2020 09:31
Show Gist options
  • Save lkaczanowski/778dd313421ef6ec968a26bf6682e9f8 to your computer and use it in GitHub Desktop.
Save lkaczanowski/778dd313421ef6ec968a26bf6682e9f8 to your computer and use it in GitHub Desktop.
IAsyncEnumerable UnFold
static async IAsyncEnumerable<T> UnFold<T, TAcc>(Func<TAcc, Task<(bool Next, IAsyncEnumerable<T> Values, TAcc Acc)>> f, TAcc seed)
{
var acc = seed;
var result = default((bool Next, IAsyncEnumerable<T> Values, TAcc Acc));
do
{
result = await f(acc);
await foreach (var value in result.Values)
yield return value;
acc = result.Acc;
} while (result.Next);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment