Search code examples
c#aggregatec#-8.0iasyncenumerable

How to aggregate results of an IAsyncEnumerable


I want to ask if there is a plan or exists a method to aggregate the return(s) of an IAsyncEnumerable? So given the below method why is there no terse way to aggregate its result?

public async IAsyncEnumerable<bool> GenerateAsyncEnumerable(int range)
{
     foreach (var x in Enumerable.Range(0, range))
     {
           await Task.Delay(500 * x);
           yield return x % 2 == 0;
     }
}

Current Scenario

public async Task Main()
{

    bool accum = true;
    await foreach (var item in GenerateAsyncEnumerable(3))
    {
         accum &= item;
         //have some side effects on each particular item of the sequence
    }
    Console.WriteLine($"Accumulator:{accum}");
}

Desired Scenario

I would like to aggregate the results of the IAsyncEnumerable given a custom aggregation Func

public async Main()
{

    bool result = await foreach (var item in GenerateAsyncEnumerable(3).Aggregate(true, (x, y) => x & y))
    {
        //have some side effects on each particular item of the sequence
    }
}

P.S I do not like (in the first scenario) that I have to add an extra local variable accum that collects the results of the enumerable. Have I missed something, is there some syntactic sugar that I am not aware of ?


Solution

  • You could use the AggregateAsync method, from the System.Linq.Async package:

    bool result = await GenerateAsyncEnumerable(3).AggregateAsync(true, (x, y) => x & y);
    Console.WriteLine($"Result: {result}");
    

    Output:

    Result: False