1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <#@ template debug="false" hostspecific="false" language="C#" #>
- <#@ assembly name="System.Core" #>
- <#@ import namespace="System.Linq" #>
- <#@ import namespace="System.Text" #>
- <#@ import namespace="System.Collections.Generic" #>
- <#@ output extension=".cs" #>
- // 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
- {
- <#
- var os = new[]
- {
- new { type = "int", zero = "0" },
- new { type = "long", zero = "0L" },
- new { type = "float", zero = "0.0f" },
- new { type = "double", zero = "0.0" },
- new { type = "decimal", zero = "0m" },
- new { type = "int?", zero = "(int?)0" },
- new { type = "long?", zero = "(long?)0L" },
- new { type = "float?", zero = "(float?)0.0f" },
- new { type = "double?", zero = "(double?)0.0" },
- new { type = "decimal?", zero = "(decimal?)0m" },
- };
- foreach (var o in os)
- {
- var n = o.type.EndsWith("?") ? ".GetValueOrDefault()" : "";
- #>
- public static Task<<#=o.type#>> Sum(this IAsyncEnumerable<<#=o.type#>> source)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return source.Aggregate(<#=o.zero#>, (x, y) => x + y<#=n#>, CancellationToken.None);
- }
- public static Task<<#=o.type#>> Sum(this IAsyncEnumerable<<#=o.type#>> source, CancellationToken cancellationToken)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return source.Aggregate(<#=o.zero#>, (x, y) => x + y<#=n#>, cancellationToken);
- }
- public static Task<<#=o.type#>> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (selector == null)
- throw new ArgumentNullException(nameof(selector));
- return source.Select(selector).Sum(CancellationToken.None);
- }
- public static Task<<#=o.type#>> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (selector == null)
- throw new ArgumentNullException(nameof(selector));
- return source.Select(selector).Sum(cancellationToken);
- }
- public static Task<<#=o.type#>> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<<#=o.type#>>> selector)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (selector == null)
- throw new ArgumentNullException(nameof(selector));
- return source.Select(selector).Sum(CancellationToken.None);
- }
- public static Task<<#=o.type#>> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<<#=o.type#>>> selector, CancellationToken cancellationToken)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (selector == null)
- throw new ArgumentNullException(nameof(selector));
- return source.Select(selector).Sum(cancellationToken);
- }
- <#
- }
- #>
- }
- }
|