Sum.Generated.cs 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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 AsyncEnumerableEx
  10. {
  11. /// <summary>
  12. /// 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.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  15. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  16. /// <param name="selector">A transform function to apply to each element.</param>
  17. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  18. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  19. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  20. public static ValueTask<int> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken = default)
  21. {
  22. if (source == null)
  23. throw Error.ArgumentNull(nameof(source));
  24. if (selector == null)
  25. throw Error.ArgumentNull(nameof(selector));
  26. return Core(source, selector, cancellationToken);
  27. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  28. {
  29. var sum = 0;
  30. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  31. {
  32. var value = selector(item);
  33. checked
  34. {
  35. sum += value;
  36. }
  37. }
  38. return sum;
  39. }
  40. }
  41. /// <summary>
  42. /// 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.
  43. /// </summary>
  44. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  45. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  46. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  47. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  48. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  49. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  50. public static ValueTask<int> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int>> selector, CancellationToken cancellationToken = default)
  51. {
  52. if (source == null)
  53. throw Error.ArgumentNull(nameof(source));
  54. if (selector == null)
  55. throw Error.ArgumentNull(nameof(selector));
  56. return Core(source, selector, cancellationToken);
  57. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int>> selector, CancellationToken cancellationToken)
  58. {
  59. var sum = 0;
  60. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  61. {
  62. var value = await selector(item).ConfigureAwait(false);
  63. checked
  64. {
  65. sum += value;
  66. }
  67. }
  68. return sum;
  69. }
  70. }
  71. /// <summary>
  72. /// 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.
  73. /// </summary>
  74. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  75. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  76. /// <param name="selector">An asynchronous, cancellable transform function to apply to each element.</param>
  77. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  78. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  79. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  80. public static ValueTask<int> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int>> selector, CancellationToken cancellationToken = default)
  81. {
  82. if (source == null)
  83. throw Error.ArgumentNull(nameof(source));
  84. if (selector == null)
  85. throw Error.ArgumentNull(nameof(selector));
  86. return Core(source, selector, cancellationToken);
  87. static async ValueTask<int> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int>> selector, CancellationToken cancellationToken)
  88. {
  89. var sum = 0;
  90. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  91. {
  92. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  93. checked
  94. {
  95. sum += value;
  96. }
  97. }
  98. return sum;
  99. }
  100. }
  101. /// <summary>
  102. /// 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.
  103. /// </summary>
  104. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  105. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  106. /// <param name="selector">A transform function to apply to each element.</param>
  107. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  108. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  109. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  110. public static ValueTask<long> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken = default)
  111. {
  112. if (source == null)
  113. throw Error.ArgumentNull(nameof(source));
  114. if (selector == null)
  115. throw Error.ArgumentNull(nameof(selector));
  116. return Core(source, selector, cancellationToken);
  117. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  118. {
  119. var sum = 0L;
  120. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  121. {
  122. var value = selector(item);
  123. checked
  124. {
  125. sum += value;
  126. }
  127. }
  128. return sum;
  129. }
  130. }
  131. /// <summary>
  132. /// 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.
  133. /// </summary>
  134. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  135. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  136. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  137. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  138. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  139. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  140. public static ValueTask<long> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long>> selector, CancellationToken cancellationToken = default)
  141. {
  142. if (source == null)
  143. throw Error.ArgumentNull(nameof(source));
  144. if (selector == null)
  145. throw Error.ArgumentNull(nameof(selector));
  146. return Core(source, selector, cancellationToken);
  147. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long>> selector, CancellationToken cancellationToken)
  148. {
  149. var sum = 0L;
  150. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  151. {
  152. var value = await selector(item).ConfigureAwait(false);
  153. checked
  154. {
  155. sum += value;
  156. }
  157. }
  158. return sum;
  159. }
  160. }
  161. /// <summary>
  162. /// 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.
  163. /// </summary>
  164. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  165. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  166. /// <param name="selector">An asynchronous, cancellable transform function to apply to each element.</param>
  167. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  168. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  169. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  170. public static ValueTask<long> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long>> selector, CancellationToken cancellationToken = default)
  171. {
  172. if (source == null)
  173. throw Error.ArgumentNull(nameof(source));
  174. if (selector == null)
  175. throw Error.ArgumentNull(nameof(selector));
  176. return Core(source, selector, cancellationToken);
  177. static async ValueTask<long> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long>> selector, CancellationToken cancellationToken)
  178. {
  179. var sum = 0L;
  180. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  181. {
  182. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  183. checked
  184. {
  185. sum += value;
  186. }
  187. }
  188. return sum;
  189. }
  190. }
  191. /// <summary>
  192. /// 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.
  193. /// </summary>
  194. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  195. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  196. /// <param name="selector">A transform function to apply to each element.</param>
  197. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  198. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  199. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  200. public static ValueTask<float> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken = default)
  201. {
  202. if (source == null)
  203. throw Error.ArgumentNull(nameof(source));
  204. if (selector == null)
  205. throw Error.ArgumentNull(nameof(selector));
  206. return Core(source, selector, cancellationToken);
  207. static async ValueTask<float> Core(IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  208. {
  209. var sum = 0.0f;
  210. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  211. {
  212. var value = selector(item);
  213. sum += value;
  214. }
  215. return sum;
  216. }
  217. }
  218. /// <summary>
  219. /// 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.
  220. /// </summary>
  221. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  222. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  223. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  224. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  225. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  226. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  227. public static ValueTask<float> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float>> selector, CancellationToken cancellationToken = default)
  228. {
  229. if (source == null)
  230. throw Error.ArgumentNull(nameof(source));
  231. if (selector == null)
  232. throw Error.ArgumentNull(nameof(selector));
  233. return Core(source, selector, cancellationToken);
  234. static async ValueTask<float> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float>> selector, CancellationToken cancellationToken)
  235. {
  236. var sum = 0.0f;
  237. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  238. {
  239. var value = await selector(item).ConfigureAwait(false);
  240. sum += value;
  241. }
  242. return sum;
  243. }
  244. }
  245. /// <summary>
  246. /// 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.
  247. /// </summary>
  248. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  249. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  250. /// <param name="selector">An asynchronous, cancellable transform function to apply to each element.</param>
  251. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  252. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  253. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  254. public static ValueTask<float> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float>> selector, CancellationToken cancellationToken = default)
  255. {
  256. if (source == null)
  257. throw Error.ArgumentNull(nameof(source));
  258. if (selector == null)
  259. throw Error.ArgumentNull(nameof(selector));
  260. return Core(source, selector, cancellationToken);
  261. static async ValueTask<float> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float>> selector, CancellationToken cancellationToken)
  262. {
  263. var sum = 0.0f;
  264. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  265. {
  266. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  267. sum += value;
  268. }
  269. return sum;
  270. }
  271. }
  272. /// <summary>
  273. /// 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.
  274. /// </summary>
  275. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  276. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  277. /// <param name="selector">A transform function to apply to each element.</param>
  278. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  279. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  280. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  281. public static ValueTask<double> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken = default)
  282. {
  283. if (source == null)
  284. throw Error.ArgumentNull(nameof(source));
  285. if (selector == null)
  286. throw Error.ArgumentNull(nameof(selector));
  287. return Core(source, selector, cancellationToken);
  288. static async ValueTask<double> Core(IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  289. {
  290. var sum = 0.0;
  291. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  292. {
  293. var value = selector(item);
  294. sum += value;
  295. }
  296. return sum;
  297. }
  298. }
  299. /// <summary>
  300. /// 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.
  301. /// </summary>
  302. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  303. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  304. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  305. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  306. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  307. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  308. public static ValueTask<double> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double>> selector, CancellationToken cancellationToken = default)
  309. {
  310. if (source == null)
  311. throw Error.ArgumentNull(nameof(source));
  312. if (selector == null)
  313. throw Error.ArgumentNull(nameof(selector));
  314. return Core(source, selector, cancellationToken);
  315. static async ValueTask<double> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double>> selector, CancellationToken cancellationToken)
  316. {
  317. var sum = 0.0;
  318. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  319. {
  320. var value = await selector(item).ConfigureAwait(false);
  321. sum += value;
  322. }
  323. return sum;
  324. }
  325. }
  326. /// <summary>
  327. /// 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.
  328. /// </summary>
  329. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  330. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  331. /// <param name="selector">An asynchronous, cancellable transform function to apply to each element.</param>
  332. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  333. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  334. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  335. public static ValueTask<double> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double>> selector, CancellationToken cancellationToken = default)
  336. {
  337. if (source == null)
  338. throw Error.ArgumentNull(nameof(source));
  339. if (selector == null)
  340. throw Error.ArgumentNull(nameof(selector));
  341. return Core(source, selector, cancellationToken);
  342. static async ValueTask<double> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double>> selector, CancellationToken cancellationToken)
  343. {
  344. var sum = 0.0;
  345. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  346. {
  347. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  348. sum += value;
  349. }
  350. return sum;
  351. }
  352. }
  353. /// <summary>
  354. /// 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.
  355. /// </summary>
  356. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  357. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  358. /// <param name="selector">A transform function to apply to each element.</param>
  359. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  360. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  361. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  362. public static ValueTask<decimal> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken = default)
  363. {
  364. if (source == null)
  365. throw Error.ArgumentNull(nameof(source));
  366. if (selector == null)
  367. throw Error.ArgumentNull(nameof(selector));
  368. return Core(source, selector, cancellationToken);
  369. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  370. {
  371. var sum = 0m;
  372. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  373. {
  374. var value = selector(item);
  375. sum += value;
  376. }
  377. return sum;
  378. }
  379. }
  380. /// <summary>
  381. /// 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.
  382. /// </summary>
  383. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  384. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  385. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  386. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  387. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  388. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  389. public static ValueTask<decimal> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal>> selector, CancellationToken cancellationToken = default)
  390. {
  391. if (source == null)
  392. throw Error.ArgumentNull(nameof(source));
  393. if (selector == null)
  394. throw Error.ArgumentNull(nameof(selector));
  395. return Core(source, selector, cancellationToken);
  396. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal>> selector, CancellationToken cancellationToken)
  397. {
  398. var sum = 0m;
  399. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  400. {
  401. var value = await selector(item).ConfigureAwait(false);
  402. sum += value;
  403. }
  404. return sum;
  405. }
  406. }
  407. /// <summary>
  408. /// 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.
  409. /// </summary>
  410. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  411. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  412. /// <param name="selector">An asynchronous, cancellable transform function to apply to each element.</param>
  413. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  414. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  415. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  416. public static ValueTask<decimal> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal>> selector, CancellationToken cancellationToken = default)
  417. {
  418. if (source == null)
  419. throw Error.ArgumentNull(nameof(source));
  420. if (selector == null)
  421. throw Error.ArgumentNull(nameof(selector));
  422. return Core(source, selector, cancellationToken);
  423. static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal>> selector, CancellationToken cancellationToken)
  424. {
  425. var sum = 0m;
  426. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  427. {
  428. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  429. sum += value;
  430. }
  431. return sum;
  432. }
  433. }
  434. /// <summary>
  435. /// 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.
  436. /// </summary>
  437. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  438. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  439. /// <param name="selector">A transform function to apply to each element.</param>
  440. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  441. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  442. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  443. public static ValueTask<int?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken = default)
  444. {
  445. if (source == null)
  446. throw Error.ArgumentNull(nameof(source));
  447. if (selector == null)
  448. throw Error.ArgumentNull(nameof(selector));
  449. return Core(source, selector, cancellationToken);
  450. static async ValueTask<int?> Core(IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  451. {
  452. var sum = 0;
  453. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  454. {
  455. var value = selector(item);
  456. checked
  457. {
  458. sum += value.GetValueOrDefault();
  459. }
  460. }
  461. return sum;
  462. }
  463. }
  464. /// <summary>
  465. /// 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.
  466. /// </summary>
  467. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  468. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  469. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  470. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  471. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  472. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  473. public static ValueTask<int?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int?>> selector, CancellationToken cancellationToken = default)
  474. {
  475. if (source == null)
  476. throw Error.ArgumentNull(nameof(source));
  477. if (selector == null)
  478. throw Error.ArgumentNull(nameof(selector));
  479. return Core(source, selector, cancellationToken);
  480. static async ValueTask<int?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<int?>> selector, CancellationToken cancellationToken)
  481. {
  482. var sum = 0;
  483. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  484. {
  485. var value = await selector(item).ConfigureAwait(false);
  486. checked
  487. {
  488. sum += value.GetValueOrDefault();
  489. }
  490. }
  491. return sum;
  492. }
  493. }
  494. /// <summary>
  495. /// 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.
  496. /// </summary>
  497. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  498. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  499. /// <param name="selector">An asynchronous, cancellable transform function to apply to each element.</param>
  500. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  501. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  502. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  503. public static ValueTask<int?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int?>> selector, CancellationToken cancellationToken = default)
  504. {
  505. if (source == null)
  506. throw Error.ArgumentNull(nameof(source));
  507. if (selector == null)
  508. throw Error.ArgumentNull(nameof(selector));
  509. return Core(source, selector, cancellationToken);
  510. static async ValueTask<int?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<int?>> selector, CancellationToken cancellationToken)
  511. {
  512. var sum = 0;
  513. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  514. {
  515. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  516. checked
  517. {
  518. sum += value.GetValueOrDefault();
  519. }
  520. }
  521. return sum;
  522. }
  523. }
  524. /// <summary>
  525. /// 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.
  526. /// </summary>
  527. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  528. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  529. /// <param name="selector">A transform function to apply to each element.</param>
  530. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  531. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  532. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  533. public static ValueTask<long?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken = default)
  534. {
  535. if (source == null)
  536. throw Error.ArgumentNull(nameof(source));
  537. if (selector == null)
  538. throw Error.ArgumentNull(nameof(selector));
  539. return Core(source, selector, cancellationToken);
  540. static async ValueTask<long?> Core(IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  541. {
  542. var sum = 0L;
  543. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  544. {
  545. var value = selector(item);
  546. checked
  547. {
  548. sum += value.GetValueOrDefault();
  549. }
  550. }
  551. return sum;
  552. }
  553. }
  554. /// <summary>
  555. /// 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.
  556. /// </summary>
  557. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  558. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  559. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  560. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  561. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  562. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  563. public static ValueTask<long?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long?>> selector, CancellationToken cancellationToken = default)
  564. {
  565. if (source == null)
  566. throw Error.ArgumentNull(nameof(source));
  567. if (selector == null)
  568. throw Error.ArgumentNull(nameof(selector));
  569. return Core(source, selector, cancellationToken);
  570. static async ValueTask<long?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<long?>> selector, CancellationToken cancellationToken)
  571. {
  572. var sum = 0L;
  573. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  574. {
  575. var value = await selector(item).ConfigureAwait(false);
  576. checked
  577. {
  578. sum += value.GetValueOrDefault();
  579. }
  580. }
  581. return sum;
  582. }
  583. }
  584. /// <summary>
  585. /// 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.
  586. /// </summary>
  587. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  588. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  589. /// <param name="selector">An asynchronous, cancellable transform function to apply to each element.</param>
  590. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  591. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  592. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  593. public static ValueTask<long?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long?>> selector, CancellationToken cancellationToken = default)
  594. {
  595. if (source == null)
  596. throw Error.ArgumentNull(nameof(source));
  597. if (selector == null)
  598. throw Error.ArgumentNull(nameof(selector));
  599. return Core(source, selector, cancellationToken);
  600. static async ValueTask<long?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<long?>> selector, CancellationToken cancellationToken)
  601. {
  602. var sum = 0L;
  603. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  604. {
  605. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  606. checked
  607. {
  608. sum += value.GetValueOrDefault();
  609. }
  610. }
  611. return sum;
  612. }
  613. }
  614. /// <summary>
  615. /// 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.
  616. /// </summary>
  617. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  618. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  619. /// <param name="selector">A transform function to apply to each element.</param>
  620. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  621. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  622. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  623. public static ValueTask<float?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken = default)
  624. {
  625. if (source == null)
  626. throw Error.ArgumentNull(nameof(source));
  627. if (selector == null)
  628. throw Error.ArgumentNull(nameof(selector));
  629. return Core(source, selector, cancellationToken);
  630. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  631. {
  632. var sum = 0.0f;
  633. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  634. {
  635. var value = selector(item);
  636. sum += value.GetValueOrDefault();
  637. }
  638. return sum;
  639. }
  640. }
  641. /// <summary>
  642. /// 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.
  643. /// </summary>
  644. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  645. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  646. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  647. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  648. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  649. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  650. public static ValueTask<float?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float?>> selector, CancellationToken cancellationToken = default)
  651. {
  652. if (source == null)
  653. throw Error.ArgumentNull(nameof(source));
  654. if (selector == null)
  655. throw Error.ArgumentNull(nameof(selector));
  656. return Core(source, selector, cancellationToken);
  657. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<float?>> selector, CancellationToken cancellationToken)
  658. {
  659. var sum = 0.0f;
  660. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  661. {
  662. var value = await selector(item).ConfigureAwait(false);
  663. sum += value.GetValueOrDefault();
  664. }
  665. return sum;
  666. }
  667. }
  668. /// <summary>
  669. /// 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.
  670. /// </summary>
  671. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  672. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  673. /// <param name="selector">An asynchronous, cancellable transform function to apply to each element.</param>
  674. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  675. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  676. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  677. public static ValueTask<float?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float?>> selector, CancellationToken cancellationToken = default)
  678. {
  679. if (source == null)
  680. throw Error.ArgumentNull(nameof(source));
  681. if (selector == null)
  682. throw Error.ArgumentNull(nameof(selector));
  683. return Core(source, selector, cancellationToken);
  684. static async ValueTask<float?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<float?>> selector, CancellationToken cancellationToken)
  685. {
  686. var sum = 0.0f;
  687. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  688. {
  689. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  690. sum += value.GetValueOrDefault();
  691. }
  692. return sum;
  693. }
  694. }
  695. /// <summary>
  696. /// 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.
  697. /// </summary>
  698. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  699. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  700. /// <param name="selector">A transform function to apply to each element.</param>
  701. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  702. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  703. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  704. public static ValueTask<double?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken = default)
  705. {
  706. if (source == null)
  707. throw Error.ArgumentNull(nameof(source));
  708. if (selector == null)
  709. throw Error.ArgumentNull(nameof(selector));
  710. return Core(source, selector, cancellationToken);
  711. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  712. {
  713. var sum = 0.0;
  714. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  715. {
  716. var value = selector(item);
  717. sum += value.GetValueOrDefault();
  718. }
  719. return sum;
  720. }
  721. }
  722. /// <summary>
  723. /// 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.
  724. /// </summary>
  725. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  726. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  727. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  728. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  729. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  730. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  731. public static ValueTask<double?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double?>> selector, CancellationToken cancellationToken = default)
  732. {
  733. if (source == null)
  734. throw Error.ArgumentNull(nameof(source));
  735. if (selector == null)
  736. throw Error.ArgumentNull(nameof(selector));
  737. return Core(source, selector, cancellationToken);
  738. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<double?>> selector, CancellationToken cancellationToken)
  739. {
  740. var sum = 0.0;
  741. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  742. {
  743. var value = await selector(item).ConfigureAwait(false);
  744. sum += value.GetValueOrDefault();
  745. }
  746. return sum;
  747. }
  748. }
  749. /// <summary>
  750. /// 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.
  751. /// </summary>
  752. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  753. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  754. /// <param name="selector">An asynchronous, cancellable transform function to apply to each element.</param>
  755. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  756. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  757. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  758. public static ValueTask<double?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double?>> selector, CancellationToken cancellationToken = default)
  759. {
  760. if (source == null)
  761. throw Error.ArgumentNull(nameof(source));
  762. if (selector == null)
  763. throw Error.ArgumentNull(nameof(selector));
  764. return Core(source, selector, cancellationToken);
  765. static async ValueTask<double?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<double?>> selector, CancellationToken cancellationToken)
  766. {
  767. var sum = 0.0;
  768. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  769. {
  770. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  771. sum += value.GetValueOrDefault();
  772. }
  773. return sum;
  774. }
  775. }
  776. /// <summary>
  777. /// 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.
  778. /// </summary>
  779. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  780. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  781. /// <param name="selector">A transform function to apply to each element.</param>
  782. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  783. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  784. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  785. public static ValueTask<decimal?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken = default)
  786. {
  787. if (source == null)
  788. throw Error.ArgumentNull(nameof(source));
  789. if (selector == null)
  790. throw Error.ArgumentNull(nameof(selector));
  791. return Core(source, selector, cancellationToken);
  792. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  793. {
  794. var sum = 0m;
  795. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  796. {
  797. var value = selector(item);
  798. sum += value.GetValueOrDefault();
  799. }
  800. return sum;
  801. }
  802. }
  803. /// <summary>
  804. /// 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.
  805. /// </summary>
  806. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  807. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  808. /// <param name="selector">An asynchronous transform function to apply to each element.</param>
  809. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  810. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  811. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  812. public static ValueTask<decimal?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal?>> selector, CancellationToken cancellationToken = default)
  813. {
  814. if (source == null)
  815. throw Error.ArgumentNull(nameof(source));
  816. if (selector == null)
  817. throw Error.ArgumentNull(nameof(selector));
  818. return Core(source, selector, cancellationToken);
  819. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<decimal?>> selector, CancellationToken cancellationToken)
  820. {
  821. var sum = 0m;
  822. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  823. {
  824. var value = await selector(item).ConfigureAwait(false);
  825. sum += value.GetValueOrDefault();
  826. }
  827. return sum;
  828. }
  829. }
  830. /// <summary>
  831. /// 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.
  832. /// </summary>
  833. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  834. /// <param name="source">A sequence of values that are used to calculate a sum.</param>
  835. /// <param name="selector">An asynchronous, cancellable transform function to apply to each element.</param>
  836. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  837. /// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
  838. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
  839. public static ValueTask<decimal?> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal?>> selector, CancellationToken cancellationToken = default)
  840. {
  841. if (source == null)
  842. throw Error.ArgumentNull(nameof(source));
  843. if (selector == null)
  844. throw Error.ArgumentNull(nameof(selector));
  845. return Core(source, selector, cancellationToken);
  846. static async ValueTask<decimal?> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<decimal?>> selector, CancellationToken cancellationToken)
  847. {
  848. var sum = 0m;
  849. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  850. {
  851. var value = await selector(item, cancellationToken).ConfigureAwait(false);
  852. sum += value.GetValueOrDefault();
  853. }
  854. return sum;
  855. }
  856. }
  857. }
  858. }