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<TSource> Aggregate<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. if (accumulator == null)
  16. throw new ArgumentNullException(nameof(accumulator));
  17. return Aggregate(source, accumulator, CancellationToken.None);
  18. }
  19. public static Task<TSource> Aggregate<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken)
  20. {
  21. if (source == null)
  22. throw new ArgumentNullException(nameof(source));
  23. if (accumulator == null)
  24. throw new ArgumentNullException(nameof(accumulator));
  25. return Aggregate_(source, accumulator, cancellationToken);
  26. }
  27. public static Task<TAccumulate> Aggregate<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)
  28. {
  29. if (source == null)
  30. throw new ArgumentNullException(nameof(source));
  31. if (accumulator == null)
  32. throw new ArgumentNullException(nameof(accumulator));
  33. return Aggregate(source, seed, accumulator, CancellationToken.None);
  34. }
  35. public static Task<TAccumulate> Aggregate<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, CancellationToken cancellationToken)
  36. {
  37. if (source == null)
  38. throw new ArgumentNullException(nameof(source));
  39. if (accumulator == null)
  40. throw new ArgumentNullException(nameof(accumulator));
  41. return source.Aggregate(seed, accumulator, x => x, cancellationToken);
  42. }
  43. public static Task<TResult> Aggregate<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector)
  44. {
  45. if (source == null)
  46. throw new ArgumentNullException(nameof(source));
  47. if (accumulator == null)
  48. throw new ArgumentNullException(nameof(accumulator));
  49. if (resultSelector == null)
  50. throw new ArgumentNullException(nameof(resultSelector));
  51. return Aggregate(source, seed, accumulator, resultSelector, CancellationToken.None);
  52. }
  53. 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)
  54. {
  55. if (source == null)
  56. throw new ArgumentNullException(nameof(source));
  57. if (accumulator == null)
  58. throw new ArgumentNullException(nameof(accumulator));
  59. if (resultSelector == null)
  60. throw new ArgumentNullException(nameof(resultSelector));
  61. return Aggregate_(source, seed, accumulator, resultSelector, 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. }