FirstOrDefault.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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<TSource> FirstOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  12. {
  13. if (source == null)
  14. throw Error.ArgumentNull(nameof(source));
  15. return Core(source, cancellationToken);
  16. static async Task<TSource> Core(IAsyncEnumerable<TSource> _source, CancellationToken _cancellationToken)
  17. {
  18. var first = await TryGetFirst(_source, _cancellationToken).ConfigureAwait(false);
  19. return first.HasValue ? first.Value : default;
  20. }
  21. }
  22. public static Task<TSource> FirstOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
  23. {
  24. if (source == null)
  25. throw Error.ArgumentNull(nameof(source));
  26. if (predicate == null)
  27. throw Error.ArgumentNull(nameof(predicate));
  28. return Core(source, predicate, cancellationToken);
  29. static async Task<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSource, bool> _predicate, CancellationToken _cancellationToken)
  30. {
  31. var first = await TryGetFirst(_source, _predicate, _cancellationToken).ConfigureAwait(false);
  32. return first.HasValue ? first.Value : default;
  33. }
  34. }
  35. public static Task<TSource> FirstOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  36. {
  37. if (source == null)
  38. throw Error.ArgumentNull(nameof(source));
  39. if (predicate == null)
  40. throw Error.ArgumentNull(nameof(predicate));
  41. return Core(source, predicate, cancellationToken);
  42. static async Task<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSource, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  43. {
  44. var first = await TryGetFirst(_source, _predicate, _cancellationToken).ConfigureAwait(false);
  45. return first.HasValue ? first.Value : default;
  46. }
  47. }
  48. #if !NO_DEEP_CANCELLATION
  49. public static Task<TSource> FirstOrDefaultAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  50. {
  51. if (source == null)
  52. throw Error.ArgumentNull(nameof(source));
  53. if (predicate == null)
  54. throw Error.ArgumentNull(nameof(predicate));
  55. return Core(source, predicate, cancellationToken);
  56. static async Task<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSource, CancellationToken, ValueTask<bool>> _predicate, CancellationToken _cancellationToken)
  57. {
  58. var first = await TryGetFirst(_source, _predicate, _cancellationToken).ConfigureAwait(false);
  59. return first.HasValue ? first.Value : default;
  60. }
  61. }
  62. #endif
  63. private static ValueTask<Maybe<TSource>> TryGetFirst<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  64. {
  65. if (source is IList<TSource> list)
  66. {
  67. if (list.Count > 0)
  68. {
  69. return new ValueTask<Maybe<TSource>>(new Maybe<TSource>(list[0]));
  70. }
  71. }
  72. else if (source is IAsyncPartition<TSource> p)
  73. {
  74. return p.TryGetFirstAsync(cancellationToken);
  75. }
  76. else
  77. {
  78. return Core(source, cancellationToken);
  79. static async ValueTask<Maybe<TSource>> Core(IAsyncEnumerable<TSource> _source, CancellationToken _cancellationToken)
  80. {
  81. #if CSHARP8
  82. await using (var e = _source.GetAsyncEnumerator(_cancellationToken).ConfigureAwait(false))
  83. {
  84. if (await e.MoveNextAsync())
  85. {
  86. return new Maybe<TSource>(e.Current);
  87. }
  88. }
  89. #else
  90. var e = _source.GetAsyncEnumerator(_cancellationToken);
  91. try
  92. {
  93. if (await e.MoveNextAsync().ConfigureAwait(false))
  94. {
  95. return new Maybe<TSource>(e.Current);
  96. }
  97. }
  98. finally
  99. {
  100. await e.DisposeAsync().ConfigureAwait(false);
  101. }
  102. #endif
  103. return new Maybe<TSource>();
  104. }
  105. }
  106. return new ValueTask<Maybe<TSource>>(new Maybe<TSource>());
  107. }
  108. private static async Task<Maybe<TSource>> TryGetFirst<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  109. {
  110. #if CSHARP8
  111. await using (var e = source.GetAsyncEnumerator(cancellationToken).ConfigureAwait(false))
  112. {
  113. while (await e.MoveNextAsync())
  114. {
  115. var value = e.Current;
  116. if (predicate(value))
  117. {
  118. return new Maybe<TSource>(value);
  119. }
  120. }
  121. }
  122. #else
  123. var e = source.GetAsyncEnumerator(cancellationToken);
  124. try
  125. {
  126. while (await e.MoveNextAsync().ConfigureAwait(false))
  127. {
  128. var value = e.Current;
  129. if (predicate(value))
  130. {
  131. return new Maybe<TSource>(value);
  132. }
  133. }
  134. }
  135. finally
  136. {
  137. await e.DisposeAsync().ConfigureAwait(false);
  138. }
  139. #endif
  140. return new Maybe<TSource>();
  141. }
  142. private static async Task<Maybe<TSource>> TryGetFirst<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  143. {
  144. #if CSHARP8
  145. await using (var e = source.GetAsyncEnumerator(cancellationToken).ConfigureAwait(false))
  146. {
  147. while (await e.MoveNextAsync())
  148. {
  149. var value = e.Current;
  150. if (await predicate(value).ConfigureAwait(false))
  151. {
  152. return new Maybe<TSource>(value);
  153. }
  154. }
  155. }
  156. #else
  157. var e = source.GetAsyncEnumerator(cancellationToken);
  158. try
  159. {
  160. while (await e.MoveNextAsync().ConfigureAwait(false))
  161. {
  162. var value = e.Current;
  163. if (await predicate(value).ConfigureAwait(false))
  164. {
  165. return new Maybe<TSource>(value);
  166. }
  167. }
  168. }
  169. finally
  170. {
  171. await e.DisposeAsync().ConfigureAwait(false);
  172. }
  173. #endif
  174. return new Maybe<TSource>();
  175. }
  176. #if !NO_DEEP_CANCELLATION
  177. private static async Task<Maybe<TSource>> TryGetFirst<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  178. {
  179. #if CSHARP8
  180. await using (var e = source.GetAsyncEnumerator(cancellationToken).ConfigureAwait(false))
  181. {
  182. while (await e.MoveNextAsync())
  183. {
  184. var value = e.Current;
  185. if (await predicate(value, cancellationToken).ConfigureAwait(false))
  186. {
  187. return new Maybe<TSource>(value);
  188. }
  189. }
  190. }
  191. #else
  192. var e = source.GetAsyncEnumerator(cancellationToken);
  193. try
  194. {
  195. while (await e.MoveNextAsync().ConfigureAwait(false))
  196. {
  197. var value = e.Current;
  198. if (await predicate(value, cancellationToken).ConfigureAwait(false))
  199. {
  200. return new Maybe<TSource>(value);
  201. }
  202. }
  203. }
  204. finally
  205. {
  206. await e.DisposeAsync().ConfigureAwait(false);
  207. }
  208. #endif
  209. return new Maybe<TSource>();
  210. }
  211. #endif
  212. }
  213. }