123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- namespace System.Linq
- {
- public static partial class AsyncEnumerable
- {
- public static Task<TSource> Aggregate<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (accumulator == null)
- throw Error.ArgumentNull(nameof(accumulator));
- return AggregateCore(source, accumulator, CancellationToken.None);
- }
- public static Task<TSource> Aggregate<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (accumulator == null)
- throw Error.ArgumentNull(nameof(accumulator));
- return AggregateCore(source, accumulator, cancellationToken);
- }
- public static Task<TSource> Aggregate<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, Task<TSource>> accumulator)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (accumulator == null)
- throw Error.ArgumentNull(nameof(accumulator));
- return AggregateCore(source, accumulator, CancellationToken.None);
- }
- public static Task<TSource> Aggregate<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, Task<TSource>> accumulator, CancellationToken cancellationToken)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (accumulator == null)
- throw Error.ArgumentNull(nameof(accumulator));
- return AggregateCore(source, accumulator, cancellationToken);
- }
- public static Task<TAccumulate> Aggregate<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (accumulator == null)
- throw Error.ArgumentNull(nameof(accumulator));
- return AggregateCore(source, seed, accumulator, x => x, CancellationToken.None);
- }
- public static Task<TAccumulate> Aggregate<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, CancellationToken cancellationToken)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (accumulator == null)
- throw Error.ArgumentNull(nameof(accumulator));
- return AggregateCore(source, seed, accumulator, x => x, cancellationToken);
- }
- public static Task<TAccumulate> Aggregate<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, Task<TAccumulate>> accumulator)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (accumulator == null)
- throw Error.ArgumentNull(nameof(accumulator));
- return AggregateCore(source, seed, accumulator, CancellationToken.None);
- }
- public static Task<TAccumulate> Aggregate<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, Task<TAccumulate>> accumulator, CancellationToken cancellationToken)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (accumulator == null)
- throw Error.ArgumentNull(nameof(accumulator));
- return AggregateCore(source, seed, accumulator, cancellationToken);
- }
- public static Task<TResult> Aggregate<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (accumulator == null)
- throw Error.ArgumentNull(nameof(accumulator));
- if (resultSelector == null)
- throw Error.ArgumentNull(nameof(resultSelector));
- return AggregateCore(source, seed, accumulator, resultSelector, CancellationToken.None);
- }
- public static Task<TResult> Aggregate<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector, CancellationToken cancellationToken)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (accumulator == null)
- throw Error.ArgumentNull(nameof(accumulator));
- if (resultSelector == null)
- throw Error.ArgumentNull(nameof(resultSelector));
- return AggregateCore(source, seed, accumulator, resultSelector, cancellationToken);
- }
- public static Task<TResult> Aggregate<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, Task<TAccumulate>> accumulator, Func<TAccumulate, Task<TResult>> resultSelector)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (accumulator == null)
- throw Error.ArgumentNull(nameof(accumulator));
- if (resultSelector == null)
- throw Error.ArgumentNull(nameof(resultSelector));
- return AggregateCore(source, seed, accumulator, resultSelector, CancellationToken.None);
- }
- public static Task<TResult> Aggregate<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, Task<TAccumulate>> accumulator, Func<TAccumulate, Task<TResult>> resultSelector, CancellationToken cancellationToken)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (accumulator == null)
- throw Error.ArgumentNull(nameof(accumulator));
- if (resultSelector == null)
- throw Error.ArgumentNull(nameof(resultSelector));
- return AggregateCore(source, seed, accumulator, resultSelector, cancellationToken);
- }
- private static async Task<TResult> AggregateCore<TSource, TAccumulate, TResult>(IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector, CancellationToken cancellationToken)
- {
- var acc = seed;
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await e.MoveNextAsync().ConfigureAwait(false))
- {
- acc = accumulator(acc, e.Current);
- }
- }
- finally
- {
- await e.DisposeAsync().ConfigureAwait(false);
- }
- return resultSelector(acc);
- }
- private static async Task<TSource> AggregateCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken)
- {
- var first = true;
- var acc = default(TSource);
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await e.MoveNextAsync().ConfigureAwait(false))
- {
- acc = first ? e.Current : accumulator(acc, e.Current);
- first = false;
- }
- }
- finally
- {
- await e.DisposeAsync().ConfigureAwait(false);
- }
- if (first)
- throw Error.NoElements();
- return acc;
- }
- private static async Task<TResult> AggregateCore<TSource, TResult>(IAsyncEnumerable<TSource> source, TResult seed, Func<TResult, TSource, Task<TResult>> accumulator, CancellationToken cancellationToken)
- {
- var acc = seed;
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await e.MoveNextAsync().ConfigureAwait(false))
- {
- acc = await accumulator(acc, e.Current).ConfigureAwait(false);
- }
- }
- finally
- {
- await e.DisposeAsync().ConfigureAwait(false);
- }
- return acc;
- }
- private static async Task<TResult> AggregateCore<TSource, TAccumulate, TResult>(IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, Task<TAccumulate>> accumulator, Func<TAccumulate, Task<TResult>> resultSelector, CancellationToken cancellationToken)
- {
- var acc = seed;
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await e.MoveNextAsync().ConfigureAwait(false))
- {
- acc = await accumulator(acc, e.Current).ConfigureAwait(false);
- }
- }
- finally
- {
- await e.DisposeAsync().ConfigureAwait(false);
- }
- return await resultSelector(acc).ConfigureAwait(false);
- }
- private static async Task<TSource> AggregateCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, TSource, Task<TSource>> accumulator, CancellationToken cancellationToken)
- {
- var first = true;
- var acc = default(TSource);
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (await e.MoveNextAsync().ConfigureAwait(false))
- {
- acc = first ? e.Current : await accumulator(acc, e.Current).ConfigureAwait(false);
- first = false;
- }
- }
- finally
- {
- await e.DisposeAsync().ConfigureAwait(false);
- }
- if (first)
- throw Error.NoElements();
- return acc;
- }
- }
- }
|