Average.Generated.cs 72 KB

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