Sum.Generated.cs 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. /// <summary>
  12. /// Computes the sum of a sequence of <see cref="int" /> values.
  13. /// </summary>
  14. /// <param name="source">A sequence of <see cref="int" /> values to calculate the sum of.</param>
  15. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  16. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  17. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  18. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  19. public static ValueTask<int> SumAsync(this IAsyncEnumerable<int> source, CancellationToken cancellationToken = default)
  20. {
  21. if (source == null)
  22. throw Error.ArgumentNull(nameof(source));
  23. return Core(source, cancellationToken);
  24. static async ValueTask<int> Core(IAsyncEnumerable<int> source, CancellationToken cancellationToken)
  25. {
  26. var sum = 0;
  27. await foreach (int value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  28. {
  29. checked
  30. {
  31. sum += value;
  32. }
  33. }
  34. return sum;
  35. }
  36. }
  37. /// <summary>
  38. /// Computes the sum of a sequence of <see cref="int" /> values that are obtained by invoking a transform function on each element of the input sequence.
  39. /// </summary>
  40. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  41. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  42. /// <param name="selector">A transform function to apply to each element.</param>
  43. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  44. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  45. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  46. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  47. public static ValueTask<int> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken = default)
  48. {
  49. if (source == null)
  50. throw Error.ArgumentNull(nameof(source));
  51. if (selector == null)
  52. throw Error.ArgumentNull(nameof(selector));
  53. return Core(source, selector, cancellationToken);
  54. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  55. {
  56. var sum = 0;
  57. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  58. {
  59. var value = selector(item);
  60. checked
  61. {
  62. sum += value;
  63. }
  64. }
  65. return sum;
  66. }
  67. }
  68. /// <summary>
  69. /// Computes the sum of a sequence of <see cref="int"/> values that are obtained by invoking a transform function on each element of the source sequence and awaiting the result.
  70. /// </summary>
  71. /// <typeparam name="TSource">The type of elements in the source sequence.</typeparam>
  72. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  73. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  74. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  75. /// <returns>A ValueTask containing the sum of the values in the source sequence.</returns>
  76. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is <see langword="null"/>.</exception>
  77. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  78. [GenerateAsyncOverload]
  79. private static ValueTask<int> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int>> selector, CancellationToken cancellationToken = default)
  80. {
  81. if (source == null)
  82. throw Error.ArgumentNull(nameof(source));
  83. if (selector == null)
  84. throw Error.ArgumentNull(nameof(selector));
  85. return Core(source, selector, cancellationToken);
  86. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int>> selector, CancellationToken cancellationToken)
  87. {
  88. var sum = 0;
  89. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  90. {
  91. var value = await selector(item).ConfigureAwait(false);
  92. checked
  93. {
  94. sum += value;
  95. }
  96. }
  97. return sum;
  98. }
  99. }
  100. #if !NO_DEEP_CANCELLATION
  101. [GenerateAsyncOverload]
  102. private static ValueTask<int> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int>> selector, CancellationToken cancellationToken = default)
  103. {
  104. if (source == null)
  105. throw Error.ArgumentNull(nameof(source));
  106. if (selector == null)
  107. throw Error.ArgumentNull(nameof(selector));
  108. return Core(source, selector, cancellationToken);
  109. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int>> selector, CancellationToken cancellationToken)
  110. {
  111. var sum = 0;
  112. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  113. {
  114. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  115. checked
  116. {
  117. sum += value;
  118. }
  119. }
  120. return sum;
  121. }
  122. }
  123. #endif
  124. /// <summary>
  125. /// Computes the sum of a sequence of <see cref="long" /> values.
  126. /// </summary>
  127. /// <param name="source">A sequence of <see cref="long" /> values to calculate the sum of.</param>
  128. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  129. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  130. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  131. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  132. public static ValueTask<long> SumAsync(this IAsyncEnumerable<long> source, CancellationToken cancellationToken = default)
  133. {
  134. if (source == null)
  135. throw Error.ArgumentNull(nameof(source));
  136. return Core(source, cancellationToken);
  137. static async ValueTask<long> Core(IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  138. {
  139. var sum = 0L;
  140. await foreach (long value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  141. {
  142. checked
  143. {
  144. sum += value;
  145. }
  146. }
  147. return sum;
  148. }
  149. }
  150. /// <summary>
  151. /// Computes the sum of a sequence of <see cref="long" /> values that are obtained by invoking a transform function on each element of the input sequence.
  152. /// </summary>
  153. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  154. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  155. /// <param name="selector">A transform function to apply to each element.</param>
  156. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  157. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  158. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  159. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  160. public static ValueTask<long> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken = default)
  161. {
  162. if (source == null)
  163. throw Error.ArgumentNull(nameof(source));
  164. if (selector == null)
  165. throw Error.ArgumentNull(nameof(selector));
  166. return Core(source, selector, cancellationToken);
  167. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  168. {
  169. var sum = 0L;
  170. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  171. {
  172. var value = selector(item);
  173. checked
  174. {
  175. sum += value;
  176. }
  177. }
  178. return sum;
  179. }
  180. }
  181. /// <summary>
  182. /// Computes the sum of a sequence of <see cref="long"/> values that are obtained by invoking a transform function on each element of the source sequence and awaiting the result.
  183. /// </summary>
  184. /// <typeparam name="TSource">The type of elements in the source sequence.</typeparam>
  185. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  186. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  187. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  188. /// <returns>A ValueTask containing the sum of the values in the source sequence.</returns>
  189. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is <see langword="null"/>.</exception>
  190. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  191. [GenerateAsyncOverload]
  192. private static ValueTask<long> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long>> selector, CancellationToken cancellationToken = default)
  193. {
  194. if (source == null)
  195. throw Error.ArgumentNull(nameof(source));
  196. if (selector == null)
  197. throw Error.ArgumentNull(nameof(selector));
  198. return Core(source, selector, cancellationToken);
  199. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long>> selector, CancellationToken cancellationToken)
  200. {
  201. var sum = 0L;
  202. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  203. {
  204. var value = await selector(item).ConfigureAwait(false);
  205. checked
  206. {
  207. sum += value;
  208. }
  209. }
  210. return sum;
  211. }
  212. }
  213. #if !NO_DEEP_CANCELLATION
  214. [GenerateAsyncOverload]
  215. private static ValueTask<long> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long>> selector, CancellationToken cancellationToken = default)
  216. {
  217. if (source == null)
  218. throw Error.ArgumentNull(nameof(source));
  219. if (selector == null)
  220. throw Error.ArgumentNull(nameof(selector));
  221. return Core(source, selector, cancellationToken);
  222. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long>> selector, CancellationToken cancellationToken)
  223. {
  224. var sum = 0L;
  225. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  226. {
  227. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  228. checked
  229. {
  230. sum += value;
  231. }
  232. }
  233. return sum;
  234. }
  235. }
  236. #endif
  237. /// <summary>
  238. /// Computes the sum of a sequence of <see cref="float" /> values.
  239. /// </summary>
  240. /// <param name="source">A sequence of <see cref="float" /> values to calculate the sum of.</param>
  241. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  242. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  243. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  244. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  245. public static ValueTask<float> SumAsync(this IAsyncEnumerable<float> source, CancellationToken cancellationToken = default)
  246. {
  247. if (source == null)
  248. throw Error.ArgumentNull(nameof(source));
  249. return Core(source, cancellationToken);
  250. static async ValueTask<float> Core(IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  251. {
  252. var sum = 0.0f;
  253. await foreach (float value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  254. {
  255. sum += value;
  256. }
  257. return sum;
  258. }
  259. }
  260. /// <summary>
  261. /// Computes the sum of a sequence of <see cref="float" /> values that are obtained by invoking a transform function on each element of the input sequence.
  262. /// </summary>
  263. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  264. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  265. /// <param name="selector">A transform function to apply to each element.</param>
  266. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  267. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  268. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  269. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  270. public static ValueTask<float> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken = default)
  271. {
  272. if (source == null)
  273. throw Error.ArgumentNull(nameof(source));
  274. if (selector == null)
  275. throw Error.ArgumentNull(nameof(selector));
  276. return Core(source, selector, cancellationToken);
  277. static async ValueTask<float> Core(IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  278. {
  279. var sum = 0.0f;
  280. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  281. {
  282. var value = selector(item);
  283. sum += value;
  284. }
  285. return sum;
  286. }
  287. }
  288. /// <summary>
  289. /// Computes the sum of a sequence of <see cref="float"/> values that are obtained by invoking a transform function on each element of the source sequence and awaiting the result.
  290. /// </summary>
  291. /// <typeparam name="TSource">The type of elements in the source sequence.</typeparam>
  292. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  293. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  294. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  295. /// <returns>A ValueTask containing the sum of the values in the source sequence.</returns>
  296. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is <see langword="null"/>.</exception>
  297. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  298. [GenerateAsyncOverload]
  299. private static ValueTask<float> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float>> selector, CancellationToken cancellationToken = default)
  300. {
  301. if (source == null)
  302. throw Error.ArgumentNull(nameof(source));
  303. if (selector == null)
  304. throw Error.ArgumentNull(nameof(selector));
  305. return Core(source, selector, cancellationToken);
  306. static async ValueTask<float> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float>> selector, CancellationToken cancellationToken)
  307. {
  308. var sum = 0.0f;
  309. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  310. {
  311. var value = await selector(item).ConfigureAwait(false);
  312. sum += value;
  313. }
  314. return sum;
  315. }
  316. }
  317. #if !NO_DEEP_CANCELLATION
  318. [GenerateAsyncOverload]
  319. private static ValueTask<float> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float>> selector, CancellationToken cancellationToken = default)
  320. {
  321. if (source == null)
  322. throw Error.ArgumentNull(nameof(source));
  323. if (selector == null)
  324. throw Error.ArgumentNull(nameof(selector));
  325. return Core(source, selector, cancellationToken);
  326. static async ValueTask<float> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float>> selector, CancellationToken cancellationToken)
  327. {
  328. var sum = 0.0f;
  329. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  330. {
  331. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  332. sum += value;
  333. }
  334. return sum;
  335. }
  336. }
  337. #endif
  338. /// <summary>
  339. /// Computes the sum of a sequence of <see cref="double" /> values.
  340. /// </summary>
  341. /// <param name="source">A sequence of <see cref="double" /> values to calculate the sum of.</param>
  342. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  343. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  344. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  345. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  346. public static ValueTask<double> SumAsync(this IAsyncEnumerable<double> source, CancellationToken cancellationToken = default)
  347. {
  348. if (source == null)
  349. throw Error.ArgumentNull(nameof(source));
  350. return Core(source, cancellationToken);
  351. static async ValueTask<double> Core(IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  352. {
  353. var sum = 0.0;
  354. await foreach (double value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  355. {
  356. sum += value;
  357. }
  358. return sum;
  359. }
  360. }
  361. /// <summary>
  362. /// Computes the sum of a sequence of <see cref="double" /> values that are obtained by invoking a transform function on each element of the input sequence.
  363. /// </summary>
  364. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  365. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  366. /// <param name="selector">A transform function to apply to each element.</param>
  367. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  368. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  369. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  370. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  371. public static ValueTask<double> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken = default)
  372. {
  373. if (source == null)
  374. throw Error.ArgumentNull(nameof(source));
  375. if (selector == null)
  376. throw Error.ArgumentNull(nameof(selector));
  377. return Core(source, selector, cancellationToken);
  378. static async ValueTask<double> Core(IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  379. {
  380. var sum = 0.0;
  381. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  382. {
  383. var value = selector(item);
  384. sum += value;
  385. }
  386. return sum;
  387. }
  388. }
  389. /// <summary>
  390. /// Computes the sum of a sequence of <see cref="double"/> values that are obtained by invoking a transform function on each element of the source sequence and awaiting the result.
  391. /// </summary>
  392. /// <typeparam name="TSource">The type of elements in the source sequence.</typeparam>
  393. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  394. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  395. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  396. /// <returns>A ValueTask containing the sum of the values in the source sequence.</returns>
  397. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is <see langword="null"/>.</exception>
  398. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  399. [GenerateAsyncOverload]
  400. private static ValueTask<double> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double>> selector, CancellationToken cancellationToken = default)
  401. {
  402. if (source == null)
  403. throw Error.ArgumentNull(nameof(source));
  404. if (selector == null)
  405. throw Error.ArgumentNull(nameof(selector));
  406. return Core(source, selector, cancellationToken);
  407. static async ValueTask<double> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double>> selector, CancellationToken cancellationToken)
  408. {
  409. var sum = 0.0;
  410. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  411. {
  412. var value = await selector(item).ConfigureAwait(false);
  413. sum += value;
  414. }
  415. return sum;
  416. }
  417. }
  418. #if !NO_DEEP_CANCELLATION
  419. [GenerateAsyncOverload]
  420. private static ValueTask<double> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double>> selector, CancellationToken cancellationToken = default)
  421. {
  422. if (source == null)
  423. throw Error.ArgumentNull(nameof(source));
  424. if (selector == null)
  425. throw Error.ArgumentNull(nameof(selector));
  426. return Core(source, selector, cancellationToken);
  427. static async ValueTask<double> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double>> selector, CancellationToken cancellationToken)
  428. {
  429. var sum = 0.0;
  430. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  431. {
  432. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  433. sum += value;
  434. }
  435. return sum;
  436. }
  437. }
  438. #endif
  439. /// <summary>
  440. /// Computes the sum of a sequence of <see cref="decimal" /> values.
  441. /// </summary>
  442. /// <param name="source">A sequence of <see cref="decimal" /> values to calculate the sum of.</param>
  443. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  444. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  445. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  446. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  447. public static ValueTask<decimal> SumAsync(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken = default)
  448. {
  449. if (source == null)
  450. throw Error.ArgumentNull(nameof(source));
  451. return Core(source, cancellationToken);
  452. static async ValueTask<decimal> Core(IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  453. {
  454. var sum = 0m;
  455. await foreach (decimal value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  456. {
  457. sum += value;
  458. }
  459. return sum;
  460. }
  461. }
  462. /// <summary>
  463. /// Computes the sum of a sequence of <see cref="decimal" /> values that are obtained by invoking a transform function on each element of the input sequence.
  464. /// </summary>
  465. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  466. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  467. /// <param name="selector">A transform function to apply to each element.</param>
  468. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  469. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  470. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  471. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  472. public static ValueTask<decimal> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken = default)
  473. {
  474. if (source == null)
  475. throw Error.ArgumentNull(nameof(source));
  476. if (selector == null)
  477. throw Error.ArgumentNull(nameof(selector));
  478. return Core(source, selector, cancellationToken);
  479. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  480. {
  481. var sum = 0m;
  482. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  483. {
  484. var value = selector(item);
  485. sum += value;
  486. }
  487. return sum;
  488. }
  489. }
  490. /// <summary>
  491. /// Computes the sum of a sequence of <see cref="decimal"/> values that are obtained by invoking a transform function on each element of the source sequence and awaiting the result.
  492. /// </summary>
  493. /// <typeparam name="TSource">The type of elements in the source sequence.</typeparam>
  494. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  495. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  496. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  497. /// <returns>A ValueTask containing the sum of the values in the source sequence.</returns>
  498. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is <see langword="null"/>.</exception>
  499. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  500. [GenerateAsyncOverload]
  501. private static ValueTask<decimal> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal>> selector, CancellationToken cancellationToken = default)
  502. {
  503. if (source == null)
  504. throw Error.ArgumentNull(nameof(source));
  505. if (selector == null)
  506. throw Error.ArgumentNull(nameof(selector));
  507. return Core(source, selector, cancellationToken);
  508. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal>> selector, CancellationToken cancellationToken)
  509. {
  510. var sum = 0m;
  511. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  512. {
  513. var value = await selector(item).ConfigureAwait(false);
  514. sum += value;
  515. }
  516. return sum;
  517. }
  518. }
  519. #if !NO_DEEP_CANCELLATION
  520. [GenerateAsyncOverload]
  521. private static ValueTask<decimal> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal>> selector, CancellationToken cancellationToken = default)
  522. {
  523. if (source == null)
  524. throw Error.ArgumentNull(nameof(source));
  525. if (selector == null)
  526. throw Error.ArgumentNull(nameof(selector));
  527. return Core(source, selector, cancellationToken);
  528. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal>> selector, CancellationToken cancellationToken)
  529. {
  530. var sum = 0m;
  531. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  532. {
  533. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  534. sum += value;
  535. }
  536. return sum;
  537. }
  538. }
  539. #endif
  540. /// <summary>
  541. /// Computes the sum of a sequence of <see cref="Nullable{Int}" /> values.
  542. /// </summary>
  543. /// <param name="source">A sequence of <see cref="Nullable{Int}" /> values to calculate the sum of.</param>
  544. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  545. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  546. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  547. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  548. public static ValueTask<int?> SumAsync(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken = default)
  549. {
  550. if (source == null)
  551. throw Error.ArgumentNull(nameof(source));
  552. return Core(source, cancellationToken);
  553. static async ValueTask<int?> Core(IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  554. {
  555. var sum = 0;
  556. await foreach (int? value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  557. {
  558. checked
  559. {
  560. sum += value.GetValueOrDefault();
  561. }
  562. }
  563. return sum;
  564. }
  565. }
  566. /// <summary>
  567. /// Computes the sum of a sequence of <see cref="Nullable{Int}" /> values that are obtained by invoking a transform function on each element of the input sequence.
  568. /// </summary>
  569. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  570. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  571. /// <param name="selector">A transform function to apply to each element.</param>
  572. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  573. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  574. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  575. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  576. public static ValueTask<int?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken = default)
  577. {
  578. if (source == null)
  579. throw Error.ArgumentNull(nameof(source));
  580. if (selector == null)
  581. throw Error.ArgumentNull(nameof(selector));
  582. return Core(source, selector, cancellationToken);
  583. static async ValueTask<int?> Core(IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  584. {
  585. var sum = 0;
  586. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  587. {
  588. var value = selector(item);
  589. checked
  590. {
  591. sum += value.GetValueOrDefault();
  592. }
  593. }
  594. return sum;
  595. }
  596. }
  597. /// <summary>
  598. /// Computes the sum of a sequence of <see cref="Nullable{Int}"/> values that are obtained by invoking a transform function on each element of the source sequence and awaiting the result.
  599. /// </summary>
  600. /// <typeparam name="TSource">The type of elements in the source sequence.</typeparam>
  601. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  602. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  603. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  604. /// <returns>A ValueTask containing the sum of the values in the source sequence.</returns>
  605. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is <see langword="null"/>.</exception>
  606. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  607. [GenerateAsyncOverload]
  608. private static ValueTask<int?> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int?>> selector, CancellationToken cancellationToken = default)
  609. {
  610. if (source == null)
  611. throw Error.ArgumentNull(nameof(source));
  612. if (selector == null)
  613. throw Error.ArgumentNull(nameof(selector));
  614. return Core(source, selector, cancellationToken);
  615. static async ValueTask<int?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int?>> selector, CancellationToken cancellationToken)
  616. {
  617. var sum = 0;
  618. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  619. {
  620. var value = await selector(item).ConfigureAwait(false);
  621. checked
  622. {
  623. sum += value.GetValueOrDefault();
  624. }
  625. }
  626. return sum;
  627. }
  628. }
  629. #if !NO_DEEP_CANCELLATION
  630. [GenerateAsyncOverload]
  631. private static ValueTask<int?> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int?>> selector, CancellationToken cancellationToken = default)
  632. {
  633. if (source == null)
  634. throw Error.ArgumentNull(nameof(source));
  635. if (selector == null)
  636. throw Error.ArgumentNull(nameof(selector));
  637. return Core(source, selector, cancellationToken);
  638. static async ValueTask<int?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int?>> selector, CancellationToken cancellationToken)
  639. {
  640. var sum = 0;
  641. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  642. {
  643. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  644. checked
  645. {
  646. sum += value.GetValueOrDefault();
  647. }
  648. }
  649. return sum;
  650. }
  651. }
  652. #endif
  653. /// <summary>
  654. /// Computes the sum of a sequence of <see cref="Nullable{Long}" /> values.
  655. /// </summary>
  656. /// <param name="source">A sequence of <see cref="Nullable{Long}" /> values to calculate the sum of.</param>
  657. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  658. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  659. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  660. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  661. public static ValueTask<long?> SumAsync(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken = default)
  662. {
  663. if (source == null)
  664. throw Error.ArgumentNull(nameof(source));
  665. return Core(source, cancellationToken);
  666. static async ValueTask<long?> Core(IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  667. {
  668. var sum = 0L;
  669. await foreach (long? value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  670. {
  671. checked
  672. {
  673. sum += value.GetValueOrDefault();
  674. }
  675. }
  676. return sum;
  677. }
  678. }
  679. /// <summary>
  680. /// Computes the sum of a sequence of <see cref="Nullable{Long}" /> values that are obtained by invoking a transform function on each element of the input sequence.
  681. /// </summary>
  682. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  683. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  684. /// <param name="selector">A transform function to apply to each element.</param>
  685. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  686. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  687. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  688. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  689. public static ValueTask<long?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken = default)
  690. {
  691. if (source == null)
  692. throw Error.ArgumentNull(nameof(source));
  693. if (selector == null)
  694. throw Error.ArgumentNull(nameof(selector));
  695. return Core(source, selector, cancellationToken);
  696. static async ValueTask<long?> Core(IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  697. {
  698. var sum = 0L;
  699. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  700. {
  701. var value = selector(item);
  702. checked
  703. {
  704. sum += value.GetValueOrDefault();
  705. }
  706. }
  707. return sum;
  708. }
  709. }
  710. /// <summary>
  711. /// Computes the sum of a sequence of <see cref="Nullable{Long}"/> values that are obtained by invoking a transform function on each element of the source sequence and awaiting the result.
  712. /// </summary>
  713. /// <typeparam name="TSource">The type of elements in the source sequence.</typeparam>
  714. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  715. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  716. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  717. /// <returns>A ValueTask containing the sum of the values in the source sequence.</returns>
  718. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is <see langword="null"/>.</exception>
  719. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  720. [GenerateAsyncOverload]
  721. private static ValueTask<long?> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long?>> selector, CancellationToken cancellationToken = default)
  722. {
  723. if (source == null)
  724. throw Error.ArgumentNull(nameof(source));
  725. if (selector == null)
  726. throw Error.ArgumentNull(nameof(selector));
  727. return Core(source, selector, cancellationToken);
  728. static async ValueTask<long?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long?>> selector, CancellationToken cancellationToken)
  729. {
  730. var sum = 0L;
  731. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  732. {
  733. var value = await selector(item).ConfigureAwait(false);
  734. checked
  735. {
  736. sum += value.GetValueOrDefault();
  737. }
  738. }
  739. return sum;
  740. }
  741. }
  742. #if !NO_DEEP_CANCELLATION
  743. [GenerateAsyncOverload]
  744. private static ValueTask<long?> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long?>> selector, CancellationToken cancellationToken = default)
  745. {
  746. if (source == null)
  747. throw Error.ArgumentNull(nameof(source));
  748. if (selector == null)
  749. throw Error.ArgumentNull(nameof(selector));
  750. return Core(source, selector, cancellationToken);
  751. static async ValueTask<long?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long?>> selector, CancellationToken cancellationToken)
  752. {
  753. var sum = 0L;
  754. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  755. {
  756. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  757. checked
  758. {
  759. sum += value.GetValueOrDefault();
  760. }
  761. }
  762. return sum;
  763. }
  764. }
  765. #endif
  766. /// <summary>
  767. /// Computes the sum of a sequence of <see cref="Nullable{Float}" /> values.
  768. /// </summary>
  769. /// <param name="source">A sequence of <see cref="Nullable{Float}" /> values to calculate the sum of.</param>
  770. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  771. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  772. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  773. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  774. public static ValueTask<float?> SumAsync(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken = default)
  775. {
  776. if (source == null)
  777. throw Error.ArgumentNull(nameof(source));
  778. return Core(source, cancellationToken);
  779. static async ValueTask<float?> Core(IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  780. {
  781. var sum = 0.0f;
  782. await foreach (float? value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  783. {
  784. sum += value.GetValueOrDefault();
  785. }
  786. return sum;
  787. }
  788. }
  789. /// <summary>
  790. /// Computes the sum of a sequence of <see cref="Nullable{Float}" /> values that are obtained by invoking a transform function on each element of the input sequence.
  791. /// </summary>
  792. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  793. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  794. /// <param name="selector">A transform function to apply to each element.</param>
  795. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  796. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  797. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  798. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  799. public static ValueTask<float?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken = default)
  800. {
  801. if (source == null)
  802. throw Error.ArgumentNull(nameof(source));
  803. if (selector == null)
  804. throw Error.ArgumentNull(nameof(selector));
  805. return Core(source, selector, cancellationToken);
  806. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  807. {
  808. var sum = 0.0f;
  809. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  810. {
  811. var value = selector(item);
  812. sum += value.GetValueOrDefault();
  813. }
  814. return sum;
  815. }
  816. }
  817. /// <summary>
  818. /// Computes the sum of a sequence of <see cref="Nullable{Float}"/> values that are obtained by invoking a transform function on each element of the source sequence and awaiting the result.
  819. /// </summary>
  820. /// <typeparam name="TSource">The type of elements in the source sequence.</typeparam>
  821. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  822. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  823. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  824. /// <returns>A ValueTask containing the sum of the values in the source sequence.</returns>
  825. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is <see langword="null"/>.</exception>
  826. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  827. [GenerateAsyncOverload]
  828. private static ValueTask<float?> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float?>> selector, CancellationToken cancellationToken = default)
  829. {
  830. if (source == null)
  831. throw Error.ArgumentNull(nameof(source));
  832. if (selector == null)
  833. throw Error.ArgumentNull(nameof(selector));
  834. return Core(source, selector, cancellationToken);
  835. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float?>> selector, CancellationToken cancellationToken)
  836. {
  837. var sum = 0.0f;
  838. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  839. {
  840. var value = await selector(item).ConfigureAwait(false);
  841. sum += value.GetValueOrDefault();
  842. }
  843. return sum;
  844. }
  845. }
  846. #if !NO_DEEP_CANCELLATION
  847. [GenerateAsyncOverload]
  848. private static ValueTask<float?> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float?>> selector, CancellationToken cancellationToken = default)
  849. {
  850. if (source == null)
  851. throw Error.ArgumentNull(nameof(source));
  852. if (selector == null)
  853. throw Error.ArgumentNull(nameof(selector));
  854. return Core(source, selector, cancellationToken);
  855. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float?>> selector, CancellationToken cancellationToken)
  856. {
  857. var sum = 0.0f;
  858. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  859. {
  860. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  861. sum += value.GetValueOrDefault();
  862. }
  863. return sum;
  864. }
  865. }
  866. #endif
  867. /// <summary>
  868. /// Computes the sum of a sequence of <see cref="Nullable{Double}" /> values.
  869. /// </summary>
  870. /// <param name="source">A sequence of <see cref="Nullable{Double}" /> values to calculate the sum of.</param>
  871. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  872. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  873. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  874. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  875. public static ValueTask<double?> SumAsync(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken = default)
  876. {
  877. if (source == null)
  878. throw Error.ArgumentNull(nameof(source));
  879. return Core(source, cancellationToken);
  880. static async ValueTask<double?> Core(IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  881. {
  882. var sum = 0.0;
  883. await foreach (double? value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  884. {
  885. sum += value.GetValueOrDefault();
  886. }
  887. return sum;
  888. }
  889. }
  890. /// <summary>
  891. /// Computes the sum of a sequence of <see cref="Nullable{Double}" /> values that are obtained by invoking a transform function on each element of the input sequence.
  892. /// </summary>
  893. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  894. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  895. /// <param name="selector">A transform function to apply to each element.</param>
  896. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  897. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  898. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  899. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  900. public static ValueTask<double?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken = default)
  901. {
  902. if (source == null)
  903. throw Error.ArgumentNull(nameof(source));
  904. if (selector == null)
  905. throw Error.ArgumentNull(nameof(selector));
  906. return Core(source, selector, cancellationToken);
  907. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  908. {
  909. var sum = 0.0;
  910. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  911. {
  912. var value = selector(item);
  913. sum += value.GetValueOrDefault();
  914. }
  915. return sum;
  916. }
  917. }
  918. /// <summary>
  919. /// Computes the sum of a sequence of <see cref="Nullable{Double}"/> values that are obtained by invoking a transform function on each element of the source sequence and awaiting the result.
  920. /// </summary>
  921. /// <typeparam name="TSource">The type of elements in the source sequence.</typeparam>
  922. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  923. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  924. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  925. /// <returns>A ValueTask containing the sum of the values in the source sequence.</returns>
  926. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is <see langword="null"/>.</exception>
  927. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  928. [GenerateAsyncOverload]
  929. private static ValueTask<double?> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double?>> selector, CancellationToken cancellationToken = default)
  930. {
  931. if (source == null)
  932. throw Error.ArgumentNull(nameof(source));
  933. if (selector == null)
  934. throw Error.ArgumentNull(nameof(selector));
  935. return Core(source, selector, cancellationToken);
  936. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double?>> selector, CancellationToken cancellationToken)
  937. {
  938. var sum = 0.0;
  939. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  940. {
  941. var value = await selector(item).ConfigureAwait(false);
  942. sum += value.GetValueOrDefault();
  943. }
  944. return sum;
  945. }
  946. }
  947. #if !NO_DEEP_CANCELLATION
  948. [GenerateAsyncOverload]
  949. private static ValueTask<double?> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double?>> selector, CancellationToken cancellationToken = default)
  950. {
  951. if (source == null)
  952. throw Error.ArgumentNull(nameof(source));
  953. if (selector == null)
  954. throw Error.ArgumentNull(nameof(selector));
  955. return Core(source, selector, cancellationToken);
  956. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double?>> selector, CancellationToken cancellationToken)
  957. {
  958. var sum = 0.0;
  959. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  960. {
  961. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  962. sum += value.GetValueOrDefault();
  963. }
  964. return sum;
  965. }
  966. }
  967. #endif
  968. /// <summary>
  969. /// Computes the sum of a sequence of <see cref="Nullable{Decimal}" /> values.
  970. /// </summary>
  971. /// <param name="source">A sequence of <see cref="Nullable{Decimal}" /> values to calculate the sum of.</param>
  972. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  973. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  974. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  975. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  976. public static ValueTask<decimal?> SumAsync(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken = default)
  977. {
  978. if (source == null)
  979. throw Error.ArgumentNull(nameof(source));
  980. return Core(source, cancellationToken);
  981. static async ValueTask<decimal?> Core(IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  982. {
  983. var sum = 0m;
  984. await foreach (decimal? value in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  985. {
  986. sum += value.GetValueOrDefault();
  987. }
  988. return sum;
  989. }
  990. }
  991. /// <summary>
  992. /// Computes the sum of a sequence of <see cref="Nullable{Decimal}" /> values that are obtained by invoking a transform function on each element of the input sequence.
  993. /// </summary>
  994. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  995. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  996. /// <param name="selector">A transform function to apply to each element.</param>
  997. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  998. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  999. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  1000. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  1001. public static ValueTask<decimal?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken = default)
  1002. {
  1003. if (source == null)
  1004. throw Error.ArgumentNull(nameof(source));
  1005. if (selector == null)
  1006. throw Error.ArgumentNull(nameof(selector));
  1007. return Core(source, selector, cancellationToken);
  1008. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  1009. {
  1010. var sum = 0m;
  1011. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  1012. {
  1013. var value = selector(item);
  1014. sum += value.GetValueOrDefault();
  1015. }
  1016. return sum;
  1017. }
  1018. }
  1019. /// <summary>
  1020. /// Computes the sum of a sequence of <see cref="Nullable{Decimal}"/> values that are obtained by invoking a transform function on each element of the source sequence and awaiting the result.
  1021. /// </summary>
  1022. /// <typeparam name="TSource">The type of elements in the source sequence.</typeparam>
  1023. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  1024. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  1025. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  1026. /// <returns>A ValueTask containing the sum of the values in the source sequence.</returns>
  1027. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is <see langword="null"/>.</exception>
  1028. /// <remarks>The return type of this operator differs from the corresponding operator on IEnumerable in order to retain asynchronous behavior.</remarks>
  1029. [GenerateAsyncOverload]
  1030. private static ValueTask<decimal?> SumAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal?>> selector, CancellationToken cancellationToken = default)
  1031. {
  1032. if (source == null)
  1033. throw Error.ArgumentNull(nameof(source));
  1034. if (selector == null)
  1035. throw Error.ArgumentNull(nameof(selector));
  1036. return Core(source, selector, cancellationToken);
  1037. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal?>> selector, CancellationToken cancellationToken)
  1038. {
  1039. var sum = 0m;
  1040. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  1041. {
  1042. var value = await selector(item).ConfigureAwait(false);
  1043. sum += value.GetValueOrDefault();
  1044. }
  1045. return sum;
  1046. }
  1047. }
  1048. #if !NO_DEEP_CANCELLATION
  1049. [GenerateAsyncOverload]
  1050. private static ValueTask<decimal?> SumAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal?>> selector, CancellationToken cancellationToken = default)
  1051. {
  1052. if (source == null)
  1053. throw Error.ArgumentNull(nameof(source));
  1054. if (selector == null)
  1055. throw Error.ArgumentNull(nameof(selector));
  1056. return Core(source, selector, cancellationToken);
  1057. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal?>> selector, CancellationToken cancellationToken)
  1058. {
  1059. var sum = 0m;
  1060. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  1061. {
  1062. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  1063. sum += value.GetValueOrDefault();
  1064. }
  1065. return sum;
  1066. }
  1067. }
  1068. #endif
  1069. }
  1070. }