Zip.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #if HAS_VALUETUPLE
  12. public static IAsyncEnumerable<(TFirst First, TSecond Second)> Zip<TFirst, TSecond>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second)
  13. {
  14. if (first == null)
  15. throw Error.ArgumentNull(nameof(first));
  16. if (second == null)
  17. throw Error.ArgumentNull(nameof(second));
  18. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  19. return Core();
  20. async IAsyncEnumerable<(TFirst, TSecond)> Core([System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  21. #else
  22. return Create(Core);
  23. async IAsyncEnumerator<(TFirst, TSecond)> Core(CancellationToken cancellationToken)
  24. #endif
  25. {
  26. await using var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false);
  27. await using var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false);
  28. while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
  29. {
  30. yield return (e1.Current, e2.Current);
  31. }
  32. }
  33. }
  34. #endif
  35. /// <summary>
  36. /// Merges two async-enumerable sequences into one async-enumerable sequence by combining their elements in a pairwise fashion.
  37. /// </summary>
  38. /// <typeparam name="TFirst">The type of the elements in the first source sequence.</typeparam>
  39. /// <typeparam name="TSecond">The type of the elements in the second source sequence.</typeparam>
  40. /// <typeparam name="TResult">The type of the elements in the result sequence, returned by the selector function.</typeparam>
  41. /// <param name="first">First async-enumerable source.</param>
  42. /// <param name="second">Second async-enumerable source.</param>
  43. /// <param name="selector">Function to invoke for each consecutive pair of elements from the first and second source.</param>
  44. /// <returns>An async-enumerable sequence containing the result of pairwise combining the elements of the first and second source using the specified result selector function.</returns>
  45. /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> or <paramref name="selector"/> is null.</exception>
  46. public static IAsyncEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> selector)
  47. {
  48. if (first == null)
  49. throw Error.ArgumentNull(nameof(first));
  50. if (second == null)
  51. throw Error.ArgumentNull(nameof(second));
  52. if (selector == null)
  53. throw Error.ArgumentNull(nameof(selector));
  54. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  55. return Core();
  56. async IAsyncEnumerable<TResult> Core([System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  57. #else
  58. return Create(Core);
  59. async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
  60. #endif
  61. {
  62. await using var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false);
  63. await using var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false);
  64. while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
  65. {
  66. yield return selector(e1.Current, e2.Current);
  67. }
  68. }
  69. }
  70. internal static IAsyncEnumerable<TResult> ZipAwaitCore<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, ValueTask<TResult>> selector)
  71. {
  72. if (first == null)
  73. throw Error.ArgumentNull(nameof(first));
  74. if (second == null)
  75. throw Error.ArgumentNull(nameof(second));
  76. if (selector == null)
  77. throw Error.ArgumentNull(nameof(selector));
  78. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  79. return Core();
  80. async IAsyncEnumerable<TResult> Core([System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  81. #else
  82. return Create(Core);
  83. async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
  84. #endif
  85. {
  86. await using var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false);
  87. await using var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false);
  88. while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
  89. {
  90. yield return await selector(e1.Current, e2.Current).ConfigureAwait(false);
  91. }
  92. }
  93. }
  94. #if !NO_DEEP_CANCELLATION
  95. internal static IAsyncEnumerable<TResult> ZipAwaitWithCancellationCore<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, CancellationToken, ValueTask<TResult>> selector)
  96. {
  97. if (first == null)
  98. throw Error.ArgumentNull(nameof(first));
  99. if (second == null)
  100. throw Error.ArgumentNull(nameof(second));
  101. if (selector == null)
  102. throw Error.ArgumentNull(nameof(selector));
  103. return Create(Core);
  104. async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
  105. {
  106. await using var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false);
  107. await using var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false);
  108. while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
  109. {
  110. yield return await selector(e1.Current, e2.Current, cancellationToken).ConfigureAwait(false);
  111. }
  112. }
  113. }
  114. #endif
  115. }
  116. }