Sum.Generated.cs 69 KB

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