Average.Generated.cs 106 KB

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