Sum.Generated.tt 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #if INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
  81. /// <summary>
  82. /// 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.
  83. /// </summary>
  84. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  85. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  86. /// <param name="selector">A transform function to apply to each element.</param>
  87. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  88. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  89. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  90. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  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. #endif // INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
  126. [Obsolete("Use SumAsync in System.Interactive.Async. 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. This functionality has moved to System.Interactive.Async, but the methods that take ValueTask-returning selectors are now overloads of SumAsync, because this SumAwaitAsync method did not conform to current .NET naming guidelines.")]
  127. [GenerateAsyncOverload]
  128. private static ValueTask<<#=o.type#>> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  129. {
  130. if (source == null)
  131. throw Error.ArgumentNull(nameof(source));
  132. if (selector == null)
  133. throw Error.ArgumentNull(nameof(selector));
  134. return Core(source, selector, cancellationToken);
  135. static async ValueTask<<#=o.type#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
  136. {
  137. var sum = <#=o.zero#>;
  138. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  139. {
  140. var value = await selector(item).ConfigureAwait(false);
  141. <#
  142. if (o.@checked)
  143. {
  144. #>
  145. checked
  146. {
  147. sum += value<#=n#>;
  148. }
  149. <#
  150. }
  151. else
  152. {
  153. #>
  154. sum += value<#=n#>;
  155. <#
  156. }
  157. #>
  158. }
  159. return sum;
  160. }
  161. }
  162. #if !NO_DEEP_CANCELLATION
  163. [Obsolete("Use SumAsync in System.Interactive.Async. 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. This functionality has moved to System.Interactive.Async, but the methods that take ValueTask-returning selectors are now overloads of SumAsync, because this SumAwaitWithCancellationAsync method did not conform to current .NET naming guidelines.")]
  164. [GenerateAsyncOverload]
  165. private static ValueTask<<#=o.type#>> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  166. {
  167. if (source == null)
  168. throw Error.ArgumentNull(nameof(source));
  169. if (selector == null)
  170. throw Error.ArgumentNull(nameof(selector));
  171. return Core(source, selector, cancellationToken);
  172. static async ValueTask<<#=o.type#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
  173. {
  174. var sum = <#=o.zero#>;
  175. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  176. {
  177. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  178. <#
  179. if (o.@checked)
  180. {
  181. #>
  182. checked
  183. {
  184. sum += value<#=n#>;
  185. }
  186. <#
  187. }
  188. else
  189. {
  190. #>
  191. sum += value<#=n#>;
  192. <#
  193. }
  194. #>
  195. }
  196. return sum;
  197. }
  198. }
  199. #endif
  200. <#
  201. }
  202. #>
  203. }
  204. }