// 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 { /// /// Computes the average of an async-enumerable sequence of values. /// /// A sequence of values to calculate the average of. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values. /// is null. /// (Asynchronous) The source sequence is empty. public static ValueTask AverageAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } long sum = e.Current; long count = 1; checked { while (await e.MoveNextAsync()) { sum += e.Current; ++count; } } return (double)sum / count; } } } /// /// Computes the average of an async-enumerable sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. /// /// The type of the elements in the source sequence. /// A sequence of values to calculate the average of. /// A transform function to apply to each element. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null. /// or is null. /// (Asynchronous) The source sequence is empty. /// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior. public static ValueTask AverageAsync(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } long sum = selector(e.Current); long count = 1; checked { while (await e.MoveNextAsync()) { sum += selector(e.Current); ++count; } } return (double)sum / count; } } } internal static ValueTask AverageAwaitAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } long sum = await selector(e.Current).ConfigureAwait(false); long count = 1; checked { while (await e.MoveNextAsync()) { sum += await selector(e.Current).ConfigureAwait(false); ++count; } } return (double)sum / count; } } } #if !NO_DEEP_CANCELLATION internal static ValueTask AverageAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } long sum = await selector(e.Current, cancellationToken).ConfigureAwait(false); long count = 1; checked { while (await e.MoveNextAsync()) { sum += await selector(e.Current, cancellationToken).ConfigureAwait(false); ++count; } } return (double)sum / count; } } } #endif /// /// Computes the average of an async-enumerable sequence of values. /// /// A sequence of values to calculate the average of. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values. /// is null. /// (Asynchronous) The source sequence is empty. public static ValueTask AverageAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } long sum = e.Current; long count = 1; checked { while (await e.MoveNextAsync()) { sum += e.Current; ++count; } } return (double)sum / count; } } } /// /// Computes the average of an async-enumerable sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. /// /// The type of the elements in the source sequence. /// A sequence of values to calculate the average of. /// A transform function to apply to each element. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null. /// or is null. /// (Asynchronous) The source sequence is empty. /// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior. public static ValueTask AverageAsync(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } long sum = selector(e.Current); long count = 1; checked { while (await e.MoveNextAsync()) { sum += selector(e.Current); ++count; } } return (double)sum / count; } } } internal static ValueTask AverageAwaitAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } long sum = await selector(e.Current).ConfigureAwait(false); long count = 1; checked { while (await e.MoveNextAsync()) { sum += await selector(e.Current).ConfigureAwait(false); ++count; } } return (double)sum / count; } } } #if !NO_DEEP_CANCELLATION internal static ValueTask AverageAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } long sum = await selector(e.Current, cancellationToken).ConfigureAwait(false); long count = 1; checked { while (await e.MoveNextAsync()) { sum += await selector(e.Current, cancellationToken).ConfigureAwait(false); ++count; } } return (double)sum / count; } } } #endif /// /// Computes the average of an async-enumerable sequence of values. /// /// A sequence of values to calculate the average of. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values. /// is null. /// (Asynchronous) The source sequence is empty. public static ValueTask AverageAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } double sum = e.Current; long count = 1; checked { while (await e.MoveNextAsync()) { sum += e.Current; ++count; } } return (float)(sum / count); } } } /// /// Computes the average of an async-enumerable sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. /// /// The type of the elements in the source sequence. /// A sequence of values to calculate the average of. /// A transform function to apply to each element. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null. /// or is null. /// (Asynchronous) The source sequence is empty. /// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior. public static ValueTask AverageAsync(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } double sum = selector(e.Current); long count = 1; checked { while (await e.MoveNextAsync()) { sum += selector(e.Current); ++count; } } return (float)(sum / count); } } } internal static ValueTask AverageAwaitAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } double sum = await selector(e.Current).ConfigureAwait(false); long count = 1; checked { while (await e.MoveNextAsync()) { sum += await selector(e.Current).ConfigureAwait(false); ++count; } } return (float)(sum / count); } } } #if !NO_DEEP_CANCELLATION internal static ValueTask AverageAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } double sum = await selector(e.Current, cancellationToken).ConfigureAwait(false); long count = 1; checked { while (await e.MoveNextAsync()) { sum += await selector(e.Current, cancellationToken).ConfigureAwait(false); ++count; } } return (float)(sum / count); } } } #endif /// /// Computes the average of an async-enumerable sequence of values. /// /// A sequence of values to calculate the average of. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values. /// is null. /// (Asynchronous) The source sequence is empty. public static ValueTask AverageAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } double sum = e.Current; long count = 1; checked { while (await e.MoveNextAsync()) { sum += e.Current; ++count; } } return sum / count; } } } /// /// Computes the average of an async-enumerable sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. /// /// The type of the elements in the source sequence. /// A sequence of values to calculate the average of. /// A transform function to apply to each element. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null. /// or is null. /// (Asynchronous) The source sequence is empty. /// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior. public static ValueTask AverageAsync(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } double sum = selector(e.Current); long count = 1; checked { while (await e.MoveNextAsync()) { sum += selector(e.Current); ++count; } } return sum / count; } } } internal static ValueTask AverageAwaitAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } double sum = await selector(e.Current).ConfigureAwait(false); long count = 1; checked { while (await e.MoveNextAsync()) { sum += await selector(e.Current).ConfigureAwait(false); ++count; } } return sum / count; } } } #if !NO_DEEP_CANCELLATION internal static ValueTask AverageAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } double sum = await selector(e.Current, cancellationToken).ConfigureAwait(false); long count = 1; checked { while (await e.MoveNextAsync()) { sum += await selector(e.Current, cancellationToken).ConfigureAwait(false); ++count; } } return sum / count; } } } #endif /// /// Computes the average of an async-enumerable sequence of values. /// /// A sequence of values to calculate the average of. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values. /// is null. /// (Asynchronous) The source sequence is empty. public static ValueTask AverageAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } decimal sum = e.Current; long count = 1; checked { while (await e.MoveNextAsync()) { sum += e.Current; ++count; } } return sum / count; } } } /// /// Computes the average of an async-enumerable sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. /// /// The type of the elements in the source sequence. /// A sequence of values to calculate the average of. /// A transform function to apply to each element. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null. /// or is null. /// (Asynchronous) The source sequence is empty. /// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior. public static ValueTask AverageAsync(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } decimal sum = selector(e.Current); long count = 1; checked { while (await e.MoveNextAsync()) { sum += selector(e.Current); ++count; } } return sum / count; } } } internal static ValueTask AverageAwaitAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } decimal sum = await selector(e.Current).ConfigureAwait(false); long count = 1; checked { while (await e.MoveNextAsync()) { sum += await selector(e.Current).ConfigureAwait(false); ++count; } } return sum / count; } } } #if !NO_DEEP_CANCELLATION internal static ValueTask AverageAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (!await e.MoveNextAsync()) { throw Error.NoElements(); } decimal sum = await selector(e.Current, cancellationToken).ConfigureAwait(false); long count = 1; checked { while (await e.MoveNextAsync()) { sum += await selector(e.Current, cancellationToken).ConfigureAwait(false); ++count; } } return sum / count; } } } #endif /// /// Computes the average of an async-enumerable sequence of values. /// /// A sequence of values to calculate the average of. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values. /// is null. /// (Asynchronous) The source sequence is empty. public static ValueTask AverageAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = e.Current; if (v.HasValue) { long sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = e.Current; if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return (double)sum / count; } } } return null; } } /// /// Computes the average of an async-enumerable sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. /// /// The type of the elements in the source sequence. /// A sequence of values to calculate the average of. /// A transform function to apply to each element. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null. /// or is null. /// (Asynchronous) The source sequence is empty. /// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior. public static ValueTask AverageAsync(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = selector(e.Current); if (v.HasValue) { long sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = selector(e.Current); if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return (double)sum / count; } } } return null; } } internal static ValueTask AverageAwaitAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = await selector(e.Current).ConfigureAwait(false); if (v.HasValue) { long sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = await selector(e.Current).ConfigureAwait(false); if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return (double)sum / count; } } } return null; } } #if !NO_DEEP_CANCELLATION internal static ValueTask AverageAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = await selector(e.Current, cancellationToken).ConfigureAwait(false); if (v.HasValue) { long sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = await selector(e.Current, cancellationToken).ConfigureAwait(false); if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return (double)sum / count; } } } return null; } } #endif /// /// Computes the average of an async-enumerable sequence of values. /// /// A sequence of values to calculate the average of. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values. /// is null. /// (Asynchronous) The source sequence is empty. public static ValueTask AverageAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = e.Current; if (v.HasValue) { long sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = e.Current; if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return (double)sum / count; } } } return null; } } /// /// Computes the average of an async-enumerable sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. /// /// The type of the elements in the source sequence. /// A sequence of values to calculate the average of. /// A transform function to apply to each element. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null. /// or is null. /// (Asynchronous) The source sequence is empty. /// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior. public static ValueTask AverageAsync(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = selector(e.Current); if (v.HasValue) { long sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = selector(e.Current); if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return (double)sum / count; } } } return null; } } internal static ValueTask AverageAwaitAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = await selector(e.Current).ConfigureAwait(false); if (v.HasValue) { long sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = await selector(e.Current).ConfigureAwait(false); if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return (double)sum / count; } } } return null; } } #if !NO_DEEP_CANCELLATION internal static ValueTask AverageAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = await selector(e.Current, cancellationToken).ConfigureAwait(false); if (v.HasValue) { long sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = await selector(e.Current, cancellationToken).ConfigureAwait(false); if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return (double)sum / count; } } } return null; } } #endif /// /// Computes the average of an async-enumerable sequence of values. /// /// A sequence of values to calculate the average of. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values. /// is null. /// (Asynchronous) The source sequence is empty. public static ValueTask AverageAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = e.Current; if (v.HasValue) { double sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = e.Current; if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return (float)(sum / count); } } } return null; } } /// /// Computes the average of an async-enumerable sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. /// /// The type of the elements in the source sequence. /// A sequence of values to calculate the average of. /// A transform function to apply to each element. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null. /// or is null. /// (Asynchronous) The source sequence is empty. /// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior. public static ValueTask AverageAsync(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = selector(e.Current); if (v.HasValue) { double sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = selector(e.Current); if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return (float)(sum / count); } } } return null; } } internal static ValueTask AverageAwaitAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = await selector(e.Current).ConfigureAwait(false); if (v.HasValue) { double sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = await selector(e.Current).ConfigureAwait(false); if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return (float)(sum / count); } } } return null; } } #if !NO_DEEP_CANCELLATION internal static ValueTask AverageAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = await selector(e.Current, cancellationToken).ConfigureAwait(false); if (v.HasValue) { double sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = await selector(e.Current, cancellationToken).ConfigureAwait(false); if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return (float)(sum / count); } } } return null; } } #endif /// /// Computes the average of an async-enumerable sequence of values. /// /// A sequence of values to calculate the average of. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values. /// is null. /// (Asynchronous) The source sequence is empty. public static ValueTask AverageAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = e.Current; if (v.HasValue) { double sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = e.Current; if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return sum / count; } } } return null; } } /// /// Computes the average of an async-enumerable sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. /// /// The type of the elements in the source sequence. /// A sequence of values to calculate the average of. /// A transform function to apply to each element. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null. /// or is null. /// (Asynchronous) The source sequence is empty. /// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior. public static ValueTask AverageAsync(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = selector(e.Current); if (v.HasValue) { double sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = selector(e.Current); if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return sum / count; } } } return null; } } internal static ValueTask AverageAwaitAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = await selector(e.Current).ConfigureAwait(false); if (v.HasValue) { double sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = await selector(e.Current).ConfigureAwait(false); if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return sum / count; } } } return null; } } #if !NO_DEEP_CANCELLATION internal static ValueTask AverageAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = await selector(e.Current, cancellationToken).ConfigureAwait(false); if (v.HasValue) { double sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = await selector(e.Current, cancellationToken).ConfigureAwait(false); if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return sum / count; } } } return null; } } #endif /// /// Computes the average of an async-enumerable sequence of values. /// /// A sequence of values to calculate the average of. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values. /// is null. /// (Asynchronous) The source sequence is empty. public static ValueTask AverageAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = e.Current; if (v.HasValue) { decimal sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = e.Current; if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return sum / count; } } } return null; } } /// /// Computes the average of an async-enumerable sequence of nullable values that are obtained by invoking a transform function on each element of the input sequence. /// /// The type of the elements in the source sequence. /// A sequence of values to calculate the average of. /// A transform function to apply to each element. /// The optional cancellation token to be used for cancelling the sequence at any time. /// An async-enumerable sequence containing a single element with the average of the sequence of values, or null if the source sequence is empty or contains only values that are null. /// or is null. /// (Asynchronous) The source sequence is empty. /// The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior. public static ValueTask AverageAsync(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = selector(e.Current); if (v.HasValue) { decimal sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = selector(e.Current); if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return sum / count; } } } return null; } } internal static ValueTask AverageAwaitAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = await selector(e.Current).ConfigureAwait(false); if (v.HasValue) { decimal sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = await selector(e.Current).ConfigureAwait(false); if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return sum / count; } } } return null; } } #if !NO_DEEP_CANCELLATION internal static ValueTask AverageAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (selector == null) throw Error.ArgumentNull(nameof(selector)); return Core(source, selector, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var v = await selector(e.Current, cancellationToken).ConfigureAwait(false); if (v.HasValue) { decimal sum = v.GetValueOrDefault(); long count = 1; checked { while (await e.MoveNextAsync()) { v = await selector(e.Current, cancellationToken).ConfigureAwait(false); if (v.HasValue) { sum += v.GetValueOrDefault(); ++count; } } } return sum / count; } } } return null; } } #endif } }