Sum.Generated.tt 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 AsyncEnumerable
  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. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  40. /// <summary>
  41. /// Computes the sum of a sequence of <see cref="<#=typeStr#>" /> values.
  42. /// </summary>
  43. /// <param name="source">A sequence of <see cref="<#=typeStr#>" /> values to calculate the sum of.</param>
  44. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  45. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  46. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  47. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  48. public static ValueTask<<#=o.type#>> SumAsync(this IAsyncEnumerable<<#=o.type#>> source, CancellationToken cancellationToken = default)
  49. {
  50. if (source == null)
  51. throw Error.ArgumentNull(nameof(source));
  52. return Core(source, cancellationToken);
  53. static async ValueTask<<#=o.type#>> Core(IAsyncEnumerable<<#=o.type#>> source, CancellationToken cancellationToken)
  54. {
  55. var sum = <#=o.zero#>;
  56. await foreach (<#=o.type#> value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  57. {
  58. <#
  59. if (o.@checked)
  60. {
  61. #>
  62. checked
  63. {
  64. sum += value<#=n#>;
  65. }
  66. <#
  67. }
  68. else
  69. {
  70. #>
  71. sum += value<#=n#>;
  72. <#
  73. }
  74. #>
  75. }
  76. return sum;
  77. }
  78. }
  79. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  80. /// <summary>
  81. /// 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.
  82. /// </summary>
  83. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  84. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  85. /// <param name="selector">A transform function to apply to each element.</param>
  86. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  87. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  88. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  89. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  90. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  91. public static ValueTask<<#=o.type#>> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=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, <#=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 = selector(item);
  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. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  126. [GenerateAsyncOverload]
  127. private static ValueTask<<#=o.type#>> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  128. {
  129. if (source == null)
  130. throw Error.ArgumentNull(nameof(source));
  131. if (selector == null)
  132. throw Error.ArgumentNull(nameof(selector));
  133. return Core(source, selector, cancellationToken);
  134. static async ValueTask<<#=o.type#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
  135. {
  136. var sum = <#=o.zero#>;
  137. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  138. {
  139. var value = await selector(item).ConfigureAwait(false);
  140. <#
  141. if (o.@checked)
  142. {
  143. #>
  144. checked
  145. {
  146. sum += value<#=n#>;
  147. }
  148. <#
  149. }
  150. else
  151. {
  152. #>
  153. sum += value<#=n#>;
  154. <#
  155. }
  156. #>
  157. }
  158. return sum;
  159. }
  160. }
  161. #if !NO_DEEP_CANCELLATION
  162. [Obsolete("Use Select then SumAsync. System.Linq.Async (a community-supported library) has been replaced by the (Microsoft supported) IAsyncEnumerable LINQ in System.Linq.AsyncEnumerable, and its SumAsync method does not include the overloads that take a selector. So you should use Select to perform the projection and then use SumAsync on the resulting sequence.")]
  163. [GenerateAsyncOverload]
  164. private static ValueTask<<#=o.type#>> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  165. {
  166. if (source == null)
  167. throw Error.ArgumentNull(nameof(source));
  168. if (selector == null)
  169. throw Error.ArgumentNull(nameof(selector));
  170. return Core(source, selector, cancellationToken);
  171. static async ValueTask<<#=o.type#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
  172. {
  173. var sum = <#=o.zero#>;
  174. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  175. {
  176. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  177. <#
  178. if (o.@checked)
  179. {
  180. #>
  181. checked
  182. {
  183. sum += value<#=n#>;
  184. }
  185. <#
  186. }
  187. else
  188. {
  189. #>
  190. sum += value<#=n#>;
  191. <#
  192. }
  193. #>
  194. }
  195. return sum;
  196. }
  197. }
  198. #endif
  199. <#
  200. }
  201. #>
  202. }
  203. }