// 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 SumAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); async Task Core(IAsyncEnumerable _source, CancellationToken _cancellationToken) { var sum = 0; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { checked { sum += e.Current; } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func _selector, CancellationToken _cancellationToken) { var sum = 0; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = _selector(e.Current); checked { sum += value; } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current).ConfigureAwait(false); checked { sum += value; } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #if !NO_DEEP_CANCELLATION public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false); checked { sum += value; } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #endif public static Task SumAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); async Task Core(IAsyncEnumerable _source, CancellationToken _cancellationToken) { var sum = 0L; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { checked { sum += e.Current; } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func _selector, CancellationToken _cancellationToken) { var sum = 0L; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = _selector(e.Current); checked { sum += value; } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0L; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current).ConfigureAwait(false); checked { sum += value; } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #if !NO_DEEP_CANCELLATION public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0L; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false); checked { sum += value; } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #endif public static Task SumAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); async Task Core(IAsyncEnumerable _source, CancellationToken _cancellationToken) { var sum = 0.0f; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { sum += e.Current; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func _selector, CancellationToken _cancellationToken) { var sum = 0.0f; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = _selector(e.Current); sum += value; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0.0f; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current).ConfigureAwait(false); sum += value; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #if !NO_DEEP_CANCELLATION public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0.0f; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false); sum += value; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #endif public static Task SumAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); async Task Core(IAsyncEnumerable _source, CancellationToken _cancellationToken) { var sum = 0.0; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { sum += e.Current; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func _selector, CancellationToken _cancellationToken) { var sum = 0.0; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = _selector(e.Current); sum += value; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0.0; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current).ConfigureAwait(false); sum += value; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #if !NO_DEEP_CANCELLATION public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0.0; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false); sum += value; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #endif public static Task SumAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); async Task Core(IAsyncEnumerable _source, CancellationToken _cancellationToken) { var sum = 0m; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { sum += e.Current; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func _selector, CancellationToken _cancellationToken) { var sum = 0m; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = _selector(e.Current); sum += value; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0m; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current).ConfigureAwait(false); sum += value; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #if !NO_DEEP_CANCELLATION public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0m; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false); sum += value; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #endif public static Task SumAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); async Task Core(IAsyncEnumerable _source, CancellationToken _cancellationToken) { var sum = 0; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { checked { sum += e.Current.GetValueOrDefault(); } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func _selector, CancellationToken _cancellationToken) { var sum = 0; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = _selector(e.Current); checked { sum += value.GetValueOrDefault(); } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current).ConfigureAwait(false); checked { sum += value.GetValueOrDefault(); } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #if !NO_DEEP_CANCELLATION public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false); checked { sum += value.GetValueOrDefault(); } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #endif public static Task SumAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); async Task Core(IAsyncEnumerable _source, CancellationToken _cancellationToken) { var sum = 0L; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { checked { sum += e.Current.GetValueOrDefault(); } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func _selector, CancellationToken _cancellationToken) { var sum = 0L; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = _selector(e.Current); checked { sum += value.GetValueOrDefault(); } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0L; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current).ConfigureAwait(false); checked { sum += value.GetValueOrDefault(); } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #if !NO_DEEP_CANCELLATION public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0L; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false); checked { sum += value.GetValueOrDefault(); } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #endif public static Task SumAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); async Task Core(IAsyncEnumerable _source, CancellationToken _cancellationToken) { var sum = 0.0f; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { sum += e.Current.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func _selector, CancellationToken _cancellationToken) { var sum = 0.0f; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = _selector(e.Current); sum += value.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0.0f; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current).ConfigureAwait(false); sum += value.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #if !NO_DEEP_CANCELLATION public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0.0f; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false); sum += value.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #endif public static Task SumAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); async Task Core(IAsyncEnumerable _source, CancellationToken _cancellationToken) { var sum = 0.0; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { sum += e.Current.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func _selector, CancellationToken _cancellationToken) { var sum = 0.0; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = _selector(e.Current); sum += value.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0.0; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current).ConfigureAwait(false); sum += value.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #if !NO_DEEP_CANCELLATION public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0.0; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false); sum += value.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #endif public static Task SumAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); async Task Core(IAsyncEnumerable _source, CancellationToken _cancellationToken) { var sum = 0m; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { sum += e.Current.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func _selector, CancellationToken _cancellationToken) { var sum = 0m; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = _selector(e.Current); sum += value.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0m; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current).ConfigureAwait(false); sum += value.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #if !NO_DEEP_CANCELLATION public static Task SumAsync(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); async Task Core(IAsyncEnumerable _source, Func> _selector, CancellationToken _cancellationToken) { var sum = 0m; var e = _source.GetAsyncEnumerator(_cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false); sum += value.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } #endif } }