Zip.cs 7.5 KB

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