Any.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 AsyncEnumerable
  10. {
  11. public static Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. if (predicate == null)
  16. throw new ArgumentNullException(nameof(predicate));
  17. return Any(source, predicate, CancellationToken.None);
  18. }
  19. public static Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate)
  20. {
  21. if (source == null)
  22. throw new ArgumentNullException(nameof(source));
  23. if (predicate == null)
  24. throw new ArgumentNullException(nameof(predicate));
  25. return Any(source, predicate, CancellationToken.None);
  26. }
  27. public static Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source)
  28. {
  29. if (source == null)
  30. throw new ArgumentNullException(nameof(source));
  31. return Any(source, CancellationToken.None);
  32. }
  33. public static Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  34. {
  35. if (source == null)
  36. throw new ArgumentNullException(nameof(source));
  37. if (predicate == null)
  38. throw new ArgumentNullException(nameof(predicate));
  39. return AnyCore(source, predicate, cancellationToken);
  40. }
  41. public static Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
  42. {
  43. if (source == null)
  44. throw new ArgumentNullException(nameof(source));
  45. if (predicate == null)
  46. throw new ArgumentNullException(nameof(predicate));
  47. return AnyCore(source, predicate, cancellationToken);
  48. }
  49. public static async Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  50. {
  51. if (source == null)
  52. throw new ArgumentNullException(nameof(source));
  53. var e = source.GetAsyncEnumerator(cancellationToken);
  54. try
  55. {
  56. return await e.MoveNextAsync().ConfigureAwait(false);
  57. }
  58. finally
  59. {
  60. await e.DisposeAsync().ConfigureAwait(false);
  61. }
  62. }
  63. private static async Task<bool> AnyCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  64. {
  65. var e = source.GetAsyncEnumerator(cancellationToken);
  66. try
  67. {
  68. while (await e.MoveNextAsync().ConfigureAwait(false))
  69. {
  70. if (predicate(e.Current))
  71. return true;
  72. }
  73. }
  74. finally
  75. {
  76. await e.DisposeAsync().ConfigureAwait(false);
  77. }
  78. return false;
  79. }
  80. private static async Task<bool> AnyCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
  81. {
  82. var e = source.GetAsyncEnumerator(cancellationToken);
  83. try
  84. {
  85. while (await e.MoveNextAsync().ConfigureAwait(false))
  86. {
  87. if (await predicate(e.Current).ConfigureAwait(false))
  88. return true;
  89. }
  90. }
  91. finally
  92. {
  93. await e.DisposeAsync().ConfigureAwait(false);
  94. }
  95. return false;
  96. }
  97. }
  98. }