Average.Generated.cs 105 KB

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