AsyncEnumerable.Aggregates.cs 73 KB

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