Debian Codegraph @

external/rx/Ix/NET/System.Interactive.Async/AsyncEnumerable.Aggregates.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Threading.Tasks;
6 using System.Threading;
7
8 namespace System.Linq
9 {
10 public static partial class AsyncEnumerable
11 {
12 public static Task<TResult> Aggregate<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector, CancellationToken cancellationToken)
13 {
14 if (source == null)
15 throw new ArgumentNullException("source");
16 if (accumulator == null)
17 throw new ArgumentNullException("accumulator");
18 if (resultSelector == null)
19 throw new ArgumentNullException("resultSelector");
20
21 var tcs = new TaskCompletionSource<TResult>();
22
23 var acc = seed;
24
25 var e = source.GetEnumerator();
26
27 var f = default(Action<CancellationToken>);
28 f = ct => e.MoveNext(ct).ContinueWith(t =>
29 {
30 t.Handle(tcs, res =>
31 {
32 if (res)
33 {
34 try
35 {
36 acc = accumulator(acc, e.Current);
37 f(ct);
38 }
39 catch (Exception exception)
40 {
41 tcs.TrySetException(exception);
42 }
43 }
44 else
45 {
46 var result = default(TResult);
47 try
48 {
49 result = resultSelector(acc);
50 }
51 catch (Exception exception)
52 {
53 tcs.TrySetException(exception);
54 return;
55 }
56
57 tcs.TrySetResult(result);
58 }
59 });
60 });
61
62 f(cancellationToken);
63
64 return tcs.Task.Finally(e.Dispose);
65 }
66
67 public static Task<TAccumulate> Aggregate<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, CancellationToken cancellationToken)
68 {
69 if (source == null)
70 throw new ArgumentNullException("source");
71 if (accumulator == null)
72 throw new ArgumentNullException("accumulator");
73
74 return source.Aggregate(seed, accumulator, x => x, cancellationToken);
75 }
76
77 public static Task<TSource> Aggregate<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken)
78 {
79 if (source == null)
80 throw new ArgumentNullException("source");
81 if (accumulator == null)
82 throw new ArgumentNullException("accumulator");
83
84 var tcs = new TaskCompletionSource<TSource>();
85
86 var first = true;
87 var acc = default(TSource);
88
89 var e = source.GetEnumerator();
90
91 var f = default(Action<CancellationToken>);
92 f = ct => e.MoveNext(ct).ContinueWith(t =>
93 {
94 t.Handle(tcs, res =>
95 {
96 if (res)
97 {
98 try
99 {
100 if (first)
101 acc = e.Current;
102 else
103 acc = accumulator(acc, e.Current);
104 f(ct);
105 }
106 catch (Exception ex)
107 {
108 tcs.TrySetException(ex);
109 }
110
111 first = false;
112 }
113 else
114 {
115 if (first)
116 tcs.TrySetException(new InvalidOperationException());
117 else
118 tcs.TrySetResult(acc);
119 }
120 });
121 });
122
123 f(cancellationToken);
124
125 return tcs.Task.Finally(e.Dispose);
126 }
127
128 public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
129 {
130 if (source == null)
131 throw new ArgumentNullException("source");
132
133 return source.Aggregate(0, (c, _) => c + 1, cancellationToken);
134 }
135
136 public static Task<int> Count<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
137 {
138 if (source == null)
139 throw new ArgumentNullException("source");
140 if (predicate == null)
141 throw new ArgumentNullException("predicate");
142
143 return source.Where(predicate).Aggregate(0, (c, _) => c + 1, cancellationToken);
144 }
145
146 public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
147 {
148 if (source == null)
149 throw new ArgumentNullException("source");
150
151 return source.Aggregate(0L, (c, _) => c + 1, cancellationToken);
152 }
153
154 public static Task<long> LongCount<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
155 {
156 if (source == null)
157 throw new ArgumentNullException("source");
158 if (predicate == null)
159 throw new ArgumentNullException("predicate");
160
161 return source.Where(predicate).Aggregate(0L, (c, _) => c + 1, cancellationToken);
162 }
163
164 public static Task<bool> All<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
165 {
166 if (source == null)
167 throw new ArgumentNullException("source");
168 if (predicate == null)
169 throw new ArgumentNullException("predicate");
170
171 var tcs = new TaskCompletionSource<bool>();
172
173 var e = source.GetEnumerator();
174
175 var f = default(Action<CancellationToken>);
176 f = ct => e.MoveNext(ct).ContinueWith(t =>
177 {
178 t.Handle(tcs, res =>
179 {
180 if (res)
181 {
182 try
183 {
184 if (!predicate(e.Current))
185 tcs.TrySetResult(false);
186 else
187 f(ct);
188 }
189 catch (Exception ex)
190 {
191 tcs.TrySetException(ex);
192 }
193 }
194 else
195 {
196 tcs.TrySetResult(true);
197 }
198 });
199 });
200
201 f(cancellationToken);
202
203 return tcs.Task.Finally(e.Dispose);
204 }
205
206 public static Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
207 {
208 if (source == null)
209 throw new ArgumentNullException("source");
210 if (predicate == null)
211 throw new ArgumentNullException("predicate");
212
213 var tcs = new TaskCompletionSource<bool>();
214
215 var e = source.GetEnumerator();
216
217 var f = default(Action<CancellationToken>);
218 f = ct => e.MoveNext(ct).ContinueWith(t =>
219 {
220 t.Handle(tcs, res =>
221 {
222 if (res)
223 {
224 try
225 {
226 if (predicate(e.Current))
227 tcs.TrySetResult(true);
228 else
229 f(ct);
230 }
231 catch (Exception ex)
232 {
233 tcs.TrySetException(ex);
234 }
235 }
236 else
237 {
238 tcs.TrySetResult(false);
239 }
240 });
241 });
242
243 f(cancellationToken);
244
245 return tcs.Task.Finally(e.Dispose);
246 }
247
248 public static Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
249 {
250 if (source == null)
251 throw new ArgumentNullException("source");
252
253 var e = source.GetEnumerator();
254 return e.MoveNext(cancellationToken);
255 }
256
257 public static Task<bool> Contains<TSource>(this IAsyncEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer, CancellationToken cancellationToken)
258 {
259 if (source == null)
260 throw new ArgumentNullException("source");
261 if (comparer == null)
262 throw new ArgumentNullException("comparer");
263
264 return source.Any(x => comparer.Equals(x, value), cancellationToken);
265 }
266
267 public static Task<bool> Contains<TSource>(this IAsyncEnumerable<TSource> source, TSource value, CancellationToken cancellationToken)
268 {
269 if (source == null)
270 throw new ArgumentNullException("source");
271
272 return source.Contains(value, EqualityComparer<TSource>.Default, cancellationToken);
273 }
274
275 public static Task<TSource> First<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
276 {
277 if (source == null)
278 throw new ArgumentNullException("source");
279
280 var tcs = new TaskCompletionSource<TSource>();
281
282 var e = source.GetEnumerator();
283
284 e.MoveNext(cancellationToken).ContinueWith(t =>
285 {
286 t.Handle(tcs, res =>
287 {
288 if (res)
289 tcs.TrySetResult(e.Current);
290 else
291 tcs.TrySetException(new InvalidOperationException());
292 });
293 });
294
295 return tcs.Task.Finally(e.Dispose);
296 }
297
298 public static Task<TSource> First<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
299 {
300 if (source == null)
301 throw new ArgumentNullException("source");
302 if (predicate == null)
303 throw new ArgumentNullException("predicate");
304
305 return source.Where(predicate).First(cancellationToken);
306 }
307
308 public static Task<TSource> FirstOrDefault<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
309 {
310 if (source == null)
311 throw new ArgumentNullException("source");
312
313 var tcs = new TaskCompletionSource<TSource>();
314
315 var e = source.GetEnumerator();
316
317 e.MoveNext(cancellationToken).ContinueWith(t =>
318 {
319 t.Handle(tcs, res =>
320 {
321 if (res)
322 tcs.TrySetResult(e.Current);
323 else
324 tcs.TrySetResult(default(TSource));
325 });
326 });
327
328 return tcs.Task.Finally(e.Dispose);
329 }
330
331 public static Task<TSource> FirstOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
332 {
333 if (source == null)
334 throw new ArgumentNullException("source");
335 if (predicate == null)
336 throw new ArgumentNullException("predicate");
337
338 return source.Where(predicate).FirstOrDefault(cancellationToken);
339 }
340
341 public static Task<TSource> Last<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
342 {
343 if (source == null)
344 throw new ArgumentNullException("source");
345
346 var tcs = new TaskCompletionSource<TSource>();
347
348 var e = source.GetEnumerator();
349 var last = default(TSource);
350 var hasLast = false;
351
352 var f = default(Action<CancellationToken>);
353 f = ct => e.MoveNext(ct).ContinueWith(t =>
354 {
355 t.Handle(tcs, res =>
356 {
357 if (res)
358 {
359 hasLast = true;
360 last = e.Current;
361 f(ct);
362 }
363 else
364 {
365 if (!hasLast)
366 tcs.TrySetException(new InvalidOperationException());
367 else
368 tcs.TrySetResult(last);
369 }
370 });
371 });
372
373 f(cancellationToken);
374
375 return tcs.Task.Finally(e.Dispose);
376 }
377
378 public static Task<TSource> Last<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
379 {
380 if (source == null)
381 throw new ArgumentNullException("source");
382 if (predicate == null)
383 throw new ArgumentNullException("predicate");
384
385 return source.Where(predicate).Last(cancellationToken);
386 }
387
388 public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
389 {
390 if (source == null)
391 throw new ArgumentNullException("source");
392
393 var tcs = new TaskCompletionSource<TSource>();
394
395 var e = source.GetEnumerator();
396 var last = default(TSource);
397 var hasLast = false;
398
399 var f = default(Action<CancellationToken>);
400 f = ct => e.MoveNext(ct).ContinueWith(t =>
401 {
402 t.Handle(tcs, res =>
403 {
404 if (res)
405 {
406 hasLast = true;
407 last = e.Current;
408 f(ct);
409 }
410 else
411 {
412 if (!hasLast)
413 tcs.TrySetResult(default(TSource));
414 else
415 tcs.TrySetResult(last);
416 }
417 });
418 });
419
420 f(cancellationToken);
421
422 return tcs.Task.Finally(e.Dispose);
423 }
424
425 public static Task<TSource> LastOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
426 {
427 if (source == null)
428 throw new ArgumentNullException("source");
429 if (predicate == null)
430 throw new ArgumentNullException("predicate");
431
432 return source.Where(predicate).LastOrDefault(cancellationToken);
433 }
434
435 public static Task<TSource> Single<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
436 {
437 if (source == null)
438 throw new ArgumentNullException("source");
439
440 var tcs = new TaskCompletionSource<TSource>();
441
442 var e = source.GetEnumerator();
443
444 e.MoveNext(cancellationToken).ContinueWith(t =>
445 {
446 t.Handle(tcs, res =>
447 {
448 if (res)
449 {
450 var result = e.Current;
451 e.MoveNext(cancellationToken).ContinueWith(t1 =>
452 {
453 t1.Handle(tcs, res1 =>
454 {
455 if (res1)
456 tcs.TrySetException(new InvalidOperationException());
457 else
458 tcs.TrySetResult(result);
459 });
460 });
461 }
462 else
463 tcs.TrySetException(new InvalidOperationException());
464 });
465 });
466
467 return tcs.Task.Finally(e.Dispose);
468 }
469
470 public static Task<TSource> Single<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
471 {
472 if (source == null)
473 throw new ArgumentNullException("source");
474 if (predicate == null)
475 throw new ArgumentNullException("predicate");
476
477 return source.Where(predicate).Single(cancellationToken);
478 }
479
480 public static Task<TSource> SingleOrDefault<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
481 {
482 if (source == null)
483 throw new ArgumentNullException("source");
484
485 var tcs = new TaskCompletionSource<TSource>();
486
487 var e = source.GetEnumerator();
488
489 e.MoveNext(cancellationToken).ContinueWith(t =>
490 {
491 t.Handle(tcs, res =>
492 {
493 if (res)
494 {
495 var result = e.Current;
496 e.MoveNext(cancellationToken).ContinueWith(t1 =>
497 {
498 t1.Handle(tcs, res1 =>
499 {
500 if (res1)
501 tcs.TrySetException(new InvalidOperationException());
502 else
503 tcs.TrySetResult(result);
504 });
505 });
506 }
507 else
508 tcs.TrySetResult(default(TSource));
509 });
510 });
511
512 return tcs.Task.Finally(e.Dispose);
513 }
514
515 public static Task<TSource> SingleOrDefault<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
516 {
517 if (source == null)
518 throw new ArgumentNullException("source");
519 if (predicate == null)
520 throw new ArgumentNullException("predicate");
521
522 return source.Where(predicate).SingleOrDefault(cancellationToken);
523 }
524
525 public static Task<TSource> ElementAt<TSource>(this IAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken)
526 {
527 if (source == null)
528 throw new ArgumentNullException("source");
529 if (index < 0)
530 throw new ArgumentOutOfRangeException("index");
531
532 var tcs = new TaskCompletionSource<TSource>();
533
534 var e = source.GetEnumerator();
535
536 var next = default(Action<CancellationToken>);
537 next = ct => e.MoveNext(ct).ContinueWith(t =>
538 {
539 t.Handle(tcs, res =>
540 {
541 if (res)
542 {
543 if (index == 0)
544 {
545 tcs.TrySetResult(e.Current);
546 }
547 else
548 {
549 index--;
550 next(ct);
551 }
552 }
553 else
554 {
555 tcs.TrySetException(new ArgumentOutOfRangeException());
556 }
557 });
558 });
559
560 next(cancellationToken);
561
562 return tcs.Task.Finally(e.Dispose);
563 }
564
565 public static Task<TSource> ElementAtOrDefault<TSource>(this IAsyncEnumerable<TSource> source, int index, CancellationToken cancellationToken)
566 {
567 if (source == null)
568 throw new ArgumentNullException("source");
569 if (index < 0)
570 throw new ArgumentOutOfRangeException("index");
571
572 var tcs = new TaskCompletionSource<TSource>();
573
574 var e = source.GetEnumerator();
575
576 var next = default(Action<CancellationToken>);
577 next = ct => e.MoveNext(ct).ContinueWith(t =>
578 {
579 t.Handle(tcs, res =>
580 {
581 if (res)
582 {
583 if (index == 0)
584 {
585 tcs.TrySetResult(e.Current);
586 }
587 else
588 {
589 index--;
590 next(ct);
591 }
592 }
593 else
594 {
595 tcs.TrySetResult(default(TSource));
596 }
597 });
598 });
599
600 next(cancellationToken);
601
602 return tcs.Task.Finally(e.Dispose);
603 }
604
605 public static Task<TSource[]> ToArray<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
606 {
607 if (source == null)
608 throw new ArgumentNullException("source");
609
610 return source.Aggregate(new List<TSource>(), (list, x) => { list.Add(x); return list; }, list => list.ToArray(), cancellationToken);
611 }
612
613 public static Task<List<TSource>> ToList<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
614 {
615 if (source == null)
616 throw new ArgumentNullException("source");
617
618 return source.Aggregate(new List<TSource>(), (list, x) => { list.Add(x); return list; }, cancellationToken);
619 }
620
621 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)
622 {
623 if (source == null)
624 throw new ArgumentNullException("source");
625 if (keySelector == null)
626 throw new ArgumentNullException("keySelector");
627 if (elementSelector == null)
628 throw new ArgumentNullException("elementSelector");
629 if (comparer == null)
630 throw new ArgumentNullException("comparer");
631
632 return source.Aggregate(new Dictionary<TKey, TElement>(comparer), (d, x) => { d.Add(keySelector(x), elementSelector(x)); return d; }, cancellationToken);
633 }
634
635 public static Task<Dictionary<TKey, TElement>> ToDictionary<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken)
636 {
637 if (source == null)
638 throw new ArgumentNullException("source");
639 if (keySelector == null)
640 throw new ArgumentNullException("keySelector");
641 if (elementSelector == null)
642 throw new ArgumentNullException("elementSelector");
643
644 return source.ToDictionary(keySelector, elementSelector, EqualityComparer<TKey>.Default, cancellationToken);
645 }
646
647 public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
648 {
649 if (source == null)
650 throw new ArgumentNullException("source");
651 if (keySelector == null)
652 throw new ArgumentNullException("keySelector");
653 if (comparer == null)
654 throw new ArgumentNullException("comparer");
655
656 return source.ToDictionary(keySelector, x => x, comparer, cancellationToken);
657 }
658
659 public static Task<Dictionary<TKey, TSource>> ToDictionary<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
660 {
661 if (source == null)
662 throw new ArgumentNullException("source");
663 if (keySelector == null)
664 throw new ArgumentNullException("keySelector");
665
666 return source.ToDictionary(keySelector, x => x, EqualityComparer<TKey>.Default, cancellationToken);
667 }
668
669 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)
670 {
671 if (source == null)
672 throw new ArgumentNullException("source");
673 if (keySelector == null)
674 throw new ArgumentNullException("keySelector");
675 if (elementSelector == null)
676 throw new ArgumentNullException("elementSelector");
677 if (comparer == null)
678 throw new ArgumentNullException("comparer");
679
680 return source.Aggregate(new Lookup<TKey, TElement>(comparer), (lookup, x) => { lookup.Add(keySelector(x), elementSelector(x)); return lookup; }, lookup => (ILookup<TKey, TElement>)lookup, cancellationToken);
681 }
682
683 public static Task<ILookup<TKey, TElement>> ToLookup<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, CancellationToken cancellationToken)
684 {
685 if (source == null)
686 throw new ArgumentNullException("source");
687 if (keySelector == null)
688 throw new ArgumentNullException("keySelector");
689 if (elementSelector == null)
690 throw new ArgumentNullException("elementSelector");
691
692 return source.ToLookup(keySelector, elementSelector, EqualityComparer<TKey>.Default, cancellationToken);
693 }
694
695 public static Task<ILookup<TKey, TSource>> ToLookup<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
696 {
697 if (source == null)
698 throw new ArgumentNullException("source");
699 if (keySelector == null)
700 throw new ArgumentNullException("keySelector");
701 if (comparer == null)
702 throw new ArgumentNullException("comparer");
703
704 return source.ToLookup(keySelector, x => x, comparer, cancellationToken);
705 }
706
707 public static Task<ILookup<TKey, TSource>> ToLookup<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
708 {
709 if (source == null)
710 throw new ArgumentNullException("source");
711 if (keySelector == null)
712 throw new ArgumentNullException("keySelector");
713
714 return source.ToLookup(keySelector, x => x, EqualityComparer<TKey>.Default, cancellationToken);
715 }
716
717 class Lookup<TKey, TElement> : ILookup<TKey, TElement>
718 {
719 private readonly Dictionary<TKey, EnumerableGrouping<TKey, TElement>> map;
720
721 public Lookup(IEqualityComparer<TKey> comparer)
722 {
723 map = new Dictionary<TKey, EnumerableGrouping<TKey, TElement>>(comparer);
724 }
725
726 public void Add(TKey key, TElement element)
727 {
728 var g = default(EnumerableGrouping<TKey, TElement>);
729 if (!map.TryGetValue(key, out g))
730 {
731 g = new EnumerableGrouping<TKey, TElement>(key);
732 map.Add(key, g);
733 }
734
735 g.Add(element);
736 }
737
738 public bool Contains(TKey key)
739 {
740 return map.ContainsKey(key);
741 }
742
743 public int Count
744 {
745 get { return map.Keys.Count; }
746 }
747
748 public IEnumerable<TElement> this[TKey key]
749 {
750 get { return map[key]; }
751 }
752
753 public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
754 {
755 return map.Values.Cast<IGrouping<TKey, TElement>>().GetEnumerator();
756 }
757
758 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
759 {
760 return GetEnumerator();
761 }
762 }
763
764 public static Task<double> Average(this IAsyncEnumerable<int> source, CancellationToken cancellationToken)
765 {
766 if (source == null)
767 throw new ArgumentNullException("source");
768
769 var tcs = new TaskCompletionSource<double>();
770
771 var count = 0L;
772 var sum = 0.0;
773
774 var e = source.GetEnumerator();
775
776 var f = default(Action<CancellationToken>);
777 f = ct => e.MoveNext(ct).ContinueWith(t =>
778 {
779 t.Handle(tcs, res =>
780 {
781 if (res)
782 {
783 count++;
784 sum += e.Current;
785 f(ct);
786 }
787 else
788 {
789 if (count == 0)
790 tcs.TrySetException(new InvalidOperationException());
791 else
792 tcs.TrySetResult(sum / count);
793 }
794 });
795 });
796
797 f(cancellationToken);
798
799 return tcs.Task.Finally(e.Dispose);
800 }
801
802 public static Task<double?> Average(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
803 {
804 if (source == null)
805 throw new ArgumentNullException("source");
806
807 var tcs = new TaskCompletionSource<double?>();
808
809 var count = 0L;
810 var sum = 0.0;
811
812 var e = source.GetEnumerator();
813
814 var f = default(Action<CancellationToken>);
815 f = ct => e.MoveNext(ct).ContinueWith(t =>
816 {
817 t.Handle(tcs, res =>
818 {
819 if (res)
820 {
821 if (e.Current.HasValue)
822 {
823 count++;
824 sum += e.Current.Value;
825 }
826 f(ct);
827 }
828 else
829 {
830 if (count == 0)
831 tcs.TrySetResult(null);
832 else
833 tcs.TrySetResult(sum / count);
834 }
835 });
836 });
837
838 f(cancellationToken);
839
840 return tcs.Task.Finally(e.Dispose);
841 }
842
843 public static Task<double> Average(this IAsyncEnumerable<long> source, CancellationToken cancellationToken)
844 {
845 if (source == null)
846 throw new ArgumentNullException("source");
847
848 var tcs = new TaskCompletionSource<double>();
849
850 var count = 0L;
851 var sum = 0.0;
852
853 var e = source.GetEnumerator();
854
855 var f = default(Action<CancellationToken>);
856 f = ct => e.MoveNext(ct).ContinueWith(t =>
857 {
858 t.Handle(tcs, res =>
859 {
860 if (res)
861 {
862 count++;
863 sum += e.Current;
864 f(ct);
865 }
866 else
867 {
868 if (count == 0)
869 tcs.TrySetException(new InvalidOperationException());
870 else
871 tcs.TrySetResult(sum / count);
872 }
873 });
874 });
875
876 f(cancellationToken);
877
878 return tcs.Task.Finally(e.Dispose);
879 }
880
881 public static Task<double?> Average(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
882 {
883 if (source == null)
884 throw new ArgumentNullException("source");
885
886 var tcs = new TaskCompletionSource<double?>();
887
888 var count = 0L;
889 var sum = 0.0;
890
891 var e = source.GetEnumerator();
892
893 var f = default(Action<CancellationToken>);
894 f = ct => e.MoveNext(ct).ContinueWith(t =>
895 {
896 t.Handle(tcs, res =>
897 {
898 if (res)
899 {
900 if (e.Current.HasValue)
901 {
902 count++;
903 sum += e.Current.Value;
904 }
905 f(ct);
906 }
907 else
908 {
909 if (count == 0)
910 tcs.TrySetResult(null);
911 else
912 tcs.TrySetResult(sum / count);
913 }
914 });
915 });
916
917 f(cancellationToken);
918
919 return tcs.Task.Finally(e.Dispose);
920 }
921
922 public static Task<double> Average(this IAsyncEnumerable<double> source, CancellationToken cancellationToken)
923 {
924 if (source == null)
925 throw new ArgumentNullException("source");
926
927 var tcs = new TaskCompletionSource<double>();
928
929 var count = 0L;
930 var sum = 0.0;
931
932 var e = source.GetEnumerator();
933
934 var f = default(Action<CancellationToken>);
935 f = ct => e.MoveNext(ct).ContinueWith(t =>
936 {
937 t.Handle(tcs, res =>
938 {
939 if (res)
940 {
941 count++;
942 sum += e.Current;
943 f(ct);
944 }
945 else
946 {
947 if (count == 0)
948 tcs.TrySetException(new InvalidOperationException());
949 else
950 tcs.TrySetResult(sum / count);
951 }
952 });
953 });
954
955 f(cancellationToken);
956
957 return tcs.Task.Finally(e.Dispose);
958 }
959
960 public static Task<double?> Average(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
961 {
962 if (source == null)
963 throw new ArgumentNullException("source");
964
965 var tcs = new TaskCompletionSource<double?>();
966
967 var count = 0L;
968 var sum = 0.0;
969
970 var e = source.GetEnumerator();
971
972 var f = default(Action<CancellationToken>);
973 f = ct => e.MoveNext(ct).ContinueWith(t =>
974 {
975 t.Handle(tcs, res =>
976 {
977 if (res)
978 {
979 if (e.Current.HasValue)
980 {
981 count++;
982 sum += e.Current.Value;
983 }
984 f(ct);
985 }
986 else
987 {
988 if (count == 0)
989 tcs.TrySetResult(null);
990 else
991 tcs.TrySetResult(sum / count);
992 }
993 });
994 });
995
996 f(cancellationToken);
997
998 return tcs.Task.Finally(e.Dispose);
999 }
1000
1001 public static Task<float> Average(this IAsyncEnumerable<float> source, CancellationToken cancellationToken)
1002 {
1003 if (source == null)
1004 throw new ArgumentNullException("source");
1005
1006 var tcs = new TaskCompletionSource<float>();
1007
1008 var count = 0L;
1009 var sum = 0f;
1010
1011 var e = source.GetEnumerator();
1012
1013 var f = default(Action<CancellationToken>);
1014 f = ct => e.MoveNext(ct).ContinueWith(t =>
1015 {
1016 t.Handle(tcs, res =>
1017 {
1018 if (res)
1019 {
1020 count++;
1021 sum += e.Current;
1022 f(ct);
1023 }
1024 else
1025 {
1026 if (count == 0)
1027 tcs.TrySetException(new InvalidOperationException());
1028 else
1029 tcs.TrySetResult(sum / count);
1030 }
1031 });
1032 });
1033
1034 f(cancellationToken);
1035
1036 return tcs.Task.Finally(e.Dispose);
1037 }
1038
1039 public static Task<float?> Average(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
1040 {
1041 if (source == null)
1042 throw new ArgumentNullException("source");
1043
1044 var tcs = new TaskCompletionSource<float?>();
1045
1046 var count = 0L;
1047 var sum = 0f;
1048
1049 var e = source.GetEnumerator();
1050
1051 var f = default(Action<CancellationToken>);
1052 f = ct => e.MoveNext(ct).ContinueWith(t =>
1053 {
1054 t.Handle(tcs, res =>
1055 {
1056 if (res)
1057 {
1058 if (e.Current.HasValue)
1059 {
1060 count++;
1061 sum += e.Current.Value;
1062 }
1063 f(ct);
1064 }
1065 else
1066 {
1067 if (count == 0)
1068 tcs.TrySetResult(null);
1069 else
1070 tcs.TrySetResult(sum / count);
1071 }
1072 });
1073 });
1074
1075 f(cancellationToken);
1076
1077 return tcs.Task.Finally(e.Dispose);
1078 }
1079
1080 public static Task<decimal> Average(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
1081 {
1082 if (source == null)
1083 throw new ArgumentNullException("source");
1084
1085 var tcs = new TaskCompletionSource<decimal>();
1086
1087 var count = 0L;
1088 var sum = 0m;
1089
1090 var e = source.GetEnumerator();
1091
1092 var f = default(Action<CancellationToken>);
1093 f = ct => e.MoveNext(ct).ContinueWith(t =>
1094 {
1095 t.Handle(tcs, res =>
1096 {
1097 if (res)
1098 {
1099 count++;
1100 sum += e.Current;
1101 f(ct);
1102 }
1103 else
1104 {
1105 if (count == 0)
1106 tcs.TrySetException(new InvalidOperationException());
1107 else
1108 tcs.TrySetResult(sum / count);
1109 }
1110 });
1111 });
1112
1113 f(cancellationToken);
1114
1115 return tcs.Task.Finally(e.Dispose);
1116 }
1117
1118 public static Task<decimal?> Average(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
1119 {
1120 if (source == null)
1121 throw new ArgumentNullException("source");
1122
1123 var tcs = new TaskCompletionSource<decimal?>();
1124
1125 var count = 0L;
1126 var sum = 0m;
1127
1128 var e = source.GetEnumerator();
1129
1130 var f = default(Action<CancellationToken>);
1131 f = ct => e.MoveNext(ct).ContinueWith(t =>
1132 {
1133 t.Handle(tcs, res =>
1134 {
1135 if (res)
1136 {
1137 if (e.Current.HasValue)
1138 {
1139 count++;
1140 sum += e.Current.Value;
1141 }
1142 f(ct);
1143 }
1144 else
1145 {
1146 if (count == 0)
1147 tcs.TrySetResult(null);
1148 else
1149 tcs.TrySetResult(sum / count);
1150 }
1151 });
1152 });
1153
1154 f(cancellationToken);
1155
1156 return tcs.Task.Finally(e.Dispose);
1157 }
1158
1159 public static Task<double?> Average<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
1166 return source.Select(selector).Average(cancellationToken);
1167 }
1168
1169 public static Task<double> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
1170 {
1171 if (source == null)
1172 throw new ArgumentNullException("source");
1173 if (selector == null)
1174 throw new ArgumentNullException("selector");
1175
1176 return source.Select(selector).Average(cancellationToken);
1177 }
1178
1179 public static Task<double> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
1180 {
1181 if (source == null)
1182 throw new ArgumentNullException("source");
1183 if (selector == null)
1184 throw new ArgumentNullException("selector");
1185
1186 return source.Select(selector).Average(cancellationToken);
1187 }
1188
1189 public static Task<double?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
1190 {
1191 if (source == null)
1192 throw new ArgumentNullException("source");
1193 if (selector == null)
1194 throw new ArgumentNullException("selector");
1195
1196 return source.Select(selector).Average(cancellationToken);
1197 }
1198
1199 public static Task<double> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
1200 {
1201 if (source == null)
1202 throw new ArgumentNullException("source");
1203 if (selector == null)
1204 throw new ArgumentNullException("selector");
1205
1206 return source.Select(selector).Average(cancellationToken);
1207 }
1208
1209 public static Task<double?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
1210 {
1211 if (source == null)
1212 throw new ArgumentNullException("source");
1213 if (selector == null)
1214 throw new ArgumentNullException("selector");
1215
1216 return source.Select(selector).Average(cancellationToken);
1217 }
1218
1219 public static Task<float> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
1220 {
1221 if (source == null)
1222 throw new ArgumentNullException("source");
1223 if (selector == null)
1224 throw new ArgumentNullException("selector");
1225
1226 return source.Select(selector).Average(cancellationToken);
1227 }
1228
1229 public static Task<float?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
1230 {
1231 if (source == null)
1232 throw new ArgumentNullException("source");
1233 if (selector == null)
1234 throw new ArgumentNullException("selector");
1235
1236 return source.Select(selector).Average(cancellationToken);
1237 }
1238
1239 public static Task<decimal> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
1240 {
1241 if (source == null)
1242 throw new ArgumentNullException("source");
1243 if (selector == null)
1244 throw new ArgumentNullException("selector");
1245
1246 return source.Select(selector).Average(cancellationToken);
1247 }
1248
1249 public static Task<decimal?> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
1250 {
1251 if (source == null)
1252 throw new ArgumentNullException("source");
1253 if (selector == null)
1254 throw new ArgumentNullException("selector");
1255
1256 return source.Select(selector).Average(cancellationToken);
1257 }
1258
1259 public static Task<int> Max(this IAsyncEnumerable<int> source, CancellationToken cancellationToken)
1260 {
1261 if (source == null)
1262 throw new ArgumentNullException("source");
1263
1264 return source.Aggregate(Math.Max, cancellationToken);
1265 }
1266
1267 public static Task<long> Max(this IAsyncEnumerable<long> source, CancellationToken cancellationToken)
1268 {
1269 if (source == null)
1270 throw new ArgumentNullException("source");
1271
1272 return source.Aggregate(Math.Max, cancellationToken);
1273 }
1274
1275 public static Task<double> Max(this IAsyncEnumerable<double> source, CancellationToken cancellationToken)
1276 {
1277 if (source == null)
1278 throw new ArgumentNullException("source");
1279
1280 return source.Aggregate(Math.Max, cancellationToken);
1281 }
1282
1283 public static Task<float> Max(this IAsyncEnumerable<float> source, CancellationToken cancellationToken)
1284 {
1285 if (source == null)
1286 throw new ArgumentNullException("source");
1287
1288 return source.Aggregate(Math.Max, cancellationToken);
1289 }
1290
1291 public static Task<decimal> Max(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
1292 {
1293 if (source == null)
1294 throw new ArgumentNullException("source");
1295
1296 return source.Aggregate(Math.Max, cancellationToken);
1297 }
1298
1299 static T? NullableMax<T>(T? x, T? y)
1300 where T : struct, IComparable<T>
1301 {
1302 if (!x.HasValue)
1303 return y;
1304 if (!y.HasValue)
1305 return x;
1306 if (x.Value.CompareTo(y.Value) >= 0)
1307 return x;
1308 return y;
1309 }
1310
1311 public static Task<int?> Max(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
1312 {
1313 if (source == null)
1314 throw new ArgumentNullException("source");
1315
1316 return source.Aggregate(default(int?), NullableMax, cancellationToken);
1317 }
1318
1319 public static Task<long?> Max(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
1320 {
1321 if (source == null)
1322 throw new ArgumentNullException("source");
1323
1324 return source.Aggregate(default(long?), NullableMax, cancellationToken);
1325 }
1326
1327 public static Task<double?> Max(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
1328 {
1329 if (source == null)
1330 throw new ArgumentNullException("source");
1331
1332 return source.Aggregate(default(double?), NullableMax, cancellationToken);
1333 }
1334
1335 public static Task<float?> Max(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
1336 {
1337 if (source == null)
1338 throw new ArgumentNullException("source");
1339
1340 return source.Aggregate(default(float?), NullableMax, cancellationToken);
1341 }
1342
1343 public static Task<decimal?> Max(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
1344 {
1345 if (source == null)
1346 throw new ArgumentNullException("source");
1347
1348 return source.Aggregate(default(decimal?), NullableMax, cancellationToken);
1349 }
1350
1351 public static Task<TSource> Max<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
1352 {
1353 if (source == null)
1354 throw new ArgumentNullException("source");
1355
1356 var comparer = Comparer<TSource>.Default;
1357 return source.Aggregate((x, y) => comparer.Compare(x, y) >= 0 ? x : y, cancellationToken);
1358 }
1359
1360 public static Task<int> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
1361 {
1362 if (source == null)
1363 throw new ArgumentNullException("source");
1364 if (selector == null)
1365 throw new ArgumentNullException("selector");
1366
1367 return source.Select(selector).Max(cancellationToken);
1368 }
1369
1370 public static Task<long> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
1371 {
1372 if (source == null)
1373 throw new ArgumentNullException("source");
1374 if (selector == null)
1375 throw new ArgumentNullException("selector");
1376
1377 return source.Select(selector).Max(cancellationToken);
1378 }
1379
1380 public static Task<double> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
1381 {
1382 if (source == null)
1383 throw new ArgumentNullException("source");
1384 if (selector == null)
1385 throw new ArgumentNullException("selector");
1386
1387 return source.Select(selector).Max(cancellationToken);
1388 }
1389
1390 public static Task<float> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
1391 {
1392 if (source == null)
1393 throw new ArgumentNullException("source");
1394 if (selector == null)
1395 throw new ArgumentNullException("selector");
1396
1397 return source.Select(selector).Max(cancellationToken);
1398 }
1399
1400 public static Task<decimal> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
1401 {
1402 if (source == null)
1403 throw new ArgumentNullException("source");
1404 if (selector == null)
1405 throw new ArgumentNullException("selector");
1406
1407 return source.Select(selector).Max(cancellationToken);
1408 }
1409
1410 public static Task<int?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
1411 {
1412 if (source == null)
1413 throw new ArgumentNullException("source");
1414 if (selector == null)
1415 throw new ArgumentNullException("selector");
1416
1417 return source.Select(selector).Max(cancellationToken);
1418 }
1419
1420 public static Task<long?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
1421 {
1422 if (source == null)
1423 throw new ArgumentNullException("source");
1424 if (selector == null)
1425 throw new ArgumentNullException("selector");
1426
1427 return source.Select(selector).Max(cancellationToken);
1428 }
1429
1430 public static Task<double?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
1431 {
1432 if (source == null)
1433 throw new ArgumentNullException("source");
1434 if (selector == null)
1435 throw new ArgumentNullException("selector");
1436
1437 return source.Select(selector).Max(cancellationToken);
1438 }
1439
1440 public static Task<float?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
1441 {
1442 if (source == null)
1443 throw new ArgumentNullException("source");
1444 if (selector == null)
1445 throw new ArgumentNullException("selector");
1446
1447 return source.Select(selector).Max(cancellationToken);
1448 }
1449
1450 public static Task<decimal?> Max<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
1451 {
1452 if (source == null)
1453 throw new ArgumentNullException("source");
1454 if (selector == null)
1455 throw new ArgumentNullException("selector");
1456
1457 return source.Select(selector).Max(cancellationToken);
1458 }
1459
1460 public static Task<TResult> Max<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
1461 {
1462 if (source == null)
1463 throw new ArgumentNullException("source");
1464 if (selector == null)
1465 throw new ArgumentNullException("selector");
1466
1467 return source.Select(selector).Max(cancellationToken);
1468 }
1469
1470 public static Task<int> Min(this IAsyncEnumerable<int> source, CancellationToken cancellationToken)
1471 {
1472 if (source == null)
1473 throw new ArgumentNullException("source");
1474
1475 return source.Aggregate(Math.Min, cancellationToken);
1476 }
1477
1478 public static Task<long> Min(this IAsyncEnumerable<long> source, CancellationToken cancellationToken)
1479 {
1480 if (source == null)
1481 throw new ArgumentNullException("source");
1482
1483 return source.Aggregate(Math.Min, cancellationToken);
1484 }
1485
1486 public static Task<double> Min(this IAsyncEnumerable<double> source, CancellationToken cancellationToken)
1487 {
1488 if (source == null)
1489 throw new ArgumentNullException("source");
1490
1491 return source.Aggregate(Math.Min, cancellationToken);
1492 }
1493
1494 public static Task<float> Min(this IAsyncEnumerable<float> source, CancellationToken cancellationToken)
1495 {
1496 if (source == null)
1497 throw new ArgumentNullException("source");
1498
1499 return source.Aggregate(Math.Min, cancellationToken);
1500 }
1501
1502 public static Task<decimal> Min(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
1503 {
1504 if (source == null)
1505 throw new ArgumentNullException("source");
1506
1507 return source.Aggregate(Math.Min, cancellationToken);
1508 }
1509
1510 static T? NullableMin<T>(T? x, T? y)
1511 where T : struct, IComparable<T>
1512 {
1513 if (!x.HasValue)
1514 return y;
1515 if (!y.HasValue)
1516 return x;
1517 if (x.Value.CompareTo(y.Value) <= 0)
1518 return x;
1519 return y;
1520 }
1521
1522 public static Task<int?> Min(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
1523 {
1524 if (source == null)
1525 throw new ArgumentNullException("source");
1526
1527 return source.Aggregate(default(int?), NullableMin, cancellationToken);
1528 }
1529
1530 public static Task<long?> Min(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
1531 {
1532 if (source == null)
1533 throw new ArgumentNullException("source");
1534
1535 return source.Aggregate(default(long?), NullableMin, cancellationToken);
1536 }
1537
1538 public static Task<double?> Min(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
1539 {
1540 if (source == null)
1541 throw new ArgumentNullException("source");
1542
1543 return source.Aggregate(default(double?), NullableMin, cancellationToken);
1544 }
1545
1546 public static Task<float?> Min(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
1547 {
1548 if (source == null)
1549 throw new ArgumentNullException("source");
1550
1551 return source.Aggregate(default(float?), NullableMin, cancellationToken);
1552 }
1553
1554 public static Task<decimal?> Min(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
1555 {
1556 if (source == null)
1557 throw new ArgumentNullException("source");
1558
1559 return source.Aggregate(default(decimal?), NullableMin, cancellationToken);
1560 }
1561
1562 public static Task<TSource> Min<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
1563 {
1564 if (source == null)
1565 throw new ArgumentNullException("source");
1566
1567 var comparer = Comparer<TSource>.Default;
1568 return source.Aggregate((x, y) => comparer.Compare(x, y) <= 0 ? x : y, cancellationToken);
1569 }
1570
1571 public static Task<int> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
1572 {
1573 if (source == null)
1574 throw new ArgumentNullException("source");
1575 if (selector == null)
1576 throw new ArgumentNullException("selector");
1577
1578 return source.Select(selector).Min(cancellationToken);
1579 }
1580
1581 public static Task<long> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
1582 {
1583 if (source == null)
1584 throw new ArgumentNullException("source");
1585 if (selector == null)
1586 throw new ArgumentNullException("selector");
1587
1588 return source.Select(selector).Min(cancellationToken);
1589 }
1590
1591 public static Task<double> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
1592 {
1593 if (source == null)
1594 throw new ArgumentNullException("source");
1595 if (selector == null)
1596 throw new ArgumentNullException("selector");
1597
1598 return source.Select(selector).Min(cancellationToken);
1599 }
1600
1601 public static Task<float> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
1602 {
1603 if (source == null)
1604 throw new ArgumentNullException("source");
1605 if (selector == null)
1606 throw new ArgumentNullException("selector");
1607
1608 return source.Select(selector).Min(cancellationToken);
1609 }
1610
1611 public static Task<decimal> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
1612 {
1613 if (source == null)
1614 throw new ArgumentNullException("source");
1615 if (selector == null)
1616 throw new ArgumentNullException("selector");
1617
1618 return source.Select(selector).Min(cancellationToken);
1619 }
1620
1621 public static Task<int?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
1622 {
1623 if (source == null)
1624 throw new ArgumentNullException("source");
1625 if (selector == null)
1626 throw new ArgumentNullException("selector");
1627
1628 return source.Select(selector).Min(cancellationToken);
1629 }
1630
1631 public static Task<long?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
1632 {
1633 if (source == null)
1634 throw new ArgumentNullException("source");
1635 if (selector == null)
1636 throw new ArgumentNullException("selector");
1637
1638 return source.Select(selector).Min(cancellationToken);
1639 }
1640
1641 public static Task<double?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
1642 {
1643 if (source == null)
1644 throw new ArgumentNullException("source");
1645 if (selector == null)
1646 throw new ArgumentNullException("selector");
1647
1648 return source.Select(selector).Min(cancellationToken);
1649 }
1650
1651 public static Task<float?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
1652 {
1653 if (source == null)
1654 throw new ArgumentNullException("source");
1655 if (selector == null)
1656 throw new ArgumentNullException("selector");
1657
1658 return source.Select(selector).Min(cancellationToken);
1659 }
1660
1661 public static Task<decimal?> Min<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
1662 {
1663 if (source == null)
1664 throw new ArgumentNullException("source");
1665 if (selector == null)
1666 throw new ArgumentNullException("selector");
1667
1668 return source.Select(selector).Min(cancellationToken);
1669 }
1670
1671 public static Task<TResult> Min<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector, CancellationToken cancellationToken)
1672 {
1673 if (source == null)
1674 throw new ArgumentNullException("source");
1675 if (selector == null)
1676 throw new ArgumentNullException("selector");
1677
1678 return source.Select(selector).Min(cancellationToken);
1679 }
1680
1681 public static Task<int> Sum(this IAsyncEnumerable<int> source, CancellationToken cancellationToken)
1682 {
1683 if (source == null)
1684 throw new ArgumentNullException("source");
1685
1686 return source.Aggregate(0, (x, y) => x + y, cancellationToken);
1687 }
1688
1689 public static Task<long> Sum(this IAsyncEnumerable<long> source, CancellationToken cancellationToken)
1690 {
1691 if (source == null)
1692 throw new ArgumentNullException("source");
1693
1694 return source.Aggregate(0L, (x, y) => x + y, cancellationToken);
1695 }
1696
1697 public static Task<double> Sum(this IAsyncEnumerable<double> source, CancellationToken cancellationToken)
1698 {
1699 if (source == null)
1700 throw new ArgumentNullException("source");
1701
1702 return source.Aggregate(0.0, (x, y) => x + y, cancellationToken);
1703 }
1704
1705 public static Task<float> Sum(this IAsyncEnumerable<float> source, CancellationToken cancellationToken)
1706 {
1707 if (source == null)
1708 throw new ArgumentNullException("source");
1709
1710 return source.Aggregate(0f, (x, y) => x + y, cancellationToken);
1711 }
1712
1713 public static Task<decimal> Sum(this IAsyncEnumerable<decimal> source, CancellationToken cancellationToken)
1714 {
1715 if (source == null)
1716 throw new ArgumentNullException("source");
1717
1718 return source.Aggregate(0m, (x, y) => x + y, cancellationToken);
1719 }
1720
1721 public static Task<int?> Sum(this IAsyncEnumerable<int?> source, CancellationToken cancellationToken)
1722 {
1723 if (source == null)
1724 throw new ArgumentNullException("source");
1725
1726 return source.Aggregate((int?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
1727 }
1728
1729 public static Task<long?> Sum(this IAsyncEnumerable<long?> source, CancellationToken cancellationToken)
1730 {
1731 if (source == null)
1732 throw new ArgumentNullException("source");
1733
1734 return source.Aggregate((long?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
1735 }
1736
1737 public static Task<double?> Sum(this IAsyncEnumerable<double?> source, CancellationToken cancellationToken)
1738 {
1739 if (source == null)
1740 throw new ArgumentNullException("source");
1741
1742 return source.Aggregate((double?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
1743 }
1744
1745 public static Task<float?> Sum(this IAsyncEnumerable<float?> source, CancellationToken cancellationToken)
1746 {
1747 if (source == null)
1748 throw new ArgumentNullException("source");
1749
1750 return source.Aggregate((float?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
1751 }
1752
1753 public static Task<decimal?> Sum(this IAsyncEnumerable<decimal?> source, CancellationToken cancellationToken)
1754 {
1755 if (source == null)
1756 throw new ArgumentNullException("source");
1757
1758 return source.Aggregate((decimal?)0, (x, y) => x + y.GetValueOrDefault(), cancellationToken);
1759 }
1760
1761 public static Task<int> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int> selector, CancellationToken cancellationToken)
1762 {
1763 if (source == null)
1764 throw new ArgumentNullException("source");
1765 if (selector == null)
1766 throw new ArgumentNullException("selector");
1767
1768 return source.Select(selector).Sum(cancellationToken);
1769 }
1770
1771 public static Task<long> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long> selector, CancellationToken cancellationToken)
1772 {
1773 if (source == null)
1774 throw new ArgumentNullException("source");
1775 if (selector == null)
1776 throw new ArgumentNullException("selector");
1777
1778 return source.Select(selector).Sum(cancellationToken);
1779 }
1780
1781 public static Task<double> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double> selector, CancellationToken cancellationToken)
1782 {
1783 if (source == null)
1784 throw new ArgumentNullException("source");
1785 if (selector == null)
1786 throw new ArgumentNullException("selector");
1787
1788 return source.Select(selector).Sum(cancellationToken);
1789 }
1790
1791 public static Task<float> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float> selector, CancellationToken cancellationToken)
1792 {
1793 if (source == null)
1794 throw new ArgumentNullException("source");
1795 if (selector == null)
1796 throw new ArgumentNullException("selector");
1797
1798 return source.Select(selector).Sum(cancellationToken);
1799 }
1800
1801 public static Task<decimal> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal> selector, CancellationToken cancellationToken)
1802 {
1803 if (source == null)
1804 throw new ArgumentNullException("source");
1805 if (selector == null)
1806 throw new ArgumentNullException("selector");
1807
1808 return source.Select(selector).Sum(cancellationToken);
1809 }
1810
1811 public static Task<int?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int?> selector, CancellationToken cancellationToken)
1812 {
1813 if (source == null)
1814 throw new ArgumentNullException("source");
1815 if (selector == null)
1816 throw new ArgumentNullException("selector");
1817
1818 return source.Select(selector).Sum(cancellationToken);
1819 }
1820
1821 public static Task<long?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, long?> selector, CancellationToken cancellationToken)
1822 {
1823 if (source == null)
1824 throw new ArgumentNullException("source");
1825 if (selector == null)
1826 throw new ArgumentNullException("selector");
1827
1828 return source.Select(selector).Sum(cancellationToken);
1829 }
1830
1831 public static Task<double?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, double?> selector, CancellationToken cancellationToken)
1832 {
1833 if (source == null)
1834 throw new ArgumentNullException("source");
1835 if (selector == null)
1836 throw new ArgumentNullException("selector");
1837
1838 return source.Select(selector).Sum(cancellationToken);
1839 }
1840
1841 public static Task<float?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, float?> selector, CancellationToken cancellationToken)
1842 {
1843 if (source == null)
1844 throw new ArgumentNullException("source");
1845 if (selector == null)
1846 throw new ArgumentNullException("selector");
1847
1848 return source.Select(selector).Sum(cancellationToken);
1849 }
1850
1851 public static Task<decimal?> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, decimal?> selector, CancellationToken cancellationToken)
1852 {
1853 if (source == null)
1854 throw new ArgumentNullException("source");
1855 if (selector == null)
1856 throw new ArgumentNullException("selector");
1857
1858 return source.Select(selector).Sum(cancellationToken);
1859 }
1860
1861 public static Task<bool> IsEmpty<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
1862 {
1863 if (source == null)
1864 throw new ArgumentNullException("source");
1865
1866 return source.Any(cancellationToken).ContinueWith(t => !t.Result);
1867 }
1868
1869 public static Task<TSource> Min<TSource>(this IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
1870 {
1871 if (source == null)
1872 throw new ArgumentNullException("source");
1873 if (comparer == null)
1874 throw new ArgumentNullException("comparer");
1875
1876 return MinBy(source, x => x, comparer, cancellationToken).ContinueWith(t => t.Result.First());
1877 }
1878
1879 public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
1880 {
1881 if (source == null)
1882 throw new ArgumentNullException("source");
1883 if (keySelector == null)
1884 throw new ArgumentNullException("keySelector");
1885
1886 return MinBy(source, keySelector, Comparer<TKey>.Default, cancellationToken);
1887 }
1888
1889 public static Task<IList<TSource>> MinBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
1890 {
1891 if (source == null)
1892 throw new ArgumentNullException("source");
1893 if (keySelector == null)
1894 throw new ArgumentNullException("keySelector");
1895 if (comparer == null)
1896 throw new ArgumentNullException("comparer");
1897
1898 return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue), cancellationToken);
1899 }
1900
1901 public static Task<TSource> Max<TSource>(this IAsyncEnumerable<TSource> source, IComparer<TSource> comparer, CancellationToken cancellationToken)
1902 {
1903 if (source == null)
1904 throw new ArgumentNullException("source");
1905 if (comparer == null)
1906 throw new ArgumentNullException("comparer");
1907
1908 return MaxBy(source, x => x, comparer, cancellationToken).ContinueWith(t => t.Result.First());
1909 }
1910
1911 public static Task<IList<TSource>> MaxBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, CancellationToken cancellationToken)
1912 {
1913 if (source == null)
1914 throw new ArgumentNullException("source");
1915 if (keySelector == null)
1916 throw new ArgumentNullException("keySelector");
1917
1918 return MaxBy(source, keySelector, Comparer<TKey>.Default, cancellationToken);
1919 }
1920
1921 public static Task<IList<TSource>> MaxBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, CancellationToken cancellationToken)
1922 {
1923 if (source == null)
1924 throw new ArgumentNullException("source");
1925 if (keySelector == null)
1926 throw new ArgumentNullException("keySelector");
1927 if (comparer == null)
1928 throw new ArgumentNullException("comparer");
1929
1930 return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue), cancellationToken);
1931 }
1932
1933 private static Task<IList<TSource>> ExtremaBy<TSource, TKey>(IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> compare, CancellationToken cancellationToken)
1934 {
1935 var tcs = new TaskCompletionSource<IList<TSource>>();
1936
1937 var result = new List<TSource>();
1938
1939 var hasFirst = false;
1940 var current = default(TSource);
1941 var resKey = default(TKey);
1942
1943 var e = source.GetEnumerator();
1944
1945 var f = default(Action<CancellationToken>);
1946 f = ct => e.MoveNext(ct).ContinueWith(t =>
1947 {
1948 t.Handle(tcs, res =>
1949 {
1950 if (!hasFirst)
1951 {
1952 if (!res)
1953 {
1954 tcs.TrySetException(new InvalidOperationException("Source sequence doesn't contain any elements."));
1955 return;
1956 }
1957
1958 current = e.Current;
1959
1960 try
1961 {
1962 resKey = keySelector(current);
1963 }
1964 catch (Exception ex)
1965 {
1966 tcs.TrySetException(ex);
1967 return;
1968 }
1969
1970 result.Add(current);
1971
1972 hasFirst = true;
1973 f(ct);
1974 }
1975 else
1976 {
1977 if (res)
1978 {
1979 var key = default(TKey);
1980 var cmp = default(int);
1981
1982 try
1983 {
1984 current = e.Current;
1985 key = keySelector(current);
1986 cmp = compare(key, resKey);
1987 }
1988 catch (Exception ex)
1989 {
1990 tcs.TrySetException(ex);
1991 return;
1992 }
1993
1994 if (cmp == 0)
1995 {
1996 result.Add(current);
1997 }
1998 else if (cmp > 0)
1999 {
2000 result = new List<TSource> { current };
2001 resKey = key;
2002 }
2003
2004 f(ct);
2005 }
2006 else
2007 {
2008 tcs.TrySetResult(result);
2009 }
2010 }
2011 });
2012 });
2013
2014 f(cancellationToken);
2015
2016 return tcs.Task.Finally(e.Dispose);
2017 }
2018 }
2019 }