TakeWhile.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 AsyncEnumerable
  10. {
  11. /// <summary>
  12. /// Returns elements from an async-enumerable sequence as long as a specified condition is true.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  15. /// <param name="source">A sequence to return elements from.</param>
  16. /// <param name="predicate">A function to test each element for a condition.</param>
  17. /// <returns>An async-enumerable sequence that contains the elements from the input sequence that occur before the element at which the test no longer passes.</returns>
  18. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
  19. public static IAsyncEnumerable<TSource> TakeWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  20. {
  21. if (source == null)
  22. throw Error.ArgumentNull(nameof(source));
  23. if (predicate == null)
  24. throw Error.ArgumentNull(nameof(predicate));
  25. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  26. return Core(source, predicate);
  27. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  28. #else
  29. return Create(Core);
  30. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  31. #endif
  32. {
  33. await foreach (var element in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  34. {
  35. if (!predicate(element))
  36. {
  37. break;
  38. }
  39. yield return element;
  40. }
  41. }
  42. }
  43. /// <summary>
  44. /// Returns elements from an async-enumerable sequence as long as a specified condition is true.
  45. /// The element's index is used in the logic of the predicate function.
  46. /// </summary>
  47. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  48. /// <param name="source">A sequence to return elements from.</param>
  49. /// <param name="predicate">A function to test each element for a condition; the second parameter of the function represents the index of the source element.</param>
  50. /// <returns>An async-enumerable sequence that contains the elements from the input sequence that occur before the element at which the test no longer passes.</returns>
  51. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
  52. public static IAsyncEnumerable<TSource> TakeWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  53. {
  54. if (source == null)
  55. throw Error.ArgumentNull(nameof(source));
  56. if (predicate == null)
  57. throw Error.ArgumentNull(nameof(predicate));
  58. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  59. return Core(source, predicate);
  60. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  61. #else
  62. return Create(Core);
  63. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  64. #endif
  65. {
  66. var index = -1;
  67. await foreach (var element in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  68. {
  69. checked
  70. {
  71. index++;
  72. }
  73. if (!predicate(element, index))
  74. {
  75. break;
  76. }
  77. yield return element;
  78. }
  79. }
  80. }
  81. internal static IAsyncEnumerable<TSource> TakeWhileAwaitCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate)
  82. {
  83. if (source == null)
  84. throw Error.ArgumentNull(nameof(source));
  85. if (predicate == null)
  86. throw Error.ArgumentNull(nameof(predicate));
  87. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  88. return Core(source, predicate);
  89. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  90. #else
  91. return Create(Core);
  92. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  93. #endif
  94. {
  95. await foreach (var element in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  96. {
  97. if (!await predicate(element).ConfigureAwait(false))
  98. {
  99. break;
  100. }
  101. yield return element;
  102. }
  103. }
  104. }
  105. #if !NO_DEEP_CANCELLATION
  106. internal static IAsyncEnumerable<TSource> TakeWhileAwaitWithCancellationCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate)
  107. {
  108. if (source == null)
  109. throw Error.ArgumentNull(nameof(source));
  110. if (predicate == null)
  111. throw Error.ArgumentNull(nameof(predicate));
  112. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  113. return Core(source, predicate);
  114. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  115. #else
  116. return Create(Core);
  117. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  118. #endif
  119. {
  120. await foreach (var element in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  121. {
  122. if (!await predicate(element, cancellationToken).ConfigureAwait(false))
  123. {
  124. break;
  125. }
  126. yield return element;
  127. }
  128. }
  129. }
  130. #endif
  131. internal static IAsyncEnumerable<TSource> TakeWhileAwaitCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, ValueTask<bool>> predicate)
  132. {
  133. if (source == null)
  134. throw Error.ArgumentNull(nameof(source));
  135. if (predicate == null)
  136. throw Error.ArgumentNull(nameof(predicate));
  137. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  138. return Core(source, predicate);
  139. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, int, ValueTask<bool>> predicate, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  140. #else
  141. return Create(Core);
  142. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  143. #endif
  144. {
  145. var index = -1;
  146. await foreach (var element in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  147. {
  148. checked
  149. {
  150. index++;
  151. }
  152. if (!await predicate(element, index).ConfigureAwait(false))
  153. {
  154. break;
  155. }
  156. yield return element;
  157. }
  158. }
  159. }
  160. #if !NO_DEEP_CANCELLATION
  161. internal static IAsyncEnumerable<TSource> TakeWhileAwaitWithCancellationCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, ValueTask<bool>> predicate)
  162. {
  163. if (source == null)
  164. throw Error.ArgumentNull(nameof(source));
  165. if (predicate == null)
  166. throw Error.ArgumentNull(nameof(predicate));
  167. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  168. return Core(source, predicate);
  169. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, ValueTask<bool>> predicate, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  170. #else
  171. return Create(Core);
  172. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  173. #endif
  174. {
  175. var index = -1;
  176. await foreach (var element in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  177. {
  178. checked
  179. {
  180. index++;
  181. }
  182. if (!await predicate(element, index, cancellationToken).ConfigureAwait(false))
  183. {
  184. break;
  185. }
  186. yield return element;
  187. }
  188. }
  189. }
  190. #endif
  191. }
  192. }