Sum.Generated.cs 69 KB

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