Aggregate.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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<TSource> Aggregate<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, Task<TSource>> 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, accumulator, CancellationToken.None);
  34. }
  35. public static Task<TSource> Aggregate<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, Task<TSource>> 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 Aggregate_(source, accumulator, cancellationToken);
  42. }
  43. public static Task<TAccumulate> Aggregate<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)
  44. {
  45. if (source == null)
  46. throw new ArgumentNullException(nameof(source));
  47. if (accumulator == null)
  48. throw new ArgumentNullException(nameof(accumulator));
  49. return Aggregate(source, seed, accumulator, CancellationToken.None);
  50. }
  51. public static Task<TAccumulate> Aggregate<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, CancellationToken cancellationToken)
  52. {
  53. if (source == null)
  54. throw new ArgumentNullException(nameof(source));
  55. if (accumulator == null)
  56. throw new ArgumentNullException(nameof(accumulator));
  57. return source.Aggregate(seed, accumulator, x => x, cancellationToken);
  58. }
  59. public static Task<TAccumulate> Aggregate<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, Task<TAccumulate>> accumulator)
  60. {
  61. if (source == null)
  62. throw new ArgumentNullException(nameof(source));
  63. if (accumulator == null)
  64. throw new ArgumentNullException(nameof(accumulator));
  65. return Aggregate(source, seed, accumulator, CancellationToken.None);
  66. }
  67. public static Task<TAccumulate> Aggregate<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, Task<TAccumulate>> accumulator, CancellationToken cancellationToken)
  68. {
  69. if (source == null)
  70. throw new ArgumentNullException(nameof(source));
  71. if (accumulator == null)
  72. throw new ArgumentNullException(nameof(accumulator));
  73. return source.Aggregate(seed, accumulator, x => Task.FromResult(x), cancellationToken);
  74. }
  75. public static Task<TResult> Aggregate<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector)
  76. {
  77. if (source == null)
  78. throw new ArgumentNullException(nameof(source));
  79. if (accumulator == null)
  80. throw new ArgumentNullException(nameof(accumulator));
  81. if (resultSelector == null)
  82. throw new ArgumentNullException(nameof(resultSelector));
  83. return Aggregate(source, seed, accumulator, resultSelector, CancellationToken.None);
  84. }
  85. 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)
  86. {
  87. if (source == null)
  88. throw new ArgumentNullException(nameof(source));
  89. if (accumulator == null)
  90. throw new ArgumentNullException(nameof(accumulator));
  91. if (resultSelector == null)
  92. throw new ArgumentNullException(nameof(resultSelector));
  93. return Aggregate_(source, seed, accumulator, resultSelector, cancellationToken);
  94. }
  95. public static Task<TResult> Aggregate<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, Task<TAccumulate>> accumulator, Func<TAccumulate, Task<TResult>> resultSelector)
  96. {
  97. if (source == null)
  98. throw new ArgumentNullException(nameof(source));
  99. if (accumulator == null)
  100. throw new ArgumentNullException(nameof(accumulator));
  101. if (resultSelector == null)
  102. throw new ArgumentNullException(nameof(resultSelector));
  103. return Aggregate(source, seed, accumulator, resultSelector, CancellationToken.None);
  104. }
  105. public static Task<TResult> Aggregate<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, Task<TAccumulate>> accumulator, Func<TAccumulate, Task<TResult>> resultSelector, CancellationToken cancellationToken)
  106. {
  107. if (source == null)
  108. throw new ArgumentNullException(nameof(source));
  109. if (accumulator == null)
  110. throw new ArgumentNullException(nameof(accumulator));
  111. if (resultSelector == null)
  112. throw new ArgumentNullException(nameof(resultSelector));
  113. return Aggregate_(source, seed, accumulator, resultSelector, cancellationToken);
  114. }
  115. 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)
  116. {
  117. var acc = seed;
  118. var e = source.GetAsyncEnumerator();
  119. try
  120. {
  121. while (await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  122. {
  123. acc = accumulator(acc, e.Current);
  124. }
  125. }
  126. finally
  127. {
  128. await e.DisposeAsync().ConfigureAwait(false);
  129. }
  130. return resultSelector(acc);
  131. }
  132. private static async Task<TSource> Aggregate_<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken)
  133. {
  134. var first = true;
  135. var acc = default(TSource);
  136. var e = source.GetAsyncEnumerator();
  137. try
  138. {
  139. while (await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  140. {
  141. acc = first ? e.Current : accumulator(acc, e.Current);
  142. first = false;
  143. }
  144. }
  145. finally
  146. {
  147. await e.DisposeAsync().ConfigureAwait(false);
  148. }
  149. if (first)
  150. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  151. return acc;
  152. }
  153. private static async Task<TResult> Aggregate_<TSource, TAccumulate, TResult>(IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, Task<TAccumulate>> accumulator, Func<TAccumulate, Task<TResult>> resultSelector, CancellationToken cancellationToken)
  154. {
  155. var acc = seed;
  156. var e = source.GetAsyncEnumerator();
  157. try
  158. {
  159. while (await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  160. {
  161. acc = await accumulator(acc, e.Current).ConfigureAwait(false);
  162. }
  163. }
  164. finally
  165. {
  166. await e.DisposeAsync().ConfigureAwait(false);
  167. }
  168. return await resultSelector(acc).ConfigureAwait(false);
  169. }
  170. private static async Task<TSource> Aggregate_<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, TSource, Task<TSource>> accumulator, CancellationToken cancellationToken)
  171. {
  172. var first = true;
  173. var acc = default(TSource);
  174. var e = source.GetAsyncEnumerator();
  175. try
  176. {
  177. while (await e.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  178. {
  179. acc = first ? e.Current : await accumulator(acc, e.Current).ConfigureAwait(false);
  180. first = false;
  181. }
  182. }
  183. finally
  184. {
  185. await e.DisposeAsync().ConfigureAwait(false);
  186. }
  187. if (first)
  188. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  189. return acc;
  190. }
  191. }
  192. }