AsyncEnumerable.Aggregates.cs 70 KB

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