Scan.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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}"/>.
  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. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  30. return Core(source, accumulator);
  31. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  32. #else
  33. return AsyncEnumerable.Create(Core);
  34. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  35. #endif
  36. {
  37. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  38. if (!await e.MoveNextAsync())
  39. {
  40. yield break;
  41. }
  42. var res = e.Current;
  43. while (await e.MoveNextAsync())
  44. {
  45. res = accumulator(res, e.Current);
  46. yield return res;
  47. }
  48. }
  49. }
  50. /// <summary>
  51. /// 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.
  52. /// For aggregation behavior with no intermediate results, see <see cref="AsyncEnumerable.AggregateAsync{TSource, Accumulate}"/>.
  53. /// </summary>
  54. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  55. /// <typeparam name="TAccumulate">The type of the result of the aggregation.</typeparam>
  56. /// <param name="source">An async-enumerable sequence to accumulate over.</param>
  57. /// <param name="seed">The initial accumulator value.</param>
  58. /// <param name="accumulator">An accumulator function to be invoked on each element.</param>
  59. /// <returns>An async-enumerable sequence containing the accumulated values.</returns>
  60. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="accumulator"/> is null.</exception>
  61. public static IAsyncEnumerable<TAccumulate> Scan<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)
  62. {
  63. if (source == null)
  64. throw Error.ArgumentNull(nameof(source));
  65. if (accumulator == null)
  66. throw Error.ArgumentNull(nameof(accumulator));
  67. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  68. return Core(source, seed, accumulator);
  69. static async IAsyncEnumerable<TAccumulate> Core(IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  70. #else
  71. return AsyncEnumerable.Create(Core);
  72. async IAsyncEnumerator<TAccumulate> Core(CancellationToken cancellationToken)
  73. #endif
  74. {
  75. var res = seed;
  76. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  77. {
  78. res = accumulator(res, item);
  79. yield return res;
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// Applies an asynchronous accumulator function over an async-enumerable sequence and returns each intermediate result.
  85. /// For aggregation behavior with no intermediate results, see <see cref="AsyncEnumerable.AggregateAsync{TSource}"/>.
  86. /// </summary>
  87. /// <typeparam name="TSource">The type of the elements in the source sequence and the result of the aggregation.</typeparam>
  88. /// <param name="source">An async-enumerable sequence to accumulate over.</param>
  89. /// <param name="accumulator">An asynchronous accumulator function to be invoked and awaited on each element.</param>
  90. /// <returns>An async-enumerable sequence containing the accumulated values.</returns>
  91. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="accumulator"/> is null.</exception>
  92. public static IAsyncEnumerable<TSource> Scan<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> accumulator)
  93. {
  94. if (source == null)
  95. throw Error.ArgumentNull(nameof(source));
  96. if (accumulator == null)
  97. throw Error.ArgumentNull(nameof(accumulator));
  98. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  99. return Core(source, accumulator);
  100. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> accumulator, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  101. #else
  102. return AsyncEnumerable.Create(Core);
  103. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  104. #endif
  105. {
  106. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  107. if (!await e.MoveNextAsync())
  108. {
  109. yield break;
  110. }
  111. var res = e.Current;
  112. while (await e.MoveNextAsync())
  113. {
  114. res = await accumulator(res, e.Current).ConfigureAwait(false);
  115. yield return res;
  116. }
  117. }
  118. }
  119. #if !NO_DEEP_CANCELLATION
  120. /// <summary>
  121. /// Applies an asynchronous (cancellable) accumulator function over an async-enumerable sequence and returns each intermediate result.
  122. /// For aggregation behavior with no intermediate results, see <see cref="AsyncEnumerable.AggregateAsync{TSource}"/>.
  123. /// </summary>
  124. /// <typeparam name="TSource">The type of the elements in the source sequence and the result of the aggregation.</typeparam>
  125. /// <param name="source">An async-enumerable sequence to accumulate over.</param>
  126. /// <param name="accumulator">An asynchronous (cancellable) accumulator function to be invoked and awaited on each element.</param>
  127. /// <returns>An async-enumerable sequence containing the accumulated values.</returns>
  128. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="accumulator"/> is null.</exception>
  129. public static IAsyncEnumerable<TSource> Scan<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, ValueTask<TSource>> accumulator)
  130. {
  131. if (source == null)
  132. throw Error.ArgumentNull(nameof(source));
  133. if (accumulator == null)
  134. throw Error.ArgumentNull(nameof(accumulator));
  135. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  136. return Core(source, accumulator);
  137. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, ValueTask<TSource>> accumulator, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  138. #else
  139. return AsyncEnumerable.Create(Core);
  140. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  141. #endif
  142. {
  143. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  144. if (!await e.MoveNextAsync())
  145. {
  146. yield break;
  147. }
  148. var res = e.Current;
  149. while (await e.MoveNextAsync())
  150. {
  151. res = await accumulator(res, e.Current, cancellationToken).ConfigureAwait(false);
  152. yield return res;
  153. }
  154. }
  155. }
  156. #endif
  157. /// <summary>
  158. /// 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.
  159. /// For aggregation behavior with no intermediate results, see <see cref="AsyncEnumerable.AggregateAsync{TSource, Accumulate}"/>.
  160. /// </summary>
  161. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  162. /// <typeparam name="TAccumulate">The type of the result of the aggregation.</typeparam>
  163. /// <param name="source">An async-enumerable sequence to accumulate over.</param>
  164. /// <param name="seed">The initial accumulator value.</param>
  165. /// <param name="accumulator">An asynchronous accumulator function to be invoked on each element.</param>
  166. /// <returns>An async-enumerable sequence containing the accumulated values.</returns>
  167. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="accumulator"/> is null.</exception>
  168. public static IAsyncEnumerable<TAccumulate> Scan<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> accumulator)
  169. {
  170. if (source == null)
  171. throw Error.ArgumentNull(nameof(source));
  172. if (accumulator == null)
  173. throw Error.ArgumentNull(nameof(accumulator));
  174. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  175. return Core(source, seed, accumulator);
  176. static async IAsyncEnumerable<TAccumulate> Core(IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> accumulator, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  177. #else
  178. return AsyncEnumerable.Create(Core);
  179. async IAsyncEnumerator<TAccumulate> Core(CancellationToken cancellationToken)
  180. #endif
  181. {
  182. var res = seed;
  183. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  184. {
  185. res = await accumulator(res, item).ConfigureAwait(false);
  186. yield return res;
  187. }
  188. }
  189. }
  190. #if !NO_DEEP_CANCELLATION
  191. /// <summary>
  192. /// 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.
  193. /// For aggregation behavior with no intermediate results, see <see cref="AsyncEnumerable.AggregateAsync{TSource, Accumulate}"/>.
  194. /// </summary>
  195. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  196. /// <typeparam name="TAccumulate">The type of the result of the aggregation.</typeparam>
  197. /// <param name="source">An async-enumerable sequence to accumulate over.</param>
  198. /// <param name="seed">The initial accumulator value.</param>
  199. /// <param name="accumulator">An asynchronous (cancellable) accumulator function to be invoked on each element.</param>
  200. /// <returns>An async-enumerable sequence containing the accumulated values.</returns>
  201. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="accumulator"/> is null.</exception>
  202. public static IAsyncEnumerable<TAccumulate> Scan<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> accumulator)
  203. {
  204. if (source == null)
  205. throw Error.ArgumentNull(nameof(source));
  206. if (accumulator == null)
  207. throw Error.ArgumentNull(nameof(accumulator));
  208. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  209. return Core(source, seed, accumulator);
  210. static async IAsyncEnumerable<TAccumulate> Core(IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> accumulator, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  211. #else
  212. return AsyncEnumerable.Create(Core);
  213. async IAsyncEnumerator<TAccumulate> Core(CancellationToken cancellationToken)
  214. #endif
  215. {
  216. var res = seed;
  217. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  218. {
  219. res = await accumulator(res, item, cancellationToken).ConfigureAwait(false);
  220. yield return res;
  221. }
  222. }
  223. }
  224. #endif
  225. }
  226. }