LastOrDefault.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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> LastOrDefaultAsync<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 last = await TryGetLast(_source, _cancellationToken).ConfigureAwait(false);
  19. return last.HasValue ? last.Value : default;
  20. }
  21. }
  22. public static Task<TSource> LastOrDefaultAsync<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 last = await TryGetLast(_source, _predicate, _cancellationToken).ConfigureAwait(false);
  32. return last.HasValue ? last.Value : default;
  33. }
  34. }
  35. public static Task<TSource> LastOrDefaultAsync<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 last = await TryGetLast(_source, _predicate, _cancellationToken).ConfigureAwait(false);
  45. return last.HasValue ? last.Value : default;
  46. }
  47. }
  48. #if !NO_DEEP_CANCELLATION
  49. public static Task<TSource> LastOrDefaultAsync<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 last = await TryGetLast(_source, _predicate, _cancellationToken).ConfigureAwait(false);
  59. return last.HasValue ? last.Value : default;
  60. }
  61. }
  62. #endif
  63. private static ValueTask<Maybe<TSource>> TryGetLast<TSource>(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  64. {
  65. if (source is IList<TSource> list)
  66. {
  67. var count = list.Count;
  68. if (count > 0)
  69. {
  70. return new ValueTask<Maybe<TSource>>(new Maybe<TSource>(list[count - 1]));
  71. }
  72. }
  73. else if (source is IAsyncPartition<TSource> p)
  74. {
  75. return p.TryGetLastAsync(cancellationToken);
  76. }
  77. else
  78. {
  79. return Core(source, cancellationToken);
  80. static async ValueTask<Maybe<TSource>> Core(IAsyncEnumerable<TSource> _source, CancellationToken _cancellationToken)
  81. {
  82. var last = default(TSource);
  83. var hasLast = false;
  84. #if CSHARP8
  85. await foreach (TSource item in _source.WithCancellation(_cancellationToken).ConfigureAwait(false))
  86. {
  87. hasLast = true;
  88. last = item;
  89. }
  90. #else
  91. var e = _source.GetAsyncEnumerator(_cancellationToken);
  92. try
  93. {
  94. while (await e.MoveNextAsync().ConfigureAwait(false))
  95. {
  96. hasLast = true;
  97. last = e.Current;
  98. }
  99. }
  100. finally
  101. {
  102. await e.DisposeAsync().ConfigureAwait(false);
  103. }
  104. #endif
  105. return hasLast ? new Maybe<TSource>(last) : new Maybe<TSource>();
  106. }
  107. }
  108. return new ValueTask<Maybe<TSource>>(new Maybe<TSource>());
  109. }
  110. private static async Task<Maybe<TSource>> TryGetLast<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  111. {
  112. var last = default(TSource);
  113. var hasLast = false;
  114. #if CSHARP8
  115. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  116. {
  117. if (predicate(item))
  118. {
  119. hasLast = true;
  120. last = item;
  121. }
  122. }
  123. #else
  124. var e = source.GetAsyncEnumerator(cancellationToken);
  125. try
  126. {
  127. while (await e.MoveNextAsync().ConfigureAwait(false))
  128. {
  129. var value = e.Current;
  130. if (predicate(value))
  131. {
  132. hasLast = true;
  133. last = value;
  134. }
  135. }
  136. }
  137. finally
  138. {
  139. await e.DisposeAsync().ConfigureAwait(false);
  140. }
  141. #endif
  142. return hasLast ? new Maybe<TSource>(last) : new Maybe<TSource>();
  143. }
  144. private static async Task<Maybe<TSource>> TryGetLast<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  145. {
  146. var last = default(TSource);
  147. var hasLast = false;
  148. #if CSHARP8
  149. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  150. {
  151. if (await predicate(item).ConfigureAwait(false))
  152. {
  153. hasLast = true;
  154. last = item;
  155. }
  156. }
  157. #else
  158. var e = source.GetAsyncEnumerator(cancellationToken);
  159. try
  160. {
  161. while (await e.MoveNextAsync().ConfigureAwait(false))
  162. {
  163. var value = e.Current;
  164. if (await predicate(value).ConfigureAwait(false))
  165. {
  166. hasLast = true;
  167. last = value;
  168. }
  169. }
  170. }
  171. finally
  172. {
  173. await e.DisposeAsync().ConfigureAwait(false);
  174. }
  175. #endif
  176. return hasLast ? new Maybe<TSource>(last) : new Maybe<TSource>();
  177. }
  178. #if !NO_DEEP_CANCELLATION
  179. private static async Task<Maybe<TSource>> TryGetLast<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  180. {
  181. var last = default(TSource);
  182. var hasLast = false;
  183. #if CSHARP8
  184. await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  185. {
  186. if (await predicate(item, cancellationToken).ConfigureAwait(false))
  187. {
  188. hasLast = true;
  189. last = item;
  190. }
  191. }
  192. #else
  193. var e = source.GetAsyncEnumerator(cancellationToken);
  194. try
  195. {
  196. while (await e.MoveNextAsync().ConfigureAwait(false))
  197. {
  198. var value = e.Current;
  199. if (await predicate(value, cancellationToken).ConfigureAwait(false))
  200. {
  201. hasLast = true;
  202. last = value;
  203. }
  204. }
  205. }
  206. finally
  207. {
  208. await e.DisposeAsync().ConfigureAwait(false);
  209. }
  210. #endif
  211. return hasLast ? new Maybe<TSource>(last) : new Maybe<TSource>();
  212. }
  213. #endif
  214. }
  215. }