Average.Generated.cs 81 KB

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