// 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 ValueTask AggregateAsync(this IAsyncEnumerable source, Func accumulator, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (accumulator == null) throw Error.ArgumentNull(nameof(accumulator)); return Core(source, accumulator, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func accumulator, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } var acc = e.Current; while (await e.MoveNextAsync()) { acc = accumulator(acc, e.Current); } return acc; } } } internal static ValueTask AggregateAwaitAsyncCore(this IAsyncEnumerable source, Func> accumulator, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (accumulator == null) throw Error.ArgumentNull(nameof(accumulator)); return Core(source, accumulator, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> accumulator, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } var acc = e.Current; while (await e.MoveNextAsync()) { acc = await accumulator(acc, e.Current).ConfigureAwait(false); } return acc; } } } #if !NO_DEEP_CANCELLATION internal static ValueTask AggregateAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> accumulator, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (accumulator == null) throw Error.ArgumentNull(nameof(accumulator)); return Core(source, accumulator, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> accumulator, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } var acc = e.Current; while (await e.MoveNextAsync()) { acc = await accumulator(acc, e.Current, cancellationToken).ConfigureAwait(false); } return acc; } } } #endif public static ValueTask AggregateAsync(this IAsyncEnumerable source, TAccumulate seed, Func accumulator, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (accumulator == null) throw Error.ArgumentNull(nameof(accumulator)); return Core(source, seed, accumulator, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, TAccumulate seed, Func accumulator, CancellationToken cancellationToken) { var acc = seed; await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false)) { acc = accumulator(acc, item); } return acc; } } internal static ValueTask AggregateAwaitAsyncCore(this IAsyncEnumerable source, TAccumulate seed, Func> accumulator, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (accumulator == null) throw Error.ArgumentNull(nameof(accumulator)); return Core(source, seed, accumulator, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, TAccumulate seed, Func> accumulator, CancellationToken cancellationToken) { var acc = seed; await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false)) { acc = await accumulator(acc, item).ConfigureAwait(false); } return acc; } } #if !NO_DEEP_CANCELLATION internal static ValueTask AggregateAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, TAccumulate seed, Func> accumulator, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (accumulator == null) throw Error.ArgumentNull(nameof(accumulator)); return Core(source, seed, accumulator, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, TAccumulate seed, Func> accumulator, CancellationToken cancellationToken) { var acc = seed; await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false)) { acc = await accumulator(acc, item, cancellationToken).ConfigureAwait(false); } return acc; } } #endif public static ValueTask AggregateAsync(this IAsyncEnumerable source, TAccumulate seed, Func accumulator, Func resultSelector, CancellationToken cancellationToken = default) { 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 Core(source, seed, accumulator, resultSelector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, TAccumulate seed, Func accumulator, Func resultSelector, CancellationToken cancellationToken) { var acc = seed; await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false)) { acc = accumulator(acc, item); } return resultSelector(acc); } } internal static ValueTask AggregateAwaitAsyncCore(this IAsyncEnumerable source, TAccumulate seed, Func> accumulator, Func> resultSelector, CancellationToken cancellationToken = default) { 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 Core(source, seed, accumulator, resultSelector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, TAccumulate seed, Func> accumulator, Func> resultSelector, CancellationToken cancellationToken) { var acc = seed; await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false)) { acc = await accumulator(acc, item).ConfigureAwait(false); } return await resultSelector(acc).ConfigureAwait(false); } } #if !NO_DEEP_CANCELLATION internal static ValueTask AggregateAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, TAccumulate seed, Func> accumulator, Func> resultSelector, CancellationToken cancellationToken = default) { 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 Core(source, seed, accumulator, resultSelector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, TAccumulate seed, Func> accumulator, Func> resultSelector, CancellationToken cancellationToken) { var acc = seed; await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false)) { acc = await accumulator(acc, item, cancellationToken).ConfigureAwait(false); } return await resultSelector(acc, cancellationToken).ConfigureAwait(false); } } #endif } }