TakeWhile.cs 11 KB

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