Skip to content

Instantly share code, notes, and snippets.

@palladin
Last active July 6, 2020 12:27
Show Gist options
  • Save palladin/05f0eecd55f238c0a6fc40c10f556d62 to your computer and use it in GitHub Desktop.
Save palladin/05f0eecd55f238c0a6fc40c10f556d62 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