Average.cs 51 KB

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