Aggregate.cs 5.1 KB

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