Skip to content

Instantly share code, notes, and snippets.

@admir-live
Last active September 6, 2023 07:19
Show Gist options
  • Save admir-live/d5428a5f036b71c058c8cea5675d12d5 to your computer and use it in GitHub Desktop.
Save admir-live/d5428a5f036b71c058c8cea5675d12d5 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
IAsyncEnumerable<int> numbers = GetNumbersAsync();
Func<int, Task<int>> asyncDoubleNumber = async n =>
{
await Task.Delay(10); // Simulate some async work
return n * 2;
};
IAsyncEnumerable<int> transformedNumbers = StreamAndTransformAsync(numbers, asyncDoubleNumber);
await foreach (var number in transformedNumbers)
{
Console.WriteLine(number);
}
}
// Asynchronous data source simulation
static async IAsyncEnumerable<int> GetNumbersAsync()
{
for (int i = 1; i <= 100; i++)
{
await Task.Delay(5); // Simulate async data retrieval
yield return i;
}
}
static async IAsyncEnumerable<T> StreamAndTransformAsync<T>(IAsyncEnumerable<T> source, Func<T, Task<T>> transformation)
{
await foreach (var item in source)
{
yield return await transformation(item);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment