Aggregate.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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;
  8. using System.Threading.Tasks;
  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)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. if (accumulator == null)
  18. throw new ArgumentNullException(nameof(accumulator));
  19. if (resultSelector == null)
  20. throw new ArgumentNullException(nameof(resultSelector));
  21. return Aggregate(source, seed, accumulator, resultSelector, CancellationToken.None);
  22. }
  23. public static Task<TAccumulate> Aggregate<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)
  24. {
  25. if (source == null)
  26. throw new ArgumentNullException(nameof(source));
  27. if (accumulator == null)
  28. throw new ArgumentNullException(nameof(accumulator));
  29. return Aggregate(source, seed, accumulator, CancellationToken.None);
  30. }
  31. public static Task<TSource> Aggregate<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator)
  32. {
  33. if (source == null)
  34. throw new ArgumentNullException(nameof(source));
  35. if (accumulator == null)
  36. throw new ArgumentNullException(nameof(accumulator));
  37. return Aggregate(source, accumulator, CancellationToken.None);
  38. }
  39. 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)
  40. {
  41. if (source == null)
  42. throw new ArgumentNullException(nameof(source));
  43. if (accumulator == null)
  44. throw new ArgumentNullException(nameof(accumulator));
  45. if (resultSelector == null)
  46. throw new ArgumentNullException(nameof(resultSelector));
  47. return Aggregate_(source, seed, accumulator, resultSelector, cancellationToken);
  48. }
  49. public static Task<TAccumulate> Aggregate<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, CancellationToken cancellationToken)
  50. {
  51. if (source == null)
  52. throw new ArgumentNullException(nameof(source));
  53. if (accumulator == null)
  54. throw new ArgumentNullException(nameof(accumulator));
  55. return source.Aggregate(seed, accumulator, x => x, cancellationToken);
  56. }
  57. public static Task<TSource> Aggregate<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken)
  58. {
  59. if (source == null)
  60. throw new ArgumentNullException(nameof(source));
  61. if (accumulator == null)
  62. throw new ArgumentNullException(nameof(accumulator));
  63. return Aggregate_(source, accumulator, cancellationToken);
  64. }
  65. private static async Task<TResult> Aggregate_<TSource, TAccumulate, TResult>(IAsyncEnumerable<TSource> source, TAccumulate seed,
  66. Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector, CancellationToken cancellationToken)
  67. {
  68. var acc = seed;
  69. using (var e = source.GetEnumerator())
  70. {
  71. while (await e.MoveNext(cancellationToken)
  72. .ConfigureAwait(false))
  73. {
  74. acc = accumulator(acc, e.Current);
  75. }
  76. }
  77. return resultSelector(acc);
  78. }
  79. private static async Task<TSource> Aggregate_<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken)
  80. {
  81. var first = true;
  82. var acc = default(TSource);
  83. using (var e = source.GetEnumerator())
  84. {
  85. while (await e.MoveNext(cancellationToken)
  86. .ConfigureAwait(false))
  87. {
  88. acc = first ? e.Current : accumulator(acc, e.Current);
  89. first = false;
  90. }
  91. }
  92. if (first)
  93. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  94. return acc;
  95. }
  96. }
  97. }