Scan.cs 6.2 KB

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