Zip.cs 7.5 KB

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