1
0

Sum.Generated.tt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <#@ template debug="false" hostspecific="false" language="C#" #>
  2. <#@ assembly name="System.Core" #>
  3. <#@ import namespace="System.Linq" #>
  4. <#@ import namespace="System.Text" #>
  5. <#@ import namespace="System.Collections.Generic" #>
  6. <#@ output extension=".cs" #>
  7. // Licensed to the .NET Foundation under one or more agreements.
  8. // The .NET Foundation licenses this file to you under the MIT License.
  9. // See the LICENSE file in the project root for more information.
  10. using System.Collections.Generic;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace System.Linq
  14. {
  15. public static partial class AsyncEnumerableEx
  16. {
  17. <#
  18. var os = new[]
  19. {
  20. new { type = "int", zero = "0", @checked = true },
  21. new { type = "long", zero = "0L", @checked = true },
  22. new { type = "float", zero = "0.0f", @checked = false },
  23. new { type = "double", zero = "0.0", @checked = false },
  24. new { type = "decimal", zero = "0m", @checked = false },
  25. new { type = "int?", zero = "0", @checked = true },
  26. new { type = "long?", zero = "0L", @checked = true },
  27. new { type = "float?", zero = "0.0f", @checked = false },
  28. new { type = "double?", zero = "0.0", @checked = false },
  29. new { type = "decimal?", zero = "0m", @checked = false },
  30. };
  31. foreach (var o in os)
  32. {
  33. var n = o.type.EndsWith("?") ? ".GetValueOrDefault()" : "";
  34. var typeStr = o.type;
  35. if (o.type.EndsWith("?")) {
  36. typeStr = "Nullable{" + o.type.Substring(0, 1).ToUpper() + o.type.Substring(1, o.type.Length - 2) + "}";
  37. }
  38. #>
  39. /// <summary>
  40. /// Computes the sum of a sequence of <see cref="<#=typeStr#>" /> values that are obtained by invoking a transform function on each element of the input sequence.
  41. /// </summary>
  42. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  43. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  44. /// <param name="selector">A transform function to apply to each element.</param>
  45. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  46. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  47. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  48. public static ValueTask<<#=o.type#>> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken = default)
  49. {
  50. if (source == null)
  51. throw Error.ArgumentNull(nameof(source));
  52. if (selector == null)
  53. throw Error.ArgumentNull(nameof(selector));
  54. return Core(source, selector, cancellationToken);
  55. static async ValueTask<<#=o.type#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken)
  56. {
  57. var sum = <#=o.zero#>;
  58. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  59. {
  60. var value = selector(item);
  61. <#
  62. if (o.@checked)
  63. {
  64. #>
  65. checked
  66. {
  67. sum += value<#=n#>;
  68. }
  69. <#
  70. }
  71. else
  72. {
  73. #>
  74. sum += value<#=n#>;
  75. <#
  76. }
  77. #>
  78. }
  79. return sum;
  80. }
  81. }
  82. /// <summary>
  83. /// Computes the sum of a sequence of <see cref="<#=typeStr#>" /> values that are obtained by invoking a transform function on each element of the input sequence.
  84. /// </summary>
  85. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  86. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  87. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  88. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  89. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  90. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  91. public static ValueTask<<#=o.type#>> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  92. {
  93. if (source == null)
  94. throw Error.ArgumentNull(nameof(source));
  95. if (selector == null)
  96. throw Error.ArgumentNull(nameof(selector));
  97. return Core(source, selector, cancellationToken);
  98. static async ValueTask<<#=o.type#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
  99. {
  100. var sum = <#=o.zero#>;
  101. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  102. {
  103. var value = await selector(item).ConfigureAwait(false);
  104. <#
  105. if (o.@checked)
  106. {
  107. #>
  108. checked
  109. {
  110. sum += value<#=n#>;
  111. }
  112. <#
  113. }
  114. else
  115. {
  116. #>
  117. sum += value<#=n#>;
  118. <#
  119. }
  120. #>
  121. }
  122. return sum;
  123. }
  124. }
  125. /// <summary>
  126. /// Computes the sum of a sequence of <see cref="<#=typeStr#>" /> values that are obtained by invoking a transform function on each element of the input sequence.
  127. /// </summary>
  128. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  129. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  130. /// <param name="selector">An asynchronous, cancellable transform function to apply to each element.</param>
  131. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  132. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  133. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  134. public static ValueTask<<#=o.type#>> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  135. {
  136. if (source == null)
  137. throw Error.ArgumentNull(nameof(source));
  138. if (selector == null)
  139. throw Error.ArgumentNull(nameof(selector));
  140. return Core(source, selector, cancellationToken);
  141. static async ValueTask<<#=o.type#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
  142. {
  143. var sum = <#=o.zero#>;
  144. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  145. {
  146. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  147. <#
  148. if (o.@checked)
  149. {
  150. #>
  151. checked
  152. {
  153. sum += value<#=n#>;
  154. }
  155. <#
  156. }
  157. else
  158. {
  159. #>
  160. sum += value<#=n#>;
  161. <#
  162. }
  163. #>
  164. }
  165. return sum;
  166. }
  167. }
  168. <#
  169. }
  170. #>
  171. }
  172. }