Zip.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. internal static IAsyncEnumerable<TResult> ZipAwaitCore<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, ValueTask<TResult>> selector)
  61. {
  62. if (first == null)
  63. throw Error.ArgumentNull(nameof(first));
  64. if (second == null)
  65. throw Error.ArgumentNull(nameof(second));
  66. if (selector == null)
  67. throw Error.ArgumentNull(nameof(selector));
  68. return Core(first, second, selector);
  69. static async IAsyncEnumerable<TResult> Core(IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, ValueTask<TResult>> selector, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  70. {
  71. await using var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false);
  72. await using var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false);
  73. while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
  74. {
  75. yield return await selector(e1.Current, e2.Current).ConfigureAwait(false);
  76. }
  77. }
  78. }
  79. #if !NO_DEEP_CANCELLATION
  80. internal static IAsyncEnumerable<TResult> ZipAwaitWithCancellationCore<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, CancellationToken, ValueTask<TResult>> selector)
  81. {
  82. if (first == null)
  83. throw Error.ArgumentNull(nameof(first));
  84. if (second == null)
  85. throw Error.ArgumentNull(nameof(second));
  86. if (selector == null)
  87. throw Error.ArgumentNull(nameof(selector));
  88. return Core(first, second, selector);
  89. 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)
  90. {
  91. await using var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false);
  92. await using var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false);
  93. while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
  94. {
  95. yield return await selector(e1.Current, e2.Current, cancellationToken).ConfigureAwait(false);
  96. }
  97. }
  98. }
  99. #endif
  100. }
  101. }