Last.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /// <summary>
  12. /// Returns the last element of an async-enumerable sequence.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  15. /// <param name="source">Source async-enumerable sequence.</param>
  16. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  17. /// <returns>ValueTask containing the last element in the async-enumerable sequence.</returns>
  18. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  19. /// <exception cref="InvalidOperationException">(Asynchronous) The source sequence is empty.</exception>
  20. public static ValueTask<TSource> LastAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  21. {
  22. if (source == null)
  23. throw Error.ArgumentNull(nameof(source));
  24. return Core(source, cancellationToken);
  25. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
  26. {
  27. var last = await TryGetLast(source, cancellationToken).ConfigureAwait(false);
  28. return last.HasValue ? last.Value : throw Error.NoElements();
  29. }
  30. }
  31. /// <summary>
  32. /// Returns the last element of an async-enumerable sequence that satisfies the condition in the predicate.
  33. /// </summary>
  34. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  35. /// <param name="source">Source async-enumerable sequence.</param>
  36. /// <param name="predicate">A predicate function to evaluate for elements in the source sequence.</param>
  37. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  38. /// <returns>ValueTask containing the last element in the async-enumerable sequence that satisfies the condition in the predicate.</returns>
  39. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
  40. /// <exception cref="InvalidOperationException">(Asynchronous) No element satisfies the condition in the predicate. -or- The source sequence is empty.</exception>
  41. public static ValueTask<TSource> LastAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken = default)
  42. {
  43. if (source == null)
  44. throw Error.ArgumentNull(nameof(source));
  45. if (predicate == null)
  46. throw Error.ArgumentNull(nameof(predicate));
  47. return Core(source, predicate, cancellationToken);
  48. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, CancellationToken cancellationToken)
  49. {
  50. var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);
  51. return last.HasValue ? last.Value : throw Error.NoElements();
  52. }
  53. }
  54. /// <summary>
  55. /// Returns the last element of an async-enumerable sequence that satisfies the condition in the predicate.
  56. /// </summary>
  57. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  58. /// <param name="source">Source async-enumerable sequence.</param>
  59. /// <param name="predicate">An asynchronous predicate function to evaluate for elements in the source sequence.</param>
  60. /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
  61. /// <returns>ValueTask containing the last element in the async-enumerable sequence that satisfies the condition in the predicate.</returns>
  62. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
  63. /// <exception cref="InvalidOperationException">(Asynchronous) No element satisfies the condition in the predicate. -or- The source sequence is empty.</exception>
  64. [GenerateAsyncOverload]
  65. private static ValueTask<TSource> LastAwaitAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  66. {
  67. if (source == null)
  68. throw Error.ArgumentNull(nameof(source));
  69. if (predicate == null)
  70. throw Error.ArgumentNull(nameof(predicate));
  71. return Core(source, predicate, cancellationToken);
  72. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  73. {
  74. var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);
  75. return last.HasValue ? last.Value : throw Error.NoElements();
  76. }
  77. }
  78. #if !NO_DEEP_CANCELLATION
  79. [GenerateAsyncOverload]
  80. private static ValueTask<TSource> LastAwaitWithCancellationAsyncCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken = default)
  81. {
  82. if (source == null)
  83. throw Error.ArgumentNull(nameof(source));
  84. if (predicate == null)
  85. throw Error.ArgumentNull(nameof(predicate));
  86. return Core(source, predicate, cancellationToken);
  87. static async ValueTask<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, CancellationToken cancellationToken)
  88. {
  89. var last = await TryGetLast(source, predicate, cancellationToken).ConfigureAwait(false);
  90. return last.HasValue ? last.Value : throw Error.NoElements();
  91. }
  92. }
  93. #endif
  94. }
  95. }