Scan.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 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. public static IAsyncEnumerable<TSource> Scan<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator)
  15. {
  16. if (source == null)
  17. throw Error.ArgumentNull(nameof(source));
  18. if (accumulator == null)
  19. throw Error.ArgumentNull(nameof(accumulator));
  20. return AsyncEnumerable.Create(Core);
  21. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  22. {
  23. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  24. {
  25. if (!await e.MoveNextAsync())
  26. {
  27. yield break;
  28. }
  29. var res = e.Current;
  30. while (await e.MoveNextAsync())
  31. {
  32. res = accumulator(res, e.Current);
  33. yield return res;
  34. }
  35. }
  36. }
  37. }
  38. public static IAsyncEnumerable<TAccumulate> Scan<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)
  39. {
  40. if (source == null)
  41. throw Error.ArgumentNull(nameof(source));
  42. if (accumulator == null)
  43. throw Error.ArgumentNull(nameof(accumulator));
  44. return AsyncEnumerable.Create(Core);
  45. async IAsyncEnumerator<TAccumulate> Core(CancellationToken cancellationToken)
  46. {
  47. var res = seed;
  48. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  49. {
  50. res = accumulator(res, item);
  51. yield return res;
  52. }
  53. }
  54. }
  55. public static IAsyncEnumerable<TSource> Scan<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> accumulator)
  56. {
  57. if (source == null)
  58. throw Error.ArgumentNull(nameof(source));
  59. if (accumulator == null)
  60. throw Error.ArgumentNull(nameof(accumulator));
  61. return AsyncEnumerable.Create(Core);
  62. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  63. {
  64. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  65. {
  66. if (!await e.MoveNextAsync())
  67. {
  68. yield break;
  69. }
  70. var res = e.Current;
  71. while (await e.MoveNextAsync())
  72. {
  73. res = await accumulator(res, e.Current).ConfigureAwait(false);
  74. yield return res;
  75. }
  76. }
  77. }
  78. }
  79. #if !NO_DEEP_CANCELLATION
  80. public static IAsyncEnumerable<TSource> Scan<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, ValueTask<TSource>> accumulator)
  81. {
  82. if (source == null)
  83. throw Error.ArgumentNull(nameof(source));
  84. if (accumulator == null)
  85. throw Error.ArgumentNull(nameof(accumulator));
  86. return AsyncEnumerable.Create(Core);
  87. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  88. {
  89. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  90. {
  91. if (!await e.MoveNextAsync())
  92. {
  93. yield break;
  94. }
  95. var res = e.Current;
  96. while (await e.MoveNextAsync())
  97. {
  98. res = await accumulator(res, e.Current, cancellationToken).ConfigureAwait(false);
  99. yield return res;
  100. }
  101. }
  102. }
  103. }
  104. #endif
  105. public static IAsyncEnumerable<TAccumulate> Scan<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> accumulator)
  106. {
  107. if (source == null)
  108. throw Error.ArgumentNull(nameof(source));
  109. if (accumulator == null)
  110. throw Error.ArgumentNull(nameof(accumulator));
  111. return AsyncEnumerable.Create(Core);
  112. async IAsyncEnumerator<TAccumulate> Core(CancellationToken cancellationToken)
  113. {
  114. var res = seed;
  115. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  116. {
  117. res = await accumulator(res, item).ConfigureAwait(false);
  118. yield return res;
  119. }
  120. }
  121. }
  122. #if !NO_DEEP_CANCELLATION
  123. public static IAsyncEnumerable<TAccumulate> Scan<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> accumulator)
  124. {
  125. if (source == null)
  126. throw Error.ArgumentNull(nameof(source));
  127. if (accumulator == null)
  128. throw Error.ArgumentNull(nameof(accumulator));
  129. return AsyncEnumerable.Create(Core);
  130. async IAsyncEnumerator<TAccumulate> Core(CancellationToken cancellationToken)
  131. {
  132. var res = seed;
  133. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  134. {
  135. res = await accumulator(res, item, cancellationToken).ConfigureAwait(false);
  136. yield return res;
  137. }
  138. }
  139. }
  140. #endif
  141. }
  142. }