Sum.Generated.cs 56 KB

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