Sum.Generated.tt 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 Apache 2.0 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. #>
  35. public static Task<<#=o.type#>> SumAsync(this IAsyncEnumerable<<#=o.type#>> source, CancellationToken cancellationToken = default)
  36. {
  37. if (source == null)
  38. throw Error.ArgumentNull(nameof(source));
  39. return Core(source, cancellationToken);
  40. async Task<<#=o.type#>> Core(IAsyncEnumerable<<#=o.type#>> _source, CancellationToken _cancellationToken)
  41. {
  42. var sum = <#=o.zero#>;
  43. var e = _source.GetAsyncEnumerator(_cancellationToken);
  44. try
  45. {
  46. while (await e.MoveNextAsync().ConfigureAwait(false))
  47. {
  48. <#
  49. if (o.@checked)
  50. {
  51. #>
  52. checked
  53. {
  54. sum += e.Current<#=n#>;
  55. }
  56. <#
  57. }
  58. else
  59. {
  60. #>
  61. sum += e.Current<#=n#>;
  62. <#
  63. }
  64. #>
  65. }
  66. }
  67. finally
  68. {
  69. await e.DisposeAsync().ConfigureAwait(false);
  70. }
  71. return sum;
  72. }
  73. }
  74. public static Task<<#=o.type#>> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken = default)
  75. {
  76. if (source == null)
  77. throw Error.ArgumentNull(nameof(source));
  78. if (selector == null)
  79. throw Error.ArgumentNull(nameof(selector));
  80. return Core(source, selector, cancellationToken);
  81. async Task<<#=o.type#>> Core(IAsyncEnumerable<TSource> _source, Func<TSource, <#=o.type#>> _selector, CancellationToken _cancellationToken)
  82. {
  83. var sum = <#=o.zero#>;
  84. var e = _source.GetAsyncEnumerator(_cancellationToken);
  85. try
  86. {
  87. while (await e.MoveNextAsync().ConfigureAwait(false))
  88. {
  89. var value = _selector(e.Current);
  90. <#
  91. if (o.@checked)
  92. {
  93. #>
  94. checked
  95. {
  96. sum += value<#=n#>;
  97. }
  98. <#
  99. }
  100. else
  101. {
  102. #>
  103. sum += value<#=n#>;
  104. <#
  105. }
  106. #>
  107. }
  108. }
  109. finally
  110. {
  111. await e.DisposeAsync().ConfigureAwait(false);
  112. }
  113. return sum;
  114. }
  115. }
  116. public static Task<<#=o.type#>> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  117. {
  118. if (source == null)
  119. throw Error.ArgumentNull(nameof(source));
  120. if (selector == null)
  121. throw Error.ArgumentNull(nameof(selector));
  122. return Core(source, selector, cancellationToken);
  123. async Task<<#=o.type#>> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<<#=o.type#>>> _selector, CancellationToken _cancellationToken)
  124. {
  125. var sum = <#=o.zero#>;
  126. var e = _source.GetAsyncEnumerator(_cancellationToken);
  127. try
  128. {
  129. while (await e.MoveNextAsync().ConfigureAwait(false))
  130. {
  131. var value = await _selector(e.Current).ConfigureAwait(false);
  132. <#
  133. if (o.@checked)
  134. {
  135. #>
  136. checked
  137. {
  138. sum += value<#=n#>;
  139. }
  140. <#
  141. }
  142. else
  143. {
  144. #>
  145. sum += value<#=n#>;
  146. <#
  147. }
  148. #>
  149. }
  150. }
  151. finally
  152. {
  153. await e.DisposeAsync().ConfigureAwait(false);
  154. }
  155. return sum;
  156. }
  157. }
  158. #if !NO_DEEP_CANCELLATION
  159. public static Task<<#=o.type#>> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  160. {
  161. if (source == null)
  162. throw Error.ArgumentNull(nameof(source));
  163. if (selector == null)
  164. throw Error.ArgumentNull(nameof(selector));
  165. return Core(source, selector, cancellationToken);
  166. async Task<<#=o.type#>> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> _selector, CancellationToken _cancellationToken)
  167. {
  168. var sum = <#=o.zero#>;
  169. var e = _source.GetAsyncEnumerator(_cancellationToken);
  170. try
  171. {
  172. while (await e.MoveNextAsync().ConfigureAwait(false))
  173. {
  174. var value = await _selector(e.Current, _cancellationToken).ConfigureAwait(false);
  175. <#
  176. if (o.@checked)
  177. {
  178. #>
  179. checked
  180. {
  181. sum += value<#=n#>;
  182. }
  183. <#
  184. }
  185. else
  186. {
  187. #>
  188. sum += value<#=n#>;
  189. <#
  190. }
  191. #>
  192. }
  193. }
  194. finally
  195. {
  196. await e.DisposeAsync().ConfigureAwait(false);
  197. }
  198. return sum;
  199. }
  200. }
  201. #endif
  202. <#
  203. }
  204. #>
  205. }
  206. }