1
0

Zip.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  12. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.zip?view=net-9.0-pp#system-linq-asyncenumerable-zip-2(system-collections-generic-iasyncenumerable((-0))-system-collections-generic-iasyncenumerable((-1)))
  13. /// <summary>
  14. /// Merges two async-enumerable sequences into one async-enumerable sequence by combining their elements in a pairwise fashion.
  15. /// </summary>
  16. /// <typeparam name="TFirst">The type of the elements in the first source sequence.</typeparam>
  17. /// <typeparam name="TSecond">The type of the elements in the second source sequence.</typeparam>
  18. /// <param name="first">First async-enumerable source.</param>
  19. /// <param name="second">Second async-enumerable source.</param>
  20. /// <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>
  21. /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> is null.</exception>
  22. public static IAsyncEnumerable<(TFirst First, TSecond Second)> Zip<TFirst, TSecond>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second)
  23. {
  24. if (first == null)
  25. throw Error.ArgumentNull(nameof(first));
  26. if (second == null)
  27. throw Error.ArgumentNull(nameof(second));
  28. return Core(first, second);
  29. static async IAsyncEnumerable<(TFirst, TSecond)> Core(IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  30. {
  31. await using var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false);
  32. await using var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false);
  33. while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
  34. {
  35. yield return (e1.Current, e2.Current);
  36. }
  37. }
  38. }
  39. // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.zip?view=net-9.0-pp#system-linq-asyncenumerable-zip-3(system-collections-generic-iasyncenumerable((-0))-system-collections-generic-iasyncenumerable((-1))-system-func((-0-1-2)))
  40. /// <summary>
  41. /// Merges two async-enumerable sequences into one async-enumerable sequence by combining their elements in a pairwise fashion.
  42. /// </summary>
  43. /// <typeparam name="TFirst">The type of the elements in the first source sequence.</typeparam>
  44. /// <typeparam name="TSecond">The type of the elements in the second source sequence.</typeparam>
  45. /// <typeparam name="TResult">The type of the elements in the result sequence, returned by the selector function.</typeparam>
  46. /// <param name="first">First async-enumerable source.</param>
  47. /// <param name="second">Second async-enumerable source.</param>
  48. /// <param name="selector">Function to invoke for each consecutive pair of elements from the first and second source.</param>
  49. /// <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>
  50. /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> or <paramref name="selector"/> is null.</exception>
  51. public static IAsyncEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> selector)
  52. {
  53. if (first == null)
  54. throw Error.ArgumentNull(nameof(first));
  55. if (second == null)
  56. throw Error.ArgumentNull(nameof(second));
  57. if (selector == null)
  58. throw Error.ArgumentNull(nameof(selector));
  59. return Core(first, second, selector);
  60. static async IAsyncEnumerable<TResult> Core(IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> selector, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  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. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  71. /// <summary>
  72. /// Merges two async-enumerable sequences into one async-enumerable sequence by combining their elements in a pairwise fashion.
  73. /// </summary>
  74. /// <typeparam name="TFirst">The type of the elements in the first source sequence.</typeparam>
  75. /// <typeparam name="TSecond">The type of the elements in the second source sequence.</typeparam>
  76. /// <typeparam name="TResult">The type of the elements in the result sequence, returned by the selector function.</typeparam>
  77. /// <param name="first">First async-enumerable source.</param>
  78. /// <param name="second">Second async-enumerable source.</param>
  79. /// <param name="selector">An asynchronous function to invoke and await for each consecutive pair of elements from the first and second source.</param>
  80. /// <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>
  81. /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> or <paramref name="selector"/> is null.</exception>
  82. [GenerateAsyncOverload]
  83. [Obsolete("Use Zip. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ZipAwait functionality now exists as overloads of Zip.")]
  84. private static IAsyncEnumerable<TResult> ZipAwaitCore<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, ValueTask<TResult>> selector)
  85. {
  86. if (first == null)
  87. throw Error.ArgumentNull(nameof(first));
  88. if (second == null)
  89. throw Error.ArgumentNull(nameof(second));
  90. if (selector == null)
  91. throw Error.ArgumentNull(nameof(selector));
  92. return Core(first, second, selector);
  93. static async IAsyncEnumerable<TResult> Core(IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, ValueTask<TResult>> selector, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  94. {
  95. await using var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false);
  96. await using var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false);
  97. while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
  98. {
  99. yield return await selector(e1.Current, e2.Current).ConfigureAwait(false);
  100. }
  101. }
  102. }
  103. #if !NO_DEEP_CANCELLATION
  104. [GenerateAsyncOverload]
  105. [Obsolete("Use Zip. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ZipAwaitWithCancellation functionality now exists as overloads of Zip.")]
  106. private static IAsyncEnumerable<TResult> ZipAwaitWithCancellationCore<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, CancellationToken, ValueTask<TResult>> selector)
  107. {
  108. if (first == null)
  109. throw Error.ArgumentNull(nameof(first));
  110. if (second == null)
  111. throw Error.ArgumentNull(nameof(second));
  112. if (selector == null)
  113. throw Error.ArgumentNull(nameof(selector));
  114. return Core(first, second, selector);
  115. 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)
  116. {
  117. await using var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false);
  118. await using var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false);
  119. while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
  120. {
  121. yield return await selector(e1.Current, e2.Current, cancellationToken).ConfigureAwait(false);
  122. }
  123. }
  124. }
  125. #endif
  126. }
  127. }