Scan.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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 AsyncEnumerableEx
  10. {
  11. // NB: Implementations of Scan never yield the first element, unlike the behavior of Aggregate on a sequence with one
  12. // element, which returns the first element (or the seed if given an empty sequence). This is compatible with Rx
  13. // but one could argue whether it was the right default.
  14. /// <summary>
  15. /// Applies an accumulator function over an async-enumerable sequence and returns each intermediate result.
  16. /// For aggregation behavior with no intermediate results, see <see cref="AsyncEnumerable.AggregateAsync{TSource}(IAsyncEnumerable{TSource}, Func{TSource, TSource, TSource}, CancellationToken)"/>.
  17. /// </summary>
  18. /// <typeparam name="TSource">The type of the elements in the source sequence and the result of the aggregation.</typeparam>
  19. /// <param name="source">An async-enumerable sequence to accumulate over.</param>
  20. /// <param name="accumulator">An accumulator function to be invoked on each element.</param>
  21. /// <returns>An async-enumerable sequence containing the accumulated values.</returns>
  22. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="accumulator"/> is null.</exception>
  23. public static IAsyncEnumerable<TSource> Scan<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator)
  24. {
  25. if (source == null)
  26. throw Error.ArgumentNull(nameof(source));
  27. if (accumulator == null)
  28. throw Error.ArgumentNull(nameof(accumulator));
  29. return Core(source, accumulator);
  30. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  31. {
  32. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  33. if (!await e.MoveNextAsync())
  34. {
  35. yield break;
  36. }
  37. var res = e.Current;
  38. while (await e.MoveNextAsync())
  39. {
  40. res = accumulator(res, e.Current);
  41. yield return res;
  42. }
  43. }
  44. }
  45. /// <summary>
  46. /// Applies an accumulator function over an async-enumerable sequence and returns each intermediate result. The specified seed value is used as the initial accumulator value.
  47. /// For aggregation behavior with no intermediate results, see <see cref="AsyncEnumerable.AggregateAsync{TSource, TAccumulate}(IAsyncEnumerable{TSource}, TAccumulate, Func{TAccumulate, TSource, TAccumulate}, CancellationToken)"/>.
  48. /// </summary>
  49. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  50. /// <typeparam name="TAccumulate">The type of the result of the aggregation.</typeparam>
  51. /// <param name="source">An async-enumerable sequence to accumulate over.</param>
  52. /// <param name="seed">The initial accumulator value.</param>
  53. /// <param name="accumulator">An accumulator function to be invoked on each element.</param>
  54. /// <returns>An async-enumerable sequence containing the accumulated values.</returns>
  55. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="accumulator"/> is null.</exception>
  56. public static IAsyncEnumerable<TAccumulate> Scan<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)
  57. {
  58. if (source == null)
  59. throw Error.ArgumentNull(nameof(source));
  60. if (accumulator == null)
  61. throw Error.ArgumentNull(nameof(accumulator));
  62. return Core(source, seed, accumulator);
  63. static async IAsyncEnumerable<TAccumulate> Core(IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  64. {
  65. var res = seed;
  66. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  67. {
  68. res = accumulator(res, item);
  69. yield return res;
  70. }
  71. }
  72. }
  73. /// <summary>
  74. /// Applies an asynchronous accumulator function over an async-enumerable sequence and returns each intermediate result.
  75. /// For aggregation behavior with no intermediate results, see <see cref="AsyncEnumerable.AggregateAsync{TSource}(IAsyncEnumerable{TSource}, Func{TSource, TSource, CancellationToken, ValueTask{TSource}}, CancellationToken)"/>.
  76. /// </summary>
  77. /// <typeparam name="TSource">The type of the elements in the source sequence and the result of the aggregation.</typeparam>
  78. /// <param name="source">An async-enumerable sequence to accumulate over.</param>
  79. /// <param name="accumulator">An asynchronous accumulator function to be invoked and awaited on each element.</param>
  80. /// <returns>An async-enumerable sequence containing the accumulated values.</returns>
  81. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="accumulator"/> is null.</exception>
  82. public static IAsyncEnumerable<TSource> Scan<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> accumulator)
  83. {
  84. if (source == null)
  85. throw Error.ArgumentNull(nameof(source));
  86. if (accumulator == null)
  87. throw Error.ArgumentNull(nameof(accumulator));
  88. return Core(source, accumulator);
  89. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> accumulator, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  90. {
  91. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  92. if (!await e.MoveNextAsync())
  93. {
  94. yield break;
  95. }
  96. var res = e.Current;
  97. while (await e.MoveNextAsync())
  98. {
  99. res = await accumulator(res, e.Current).ConfigureAwait(false);
  100. yield return res;
  101. }
  102. }
  103. }
  104. #if !NO_DEEP_CANCELLATION
  105. /// <summary>
  106. /// Applies an asynchronous (cancellable) accumulator function over an async-enumerable sequence and returns each intermediate result.
  107. /// For aggregation behavior with no intermediate results, see <see cref="AsyncEnumerable.AggregateAsync{TSource, TAccumulate}(IAsyncEnumerable{TSource}, TAccumulate, Func{TAccumulate, TSource, CancellationToken, ValueTask{TAccumulate}}, CancellationToken)"/>.
  108. /// </summary>
  109. /// <typeparam name="TSource">The type of the elements in the source sequence and the result of the aggregation.</typeparam>
  110. /// <param name="source">An async-enumerable sequence to accumulate over.</param>
  111. /// <param name="accumulator">An asynchronous (cancellable) accumulator function to be invoked and awaited on each element.</param>
  112. /// <returns>An async-enumerable sequence containing the accumulated values.</returns>
  113. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="accumulator"/> is null.</exception>
  114. public static IAsyncEnumerable<TSource> Scan<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, ValueTask<TSource>> accumulator)
  115. {
  116. if (source == null)
  117. throw Error.ArgumentNull(nameof(source));
  118. if (accumulator == null)
  119. throw Error.ArgumentNull(nameof(accumulator));
  120. return Core(source, accumulator);
  121. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, ValueTask<TSource>> accumulator, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  122. {
  123. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  124. if (!await e.MoveNextAsync())
  125. {
  126. yield break;
  127. }
  128. var res = e.Current;
  129. while (await e.MoveNextAsync())
  130. {
  131. res = await accumulator(res, e.Current, cancellationToken).ConfigureAwait(false);
  132. yield return res;
  133. }
  134. }
  135. }
  136. #endif
  137. /// <summary>
  138. /// Applies an asynchronous accumulator function over an async-enumerable sequence and returns each intermediate result. The specified seed value is used as the initial accumulator value.
  139. /// For aggregation behavior with no intermediate results, see <see cref="AsyncEnumerable.AggregateAsync{TSource, TAccumulate, TResult}(IAsyncEnumerable{TSource}, TAccumulate, Func{TAccumulate, TSource, CancellationToken, ValueTask{TAccumulate}}, Func{TAccumulate, CancellationToken, ValueTask{TResult}}, CancellationToken)"/>.
  140. /// </summary>
  141. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  142. /// <typeparam name="TAccumulate">The type of the result of the aggregation.</typeparam>
  143. /// <param name="source">An async-enumerable sequence to accumulate over.</param>
  144. /// <param name="seed">The initial accumulator value.</param>
  145. /// <param name="accumulator">An asynchronous accumulator function to be invoked on each element.</param>
  146. /// <returns>An async-enumerable sequence containing the accumulated values.</returns>
  147. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="accumulator"/> is null.</exception>
  148. public static IAsyncEnumerable<TAccumulate> Scan<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> accumulator)
  149. {
  150. if (source == null)
  151. throw Error.ArgumentNull(nameof(source));
  152. if (accumulator == null)
  153. throw Error.ArgumentNull(nameof(accumulator));
  154. return Core(source, seed, accumulator);
  155. static async IAsyncEnumerable<TAccumulate> Core(IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> accumulator, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  156. {
  157. var res = seed;
  158. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  159. {
  160. res = await accumulator(res, item).ConfigureAwait(false);
  161. yield return res;
  162. }
  163. }
  164. }
  165. #if !NO_DEEP_CANCELLATION
  166. /// <summary>
  167. /// Applies an asynchronous (cancellable) accumulator function over an async-enumerable sequence and returns each intermediate result. The specified seed value is used as the initial accumulator value.
  168. /// For aggregation behavior with no intermediate results, see <see cref="AsyncEnumerable.AggregateAsync{TSource, TAccumulate, TResult}(IAsyncEnumerable{TSource}, TAccumulate, Func{TAccumulate, TSource, CancellationToken, ValueTask{TAccumulate}}, Func{TAccumulate, CancellationToken, ValueTask{TResult}}, CancellationToken)"/>.
  169. /// </summary>
  170. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  171. /// <typeparam name="TAccumulate">The type of the result of the aggregation.</typeparam>
  172. /// <param name="source">An async-enumerable sequence to accumulate over.</param>
  173. /// <param name="seed">The initial accumulator value.</param>
  174. /// <param name="accumulator">An asynchronous (cancellable) accumulator function to be invoked on each element.</param>
  175. /// <returns>An async-enumerable sequence containing the accumulated values.</returns>
  176. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="accumulator"/> is null.</exception>
  177. public static IAsyncEnumerable<TAccumulate> Scan<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> accumulator)
  178. {
  179. if (source == null)
  180. throw Error.ArgumentNull(nameof(source));
  181. if (accumulator == null)
  182. throw Error.ArgumentNull(nameof(accumulator));
  183. return Core(source, seed, accumulator);
  184. static async IAsyncEnumerable<TAccumulate> Core(IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> accumulator, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  185. {
  186. var res = seed;
  187. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  188. {
  189. res = await accumulator(res, item, cancellationToken).ConfigureAwait(false);
  190. yield return res;
  191. }
  192. }
  193. }
  194. #endif
  195. }
  196. }