Average.Generated.cs 101 KB

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