Any.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 AnyCore(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 AnyCore(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 AnyCore(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 Task<bool> Any<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  50. {
  51. if (source == null)
  52. throw new ArgumentNullException(nameof(source));
  53. return AnyCore(source, cancellationToken);
  54. }
  55. private static async Task<bool> AnyCore<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  56. {
  57. var e = source.GetAsyncEnumerator(cancellationToken);
  58. try
  59. {
  60. return await e.MoveNextAsync().ConfigureAwait(false);
  61. }
  62. finally
  63. {
  64. await e.DisposeAsync().ConfigureAwait(false);
  65. }
  66. }
  67. private static async Task<bool> AnyCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  68. {
  69. var e = source.GetAsyncEnumerator(cancellationToken);
  70. try
  71. {
  72. while (await e.MoveNextAsync().ConfigureAwait(false))
  73. {
  74. if (predicate(e.Current))
  75. return true;
  76. }
  77. }
  78. finally
  79. {
  80. await e.DisposeAsync().ConfigureAwait(false);
  81. }
  82. return false;
  83. }
  84. private static async Task<bool> AnyCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task<bool>> predicate, CancellationToken cancellationToken)
  85. {
  86. var e = source.GetAsyncEnumerator(cancellationToken);
  87. try
  88. {
  89. while (await e.MoveNextAsync().ConfigureAwait(false))
  90. {
  91. if (await predicate(e.Current).ConfigureAwait(false))
  92. return true;
  93. }
  94. }
  95. finally
  96. {
  97. await e.DisposeAsync().ConfigureAwait(false);
  98. }
  99. return false;
  100. }
  101. }
  102. }