Aggregate.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. {
  15. throw new ArgumentNullException(nameof(source));
  16. }
  17. if (accumulator == null)
  18. {
  19. throw new ArgumentNullException(nameof(accumulator));
  20. }
  21. if (resultSelector == null)
  22. {
  23. throw new ArgumentNullException(nameof(resultSelector));
  24. }
  25. return Aggregate(source, seed, accumulator, resultSelector, CancellationToken.None);
  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. {
  31. throw new ArgumentNullException(nameof(source));
  32. }
  33. if (accumulator == null)
  34. {
  35. throw new ArgumentNullException(nameof(accumulator));
  36. }
  37. return Aggregate(source, seed, accumulator, CancellationToken.None);
  38. }
  39. public static Task<TSource> Aggregate<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator)
  40. {
  41. if (source == null)
  42. {
  43. throw new ArgumentNullException(nameof(source));
  44. }
  45. if (accumulator == null)
  46. {
  47. throw new ArgumentNullException(nameof(accumulator));
  48. }
  49. return Aggregate(source, accumulator, CancellationToken.None);
  50. }
  51. 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)
  52. {
  53. if (source == null)
  54. {
  55. throw new ArgumentNullException(nameof(source));
  56. }
  57. if (accumulator == null)
  58. {
  59. throw new ArgumentNullException(nameof(accumulator));
  60. }
  61. if (resultSelector == null)
  62. {
  63. throw new ArgumentNullException(nameof(resultSelector));
  64. }
  65. return Aggregate_(source, seed, accumulator, resultSelector, cancellationToken);
  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. {
  71. throw new ArgumentNullException(nameof(source));
  72. }
  73. if (accumulator == null)
  74. {
  75. throw new ArgumentNullException(nameof(accumulator));
  76. }
  77. return source.Aggregate(seed, accumulator, x => x, cancellationToken);
  78. }
  79. public static Task<TSource> Aggregate<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken)
  80. {
  81. if (source == null)
  82. {
  83. throw new ArgumentNullException(nameof(source));
  84. }
  85. if (accumulator == null)
  86. {
  87. throw new ArgumentNullException(nameof(accumulator));
  88. }
  89. return Aggregate_(source, accumulator, cancellationToken);
  90. }
  91. private static async Task<TResult> Aggregate_<TSource, TAccumulate, TResult>(IAsyncEnumerable<TSource> source, TAccumulate seed,
  92. Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector, CancellationToken cancellationToken)
  93. {
  94. var acc = seed;
  95. using (var e = source.GetEnumerator())
  96. {
  97. while (await e.MoveNext(cancellationToken)
  98. .ConfigureAwait(false))
  99. {
  100. acc = accumulator(acc, e.Current);
  101. }
  102. }
  103. return resultSelector(acc);
  104. }
  105. private static async Task<TSource> Aggregate_<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken)
  106. {
  107. var first = true;
  108. var acc = default(TSource);
  109. using (var e = source.GetEnumerator())
  110. {
  111. while (await e.MoveNext(cancellationToken)
  112. .ConfigureAwait(false))
  113. {
  114. acc = first ? e.Current : accumulator(acc, e.Current);
  115. first = false;
  116. }
  117. }
  118. if (first)
  119. {
  120. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  121. }
  122. return acc;
  123. }
  124. }
  125. }