AsyncEnumerable.Aggregates.cs 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
  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 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. return source.Aggregate(new Lookup<TKey, TElement>(comparer), (lookup, x) => { lookup.Add(keySelector(x), elementSelector(x)); return lookup; }, lookup => (ILookup<TKey, TElement>)lookup, cancellationToken);
  569. }
  570. public static Task<ILookup<TKey, TElement>> ToLookup<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken)
  571. {
  572. if (source == null)
  573. throw new ArgumentNullException("source");
  574. if (keySelector == null)
  575. throw new ArgumentNullException("keySelector");
  576. if (elementSelector == null)
  577. throw new ArgumentNullException("elementSelector");
  578. return source.ToLookup(keySelector, elementSelector, EqualityComparer<TKey>.Default, cancellationToken);
  579. }
  580. public static Task<ILookup<TKey, TSource>> ToLookup<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  581. {
  582. if (source == null)
  583. throw new ArgumentNullException("source");
  584. if (keySelector == null)
  585. throw new ArgumentNullException("keySelector");
  586. if (comparer == null)
  587. throw new ArgumentNullException("comparer");
  588. return source.ToLookup(keySelector, x => x, comparer, cancellationToken);
  589. }
  590. public static Task<ILookup<TKey, TSource>> ToLookup<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
  591. {
  592. if (source == null)
  593. throw new ArgumentNullException("source");
  594. if (keySelector == null)
  595. throw new ArgumentNullException("keySelector");
  596. return source.ToLookup(keySelector, x => x, EqualityComparer<TKey>.Default, cancellationToken);
  597. }
  598. class Lookup<TKey, TElement> : ILookup<TKey, TElement>
  599. {
  600. private readonly Dictionary<TKey, EnumerableGrouping<TKey, TElement>> map;
  601. public Lookup(IEqualityComparer<TKey> comparer)
  602. {
  603. map = new Dictionary<TKey, EnumerableGrouping<TKey, TElement>>(comparer);
  604. }
  605. public void Add(TKey key, TElement element)
  606. {
  607. var g = default(EnumerableGrouping<TKey, TElement>);
  608. if (!map.TryGetValue(key, out g))
  609. {
  610. g = new EnumerableGrouping<TKey, TElement>(key);
  611. map.Add(key, g);
  612. }
  613. g.Add(element);
  614. }
  615. public bool Contains(TKey key)
  616. {
  617. return map.ContainsKey(key);
  618. }
  619. public int Count
  620. {
  621. get { return map.Keys.Count; }
  622. }
  623. public IEnumerable<TElement> this[TKey key]
  624. {
  625. get { return map[key]; }
  626. }
  627. public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
  628. {
  629. return map.Values.Cast<IGrouping<TKey, TElement>>().GetEnumerator();
  630. }
  631. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  632. {
  633. return GetEnumerator();
  634. }
  635. }
  636. public static Task<double> Average(this IAsyncEnumerable<int> source, CancellationToken cancellationToken)
  637. {
  638. if (source == null)
  639. throw new ArgumentNullException("source");
  640. var tcs = new TaskCompletionSource<double>();
  641. var count = 0L;
  642. var sum = 0.0;
  643. var e = source.GetEnumerator();
  644. var f = default(Action<CancellationToken>);
  645. f = ct => e.MoveNext(ct).Then(t =>
  646. {
  647. t.Handle(tcs, res =>
  648. {
  649. if (res)
  650. {
  651. count++;
  652. sum += e.Current;
  653. f(ct);
  654. }
  655. else
  656. {
  657. if (count == 0)
  658. tcs.TrySetException(new InvalidOperationException(Strings.NO_ELEMENTS));
  659. else
  660. tcs.TrySetResult(sum / count);
  661. }
  662. });
  663. });
  664. f(cancellationToken);
  665. return tcs.Task.Finally(e.Dispose);
  666. }
  667. public static Task<double?> Average(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  668. {
  669. if (source == null)
  670. throw new ArgumentNullException("source");
  671. var tcs = new TaskCompletionSource<double?>();
  672. var count = 0L;
  673. var sum = 0.0;
  674. var e = source.GetEnumerator();
  675. var f = default(Action<CancellationToken>);
  676. f = ct => e.MoveNext(ct).Then(t =>
  677. {
  678. t.Handle(tcs, res =>
  679. {
  680. if (res)
  681. {
  682. if (e.Current.HasValue)
  683. {
  684. count++;
  685. sum += e.Current.Value;
  686. }
  687. f(ct);
  688. }
  689. else
  690. {
  691. if (count == 0)
  692. tcs.TrySetResult(null);
  693. else
  694. tcs.TrySetResult(sum / count);
  695. }
  696. });
  697. });
  698. f(cancellationToken);
  699. return tcs.Task.Finally(e.Dispose);
  700. }
  701. public static Task<double> Average(this IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  702. {
  703. if (source == null)
  704. throw new ArgumentNullException("source");
  705. var tcs = new TaskCompletionSource<double>();
  706. var count = 0L;
  707. var sum = 0.0;
  708. var e = source.GetEnumerator();
  709. var f = default(Action<CancellationToken>);
  710. f = ct => e.MoveNext(ct).Then(t =>
  711. {
  712. t.Handle(tcs, res =>
  713. {
  714. if (res)
  715. {
  716. count++;
  717. sum += e.Current;
  718. f(ct);
  719. }
  720. else
  721. {
  722. if (count == 0)
  723. tcs.TrySetException(new InvalidOperationException(Strings.NO_ELEMENTS));
  724. else
  725. tcs.TrySetResult(sum / count);
  726. }
  727. });
  728. });
  729. f(cancellationToken);
  730. return tcs.Task.Finally(e.Dispose);
  731. }
  732. public static Task<double?> Average(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  733. {
  734. if (source == null)
  735. throw new ArgumentNullException("source");
  736. var tcs = new TaskCompletionSource<double?>();
  737. var count = 0L;
  738. var sum = 0.0;
  739. var e = source.GetEnumerator();
  740. var f = default(Action<CancellationToken>);
  741. f = ct => e.MoveNext(ct).Then(t =>
  742. {
  743. t.Handle(tcs, res =>
  744. {
  745. if (res)
  746. {
  747. if (e.Current.HasValue)
  748. {
  749. count++;
  750. sum += e.Current.Value;
  751. }
  752. f(ct);
  753. }
  754. else
  755. {
  756. if (count == 0)
  757. tcs.TrySetResult(null);
  758. else
  759. tcs.TrySetResult(sum / count);
  760. }
  761. });
  762. });
  763. f(cancellationToken);
  764. return tcs.Task.Finally(e.Dispose);
  765. }
  766. public static Task<double> Average(this IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  767. {
  768. if (source == null)
  769. throw new ArgumentNullException("source");
  770. var tcs = new TaskCompletionSource<double>();
  771. var count = 0L;
  772. var sum = 0.0;
  773. var e = source.GetEnumerator();
  774. var f = default(Action<CancellationToken>);
  775. f = ct => e.MoveNext(ct).Then(t =>
  776. {
  777. t.Handle(tcs, res =>
  778. {
  779. if (res)
  780. {
  781. count++;
  782. sum += e.Current;
  783. f(ct);
  784. }
  785. else
  786. {
  787. if (count == 0)
  788. tcs.TrySetException(new InvalidOperationException(Strings.NO_ELEMENTS));
  789. else
  790. tcs.TrySetResult(sum / count);
  791. }
  792. });
  793. });
  794. f(cancellationToken);
  795. return tcs.Task.Finally(e.Dispose);
  796. }
  797. public static Task<double?> Average(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  798. {
  799. if (source == null)
  800. throw new ArgumentNullException("source");
  801. var tcs = new TaskCompletionSource<double?>();
  802. var count = 0L;
  803. var sum = 0.0;
  804. var e = source.GetEnumerator();
  805. var f = default(Action<CancellationToken>);
  806. f = ct => e.MoveNext(ct).Then(t =>
  807. {
  808. t.Handle(tcs, res =>
  809. {
  810. if (res)
  811. {
  812. if (e.Current.HasValue)
  813. {
  814. count++;
  815. sum += e.Current.Value;
  816. }
  817. f(ct);
  818. }
  819. else
  820. {
  821. if (count == 0)
  822. tcs.TrySetResult(null);
  823. else
  824. tcs.TrySetResult(sum / count);
  825. }
  826. });
  827. });
  828. f(cancellationToken);
  829. return tcs.Task.Finally(e.Dispose);
  830. }
  831. public static Task<float> Average(this IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  832. {
  833. if (source == null)
  834. throw new ArgumentNullException("source");
  835. var tcs = new TaskCompletionSource<float>();
  836. var count = 0L;
  837. var sum = 0f;
  838. var e = source.GetEnumerator();
  839. var f = default(Action<CancellationToken>);
  840. f = ct => e.MoveNext(ct).Then(t =>
  841. {
  842. t.Handle(tcs, res =>
  843. {
  844. if (res)
  845. {
  846. count++;
  847. sum += e.Current;
  848. f(ct);
  849. }
  850. else
  851. {
  852. if (count == 0)
  853. tcs.TrySetException(new InvalidOperationException(Strings.NO_ELEMENTS));
  854. else
  855. tcs.TrySetResult(sum / count);
  856. }
  857. });
  858. });
  859. f(cancellationToken);
  860. return tcs.Task.Finally(e.Dispose);
  861. }
  862. public static Task<float?> Average(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  863. {
  864. if (source == null)
  865. throw new ArgumentNullException("source");
  866. var tcs = new TaskCompletionSource<float?>();
  867. var count = 0L;
  868. var sum = 0f;
  869. var e = source.GetEnumerator();
  870. var f = default(Action<CancellationToken>);
  871. f = ct => e.MoveNext(ct).Then(t =>
  872. {
  873. t.Handle(tcs, res =>
  874. {
  875. if (res)
  876. {
  877. if (e.Current.HasValue)
  878. {
  879. count++;
  880. sum += e.Current.Value;
  881. }
  882. f(ct);
  883. }
  884. else
  885. {
  886. if (count == 0)
  887. tcs.TrySetResult(null);
  888. else
  889. tcs.TrySetResult(sum / count);
  890. }
  891. });
  892. });
  893. f(cancellationToken);
  894. return tcs.Task.Finally(e.Dispose);
  895. }
  896. public static Task<decimal> Average(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  897. {
  898. if (source == null)
  899. throw new ArgumentNullException("source");
  900. var tcs = new TaskCompletionSource<decimal>();
  901. var count = 0L;
  902. var sum = 0m;
  903. var e = source.GetEnumerator();
  904. var f = default(Action<CancellationToken>);
  905. f = ct => e.MoveNext(ct).Then(t =>
  906. {
  907. t.Handle(tcs, res =>
  908. {
  909. if (res)
  910. {
  911. count++;
  912. sum += e.Current;
  913. f(ct);
  914. }
  915. else
  916. {
  917. if (count == 0)
  918. tcs.TrySetException(new InvalidOperationException(Strings.NO_ELEMENTS));
  919. else
  920. tcs.TrySetResult(sum / count);
  921. }
  922. });
  923. });
  924. f(cancellationToken);
  925. return tcs.Task.Finally(e.Dispose);
  926. }
  927. public static Task<decimal?> Average(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  928. {
  929. if (source == null)
  930. throw new ArgumentNullException("source");
  931. var tcs = new TaskCompletionSource<decimal?>();
  932. var count = 0L;
  933. var sum = 0m;
  934. var e = source.GetEnumerator();
  935. var f = default(Action<CancellationToken>);
  936. f = ct => e.MoveNext(ct).Then(t =>
  937. {
  938. t.Handle(tcs, res =>
  939. {
  940. if (res)
  941. {
  942. if (e.Current.HasValue)
  943. {
  944. count++;
  945. sum += e.Current.Value;
  946. }
  947. f(ct);
  948. }
  949. else
  950. {
  951. if (count == 0)
  952. tcs.TrySetResult(null);
  953. else
  954. tcs.TrySetResult(sum / count);
  955. }
  956. });
  957. });
  958. f(cancellationToken);
  959. return tcs.Task.Finally(e.Dispose);
  960. }
  961. public static Task<double?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  962. {
  963. if (source == null)
  964. throw new ArgumentNullException("source");
  965. if (selector == null)
  966. throw new ArgumentNullException("selector");
  967. return source.Select(selector).Average(cancellationToken);
  968. }
  969. public static Task<double> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  970. {
  971. if (source == null)
  972. throw new ArgumentNullException("source");
  973. if (selector == null)
  974. throw new ArgumentNullException("selector");
  975. return source.Select(selector).Average(cancellationToken);
  976. }
  977. public static Task<double> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  978. {
  979. if (source == null)
  980. throw new ArgumentNullException("source");
  981. if (selector == null)
  982. throw new ArgumentNullException("selector");
  983. return source.Select(selector).Average(cancellationToken);
  984. }
  985. public static Task<double?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  986. {
  987. if (source == null)
  988. throw new ArgumentNullException("source");
  989. if (selector == null)
  990. throw new ArgumentNullException("selector");
  991. return source.Select(selector).Average(cancellationToken);
  992. }
  993. public static Task<double> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  994. {
  995. if (source == null)
  996. throw new ArgumentNullException("source");
  997. if (selector == null)
  998. throw new ArgumentNullException("selector");
  999. return source.Select(selector).Average(cancellationToken);
  1000. }
  1001. public static Task<double?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  1002. {
  1003. if (source == null)
  1004. throw new ArgumentNullException("source");
  1005. if (selector == null)
  1006. throw new ArgumentNullException("selector");
  1007. return source.Select(selector).Average(cancellationToken);
  1008. }
  1009. public static Task<float> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  1010. {
  1011. if (source == null)
  1012. throw new ArgumentNullException("source");
  1013. if (selector == null)
  1014. throw new ArgumentNullException("selector");
  1015. return source.Select(selector).Average(cancellationToken);
  1016. }
  1017. public static Task<float?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  1018. {
  1019. if (source == null)
  1020. throw new ArgumentNullException("source");
  1021. if (selector == null)
  1022. throw new ArgumentNullException("selector");
  1023. return source.Select(selector).Average(cancellationToken);
  1024. }
  1025. public static Task<decimal> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  1026. {
  1027. if (source == null)
  1028. throw new ArgumentNullException("source");
  1029. if (selector == null)
  1030. throw new ArgumentNullException("selector");
  1031. return source.Select(selector).Average(cancellationToken);
  1032. }
  1033. public static Task<decimal?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  1034. {
  1035. if (source == null)
  1036. throw new ArgumentNullException("source");
  1037. if (selector == null)
  1038. throw new ArgumentNullException("selector");
  1039. return source.Select(selector).Average(cancellationToken);
  1040. }
  1041. public static Task<int> Max(this IAsyncEnumerable<int> source, CancellationToken cancellationToken)
  1042. {
  1043. if (source == null)
  1044. throw new ArgumentNullException("source");
  1045. return source.Aggregate(Math.Max, cancellationToken);
  1046. }
  1047. public static Task<long> Max(this IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  1048. {
  1049. if (source == null)
  1050. throw new ArgumentNullException("source");
  1051. return source.Aggregate(Math.Max, cancellationToken);
  1052. }
  1053. public static Task<double> Max(this IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  1054. {
  1055. if (source == null)
  1056. throw new ArgumentNullException("source");
  1057. return source.Aggregate(Math.Max, cancellationToken);
  1058. }
  1059. public static Task<float> Max(this IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  1060. {
  1061. if (source == null)
  1062. throw new ArgumentNullException("source");
  1063. return source.Aggregate(Math.Max, cancellationToken);
  1064. }
  1065. public static Task<decimal> Max(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  1066. {
  1067. if (source == null)
  1068. throw new ArgumentNullException("source");
  1069. return source.Aggregate(Math.Max, cancellationToken);
  1070. }
  1071. static T? NullableMax<T>(T? x, T? y)
  1072. where T : struct, IComparable<T>
  1073. {
  1074. if (!x.HasValue)
  1075. return y;
  1076. if (!y.HasValue)
  1077. return x;
  1078. if (x.Value.CompareTo(y.Value) >= 0)
  1079. return x;
  1080. return y;
  1081. }
  1082. public static Task<int?> Max(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  1083. {
  1084. if (source == null)
  1085. throw new ArgumentNullException("source");
  1086. return source.Aggregate(default(int?), NullableMax, cancellationToken);
  1087. }
  1088. public static Task<long?> Max(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  1089. {
  1090. if (source == null)
  1091. throw new ArgumentNullException("source");
  1092. return source.Aggregate(default(long?), NullableMax, cancellationToken);
  1093. }
  1094. public static Task<double?> Max(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  1095. {
  1096. if (source == null)
  1097. throw new ArgumentNullException("source");
  1098. return source.Aggregate(default(double?), NullableMax, cancellationToken);
  1099. }
  1100. public static Task<float?> Max(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  1101. {
  1102. if (source == null)
  1103. throw new ArgumentNullException("source");
  1104. return source.Aggregate(default(float?), NullableMax, cancellationToken);
  1105. }
  1106. public static Task<decimal?> Max(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  1107. {
  1108. if (source == null)
  1109. throw new ArgumentNullException("source");
  1110. return source.Aggregate(default(decimal?), NullableMax, cancellationToken);
  1111. }
  1112. public static Task<TSource> Max<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  1113. {
  1114. if (source == null)
  1115. throw new ArgumentNullException("source");
  1116. var comparer = Comparer<TSource>.Default;
  1117. return source.Aggregate((x, y) => comparer.Compare(x, y) >= 0 ? x : y, cancellationToken);
  1118. }
  1119. public static Task<int> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  1120. {
  1121. if (source == null)
  1122. throw new ArgumentNullException("source");
  1123. if (selector == null)
  1124. throw new ArgumentNullException("selector");
  1125. return source.Select(selector).Max(cancellationToken);
  1126. }
  1127. public static Task<long> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  1128. {
  1129. if (source == null)
  1130. throw new ArgumentNullException("source");
  1131. if (selector == null)
  1132. throw new ArgumentNullException("selector");
  1133. return source.Select(selector).Max(cancellationToken);
  1134. }
  1135. public static Task<double> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  1136. {
  1137. if (source == null)
  1138. throw new ArgumentNullException("source");
  1139. if (selector == null)
  1140. throw new ArgumentNullException("selector");
  1141. return source.Select(selector).Max(cancellationToken);
  1142. }
  1143. public static Task<float> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  1144. {
  1145. if (source == null)
  1146. throw new ArgumentNullException("source");
  1147. if (selector == null)
  1148. throw new ArgumentNullException("selector");
  1149. return source.Select(selector).Max(cancellationToken);
  1150. }
  1151. public static Task<decimal> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  1152. {
  1153. if (source == null)
  1154. throw new ArgumentNullException("source");
  1155. if (selector == null)
  1156. throw new ArgumentNullException("selector");
  1157. return source.Select(selector).Max(cancellationToken);
  1158. }
  1159. public static Task<int?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  1160. {
  1161. if (source == null)
  1162. throw new ArgumentNullException("source");
  1163. if (selector == null)
  1164. throw new ArgumentNullException("selector");
  1165. return source.Select(selector).Max(cancellationToken);
  1166. }
  1167. public static Task<long?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  1168. {
  1169. if (source == null)
  1170. throw new ArgumentNullException("source");
  1171. if (selector == null)
  1172. throw new ArgumentNullException("selector");
  1173. return source.Select(selector).Max(cancellationToken);
  1174. }
  1175. public static Task<double?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  1176. {
  1177. if (source == null)
  1178. throw new ArgumentNullException("source");
  1179. if (selector == null)
  1180. throw new ArgumentNullException("selector");
  1181. return source.Select(selector).Max(cancellationToken);
  1182. }
  1183. public static Task<float?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  1184. {
  1185. if (source == null)
  1186. throw new ArgumentNullException("source");
  1187. if (selector == null)
  1188. throw new ArgumentNullException("selector");
  1189. return source.Select(selector).Max(cancellationToken);
  1190. }
  1191. public static Task<decimal?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  1192. {
  1193. if (source == null)
  1194. throw new ArgumentNullException("source");
  1195. if (selector == null)
  1196. throw new ArgumentNullException("selector");
  1197. return source.Select(selector).Max(cancellationToken);
  1198. }
  1199. public static Task<TResult> Max<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
  1200. {
  1201. if (source == null)
  1202. throw new ArgumentNullException("source");
  1203. if (selector == null)
  1204. throw new ArgumentNullException("selector");
  1205. return source.Select(selector).Max(cancellationToken);
  1206. }
  1207. public static Task<int> Min(this IAsyncEnumerable<int> source, CancellationToken cancellationToken)
  1208. {
  1209. if (source == null)
  1210. throw new ArgumentNullException("source");
  1211. return source.Aggregate(Math.Min, cancellationToken);
  1212. }
  1213. public static Task<long> Min(this IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  1214. {
  1215. if (source == null)
  1216. throw new ArgumentNullException("source");
  1217. return source.Aggregate(Math.Min, cancellationToken);
  1218. }
  1219. public static Task<double> Min(this IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  1220. {
  1221. if (source == null)
  1222. throw new ArgumentNullException("source");
  1223. return source.Aggregate(Math.Min, cancellationToken);
  1224. }
  1225. public static Task<float> Min(this IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  1226. {
  1227. if (source == null)
  1228. throw new ArgumentNullException("source");
  1229. return source.Aggregate(Math.Min, cancellationToken);
  1230. }
  1231. public static Task<decimal> Min(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  1232. {
  1233. if (source == null)
  1234. throw new ArgumentNullException("source");
  1235. return source.Aggregate(Math.Min, cancellationToken);
  1236. }
  1237. static T? NullableMin<T>(T? x, T? y)
  1238. where T : struct, IComparable<T>
  1239. {
  1240. if (!x.HasValue)
  1241. return y;
  1242. if (!y.HasValue)
  1243. return x;
  1244. if (x.Value.CompareTo(y.Value) <= 0)
  1245. return x;
  1246. return y;
  1247. }
  1248. public static Task<int?> Min(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  1249. {
  1250. if (source == null)
  1251. throw new ArgumentNullException("source");
  1252. return source.Aggregate(default(int?), NullableMin, cancellationToken);
  1253. }
  1254. public static Task<long?> Min(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  1255. {
  1256. if (source == null)
  1257. throw new ArgumentNullException("source");
  1258. return source.Aggregate(default(long?), NullableMin, cancellationToken);
  1259. }
  1260. public static Task<double?> Min(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  1261. {
  1262. if (source == null)
  1263. throw new ArgumentNullException("source");
  1264. return source.Aggregate(default(double?), NullableMin, cancellationToken);
  1265. }
  1266. public static Task<float?> Min(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  1267. {
  1268. if (source == null)
  1269. throw new ArgumentNullException("source");
  1270. return source.Aggregate(default(float?), NullableMin, cancellationToken);
  1271. }
  1272. public static Task<decimal?> Min(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  1273. {
  1274. if (source == null)
  1275. throw new ArgumentNullException("source");
  1276. return source.Aggregate(default(decimal?), NullableMin, cancellationToken);
  1277. }
  1278. public static Task<TSource> Min<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  1279. {
  1280. if (source == null)
  1281. throw new ArgumentNullException("source");
  1282. var comparer = Comparer<TSource>.Default;
  1283. return source.Aggregate((x, y) => comparer.Compare(x, y) <= 0 ? x : y, cancellationToken);
  1284. }
  1285. public static Task<int> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  1286. {
  1287. if (source == null)
  1288. throw new ArgumentNullException("source");
  1289. if (selector == null)
  1290. throw new ArgumentNullException("selector");
  1291. return source.Select(selector).Min(cancellationToken);
  1292. }
  1293. public static Task<long> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  1294. {
  1295. if (source == null)
  1296. throw new ArgumentNullException("source");
  1297. if (selector == null)
  1298. throw new ArgumentNullException("selector");
  1299. return source.Select(selector).Min(cancellationToken);
  1300. }
  1301. public static Task<double> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  1302. {
  1303. if (source == null)
  1304. throw new ArgumentNullException("source");
  1305. if (selector == null)
  1306. throw new ArgumentNullException("selector");
  1307. return source.Select(selector).Min(cancellationToken);
  1308. }
  1309. public static Task<float> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  1310. {
  1311. if (source == null)
  1312. throw new ArgumentNullException("source");
  1313. if (selector == null)
  1314. throw new ArgumentNullException("selector");
  1315. return source.Select(selector).Min(cancellationToken);
  1316. }
  1317. public static Task<decimal> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  1318. {
  1319. if (source == null)
  1320. throw new ArgumentNullException("source");
  1321. if (selector == null)
  1322. throw new ArgumentNullException("selector");
  1323. return source.Select(selector).Min(cancellationToken);
  1324. }
  1325. public static Task<int?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  1326. {
  1327. if (source == null)
  1328. throw new ArgumentNullException("source");
  1329. if (selector == null)
  1330. throw new ArgumentNullException("selector");
  1331. return source.Select(selector).Min(cancellationToken);
  1332. }
  1333. public static Task<long?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  1334. {
  1335. if (source == null)
  1336. throw new ArgumentNullException("source");
  1337. if (selector == null)
  1338. throw new ArgumentNullException("selector");
  1339. return source.Select(selector).Min(cancellationToken);
  1340. }
  1341. public static Task<double?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  1342. {
  1343. if (source == null)
  1344. throw new ArgumentNullException("source");
  1345. if (selector == null)
  1346. throw new ArgumentNullException("selector");
  1347. return source.Select(selector).Min(cancellationToken);
  1348. }
  1349. public static Task<float?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  1350. {
  1351. if (source == null)
  1352. throw new ArgumentNullException("source");
  1353. if (selector == null)
  1354. throw new ArgumentNullException("selector");
  1355. return source.Select(selector).Min(cancellationToken);
  1356. }
  1357. public static Task<decimal?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  1358. {
  1359. if (source == null)
  1360. throw new ArgumentNullException("source");
  1361. if (selector == null)
  1362. throw new ArgumentNullException("selector");
  1363. return source.Select(selector).Min(cancellationToken);
  1364. }
  1365. public static Task<TResult> Min<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
  1366. {
  1367. if (source == null)
  1368. throw new ArgumentNullException("source");
  1369. if (selector == null)
  1370. throw new ArgumentNullException("selector");
  1371. return source.Select(selector).Min(cancellationToken);
  1372. }
  1373. public static Task<int> Sum(this IAsyncEnumerable<int> source, CancellationToken cancellationToken)
  1374. {
  1375. if (source == null)
  1376. throw new ArgumentNullException("source");
  1377. return source.Aggregate(0, (x, y) => x + y, cancellationToken);
  1378. }
  1379. public static Task<long> Sum(this IAsyncEnumerable<long> source, CancellationToken cancellationToken)
  1380. {
  1381. if (source == null)
  1382. throw new ArgumentNullException("source");
  1383. return source.Aggregate(0L, (x, y) => x + y, cancellationToken);
  1384. }
  1385. public static Task<double> Sum(this IAsyncEnumerable<double> source, CancellationToken cancellationToken)
  1386. {
  1387. if (source == null)
  1388. throw new ArgumentNullException("source");
  1389. return source.Aggregate(0.0, (x, y) => x + y, cancellationToken);
  1390. }
  1391. public static Task<float> Sum(this IAsyncEnumerable<float> source, CancellationToken cancellationToken)
  1392. {
  1393. if (source == null)
  1394. throw new ArgumentNullException("source");
  1395. return source.Aggregate(0f, (x, y) => x + y, cancellationToken);
  1396. }
  1397. public static Task<decimal> Sum(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
  1398. {
  1399. if (source == null)
  1400. throw new ArgumentNullException("source");
  1401. return source.Aggregate(0m, (x, y) => x + y, cancellationToken);
  1402. }
  1403. public static Task<int?> Sum(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
  1404. {
  1405. if (source == null)
  1406. throw new ArgumentNullException("source");
  1407. return source.Aggregate((int?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  1408. }
  1409. public static Task<long?> Sum(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
  1410. {
  1411. if (source == null)
  1412. throw new ArgumentNullException("source");
  1413. return source.Aggregate((long?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  1414. }
  1415. public static Task<double?> Sum(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
  1416. {
  1417. if (source == null)
  1418. throw new ArgumentNullException("source");
  1419. return source.Aggregate((double?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  1420. }
  1421. public static Task<float?> Sum(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
  1422. {
  1423. if (source == null)
  1424. throw new ArgumentNullException("source");
  1425. return source.Aggregate((float?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  1426. }
  1427. public static Task<decimal?> Sum(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
  1428. {
  1429. if (source == null)
  1430. throw new ArgumentNullException("source");
  1431. return source.Aggregate((decimal?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
  1432. }
  1433. public static Task<int> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
  1434. {
  1435. if (source == null)
  1436. throw new ArgumentNullException("source");
  1437. if (selector == null)
  1438. throw new ArgumentNullException("selector");
  1439. return source.Select(selector).Sum(cancellationToken);
  1440. }
  1441. public static Task<long> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
  1442. {
  1443. if (source == null)
  1444. throw new ArgumentNullException("source");
  1445. if (selector == null)
  1446. throw new ArgumentNullException("selector");
  1447. return source.Select(selector).Sum(cancellationToken);
  1448. }
  1449. public static Task<double> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
  1450. {
  1451. if (source == null)
  1452. throw new ArgumentNullException("source");
  1453. if (selector == null)
  1454. throw new ArgumentNullException("selector");
  1455. return source.Select(selector).Sum(cancellationToken);
  1456. }
  1457. public static Task<float> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
  1458. {
  1459. if (source == null)
  1460. throw new ArgumentNullException("source");
  1461. if (selector == null)
  1462. throw new ArgumentNullException("selector");
  1463. return source.Select(selector).Sum(cancellationToken);
  1464. }
  1465. public static Task<decimal> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
  1466. {
  1467. if (source == null)
  1468. throw new ArgumentNullException("source");
  1469. if (selector == null)
  1470. throw new ArgumentNullException("selector");
  1471. return source.Select(selector).Sum(cancellationToken);
  1472. }
  1473. public static Task<int?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
  1474. {
  1475. if (source == null)
  1476. throw new ArgumentNullException("source");
  1477. if (selector == null)
  1478. throw new ArgumentNullException("selector");
  1479. return source.Select(selector).Sum(cancellationToken);
  1480. }
  1481. public static Task<long?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
  1482. {
  1483. if (source == null)
  1484. throw new ArgumentNullException("source");
  1485. if (selector == null)
  1486. throw new ArgumentNullException("selector");
  1487. return source.Select(selector).Sum(cancellationToken);
  1488. }
  1489. public static Task<double?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
  1490. {
  1491. if (source == null)
  1492. throw new ArgumentNullException("source");
  1493. if (selector == null)
  1494. throw new ArgumentNullException("selector");
  1495. return source.Select(selector).Sum(cancellationToken);
  1496. }
  1497. public static Task<float?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
  1498. {
  1499. if (source == null)
  1500. throw new ArgumentNullException("source");
  1501. if (selector == null)
  1502. throw new ArgumentNullException("selector");
  1503. return source.Select(selector).Sum(cancellationToken);
  1504. }
  1505. public static Task<decimal?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
  1506. {
  1507. if (source == null)
  1508. throw new ArgumentNullException("source");
  1509. if (selector == null)
  1510. throw new ArgumentNullException("selector");
  1511. return source.Select(selector).Sum(cancellationToken);
  1512. }
  1513. public static Task<bool> IsEmpty<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  1514. {
  1515. if (source == null)
  1516. throw new ArgumentNullException("source");
  1517. return source.Any(cancellationToken).Then(t => !t.Result);
  1518. }
  1519. public static Task<TSource> Min<TSource>(this IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
  1520. {
  1521. if (source == null)
  1522. throw new ArgumentNullException("source");
  1523. if (comparer == null)
  1524. throw new ArgumentNullException("comparer");
  1525. return MinBy(source, x => x, comparer, cancellationToken).Then(t => t.Result.First());
  1526. }
  1527. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
  1528. {
  1529. if (source == null)
  1530. throw new ArgumentNullException("source");
  1531. if (keySelector == null)
  1532. throw new ArgumentNullException("keySelector");
  1533. return MinBy(source, keySelector, Comparer<TKey>.Default, cancellationToken);
  1534. }
  1535. public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
  1536. {
  1537. if (source == null)
  1538. throw new ArgumentNullException("source");
  1539. if (keySelector == null)
  1540. throw new ArgumentNullException("keySelector");
  1541. if (comparer == null)
  1542. throw new ArgumentNullException("comparer");
  1543. return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
  1544. }
  1545. public static Task<TSource> Max<TSource>(this IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
  1546. {
  1547. if (source == null)
  1548. throw new ArgumentNullException("source");
  1549. if (comparer == null)
  1550. throw new ArgumentNullException("comparer");
  1551. return MaxBy(source, x => x, comparer, cancellationToken).Then(t => t.Result.First());
  1552. }
  1553. public static Task<IList<TSource>> MaxBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
  1554. {
  1555. if (source == null)
  1556. throw new ArgumentNullException("source");
  1557. if (keySelector == null)
  1558. throw new ArgumentNullException("keySelector");
  1559. return MaxBy(source, keySelector, Comparer<TKey>.Default, cancellationToken);
  1560. }
  1561. public static Task<IList<TSource>> MaxBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
  1562. {
  1563. if (source == null)
  1564. throw new ArgumentNullException("source");
  1565. if (keySelector == null)
  1566. throw new ArgumentNullException("keySelector");
  1567. if (comparer == null)
  1568. throw new ArgumentNullException("comparer");
  1569. return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue), cancellationToken);
  1570. }
  1571. private static Task<IList<TSource>> ExtremaBy<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> compare, CancellationToken cancellationToken)
  1572. {
  1573. var tcs = new TaskCompletionSource<IList<TSource>>();
  1574. var result = new List<TSource>();
  1575. var hasFirst = false;
  1576. var current = default(TSource);
  1577. var resKey = default(TKey);
  1578. var e = source.GetEnumerator();
  1579. var f = default(Action<CancellationToken>);
  1580. f = ct => e.MoveNext(ct).Then(t =>
  1581. {
  1582. t.Handle(tcs, res =>
  1583. {
  1584. if (!hasFirst)
  1585. {
  1586. if (!res)
  1587. {
  1588. tcs.TrySetException(new InvalidOperationException(Strings.NO_ELEMENTS));
  1589. return;
  1590. }
  1591. current = e.Current;
  1592. try
  1593. {
  1594. resKey = keySelector(current);
  1595. }
  1596. catch (Exception ex)
  1597. {
  1598. tcs.TrySetException(ex);
  1599. return;
  1600. }
  1601. result.Add(current);
  1602. hasFirst = true;
  1603. f(ct);
  1604. }
  1605. else
  1606. {
  1607. if (res)
  1608. {
  1609. var key = default(TKey);
  1610. var cmp = default(int);
  1611. try
  1612. {
  1613. current = e.Current;
  1614. key = keySelector(current);
  1615. cmp = compare(key, resKey);
  1616. }
  1617. catch (Exception ex)
  1618. {
  1619. tcs.TrySetException(ex);
  1620. return;
  1621. }
  1622. if (cmp == 0)
  1623. {
  1624. result.Add(current);
  1625. }
  1626. else if (cmp > 0)
  1627. {
  1628. result = new List<TSource> { current };
  1629. resKey = key;
  1630. }
  1631. f(ct);
  1632. }
  1633. else
  1634. {
  1635. tcs.TrySetResult(result);
  1636. }
  1637. }
  1638. });
  1639. });
  1640. f(cancellationToken);
  1641. return tcs.Task.Finally(e.Dispose);
  1642. }
  1643. }
  1644. }