Zip.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerable
  11. {
  12. #if HAS_VALUETUPLE
  13. public static IAsyncEnumerable<(TFirst First, TSecond Second)> Zip<TFirst, TSecond>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second)
  14. {
  15. if (first == null)
  16. throw Error.ArgumentNull(nameof(first));
  17. if (second == null)
  18. throw Error.ArgumentNull(nameof(second));
  19. return Create(Core);
  20. async IAsyncEnumerator<(TFirst, TSecond)> Core(CancellationToken cancellationToken)
  21. {
  22. await using (var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false))
  23. {
  24. await using (var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false))
  25. {
  26. while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
  27. {
  28. yield return (e1.Current, e2.Current);
  29. }
  30. }
  31. }
  32. }
  33. }
  34. #endif
  35. public static IAsyncEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> selector)
  36. {
  37. if (first == null)
  38. throw Error.ArgumentNull(nameof(first));
  39. if (second == null)
  40. throw Error.ArgumentNull(nameof(second));
  41. if (selector == null)
  42. throw Error.ArgumentNull(nameof(selector));
  43. return Create(Core);
  44. async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
  45. {
  46. await using (var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false))
  47. {
  48. await using (var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false))
  49. {
  50. while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
  51. {
  52. yield return selector(e1.Current, e2.Current);
  53. }
  54. }
  55. }
  56. }
  57. }
  58. internal static IAsyncEnumerable<TResult> ZipAwaitCore<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, ValueTask<TResult>> selector)
  59. {
  60. if (first == null)
  61. throw Error.ArgumentNull(nameof(first));
  62. if (second == null)
  63. throw Error.ArgumentNull(nameof(second));
  64. if (selector == null)
  65. throw Error.ArgumentNull(nameof(selector));
  66. return Create(Core);
  67. async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
  68. {
  69. await using (var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false))
  70. {
  71. await using (var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false))
  72. {
  73. while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
  74. {
  75. yield return await selector(e1.Current, e2.Current).ConfigureAwait(false);
  76. }
  77. }
  78. }
  79. }
  80. }
  81. #if !NO_DEEP_CANCELLATION
  82. internal static IAsyncEnumerable<TResult> ZipAwaitWithCancellationCore<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, CancellationToken, ValueTask<TResult>> selector)
  83. {
  84. if (first == null)
  85. throw Error.ArgumentNull(nameof(first));
  86. if (second == null)
  87. throw Error.ArgumentNull(nameof(second));
  88. if (selector == null)
  89. throw Error.ArgumentNull(nameof(selector));
  90. return Create(Core);
  91. async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
  92. {
  93. await using (var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false))
  94. {
  95. await using (var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false))
  96. {
  97. while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
  98. {
  99. yield return await selector(e1.Current, e2.Current, cancellationToken).ConfigureAwait(false);
  100. }
  101. }
  102. }
  103. }
  104. }
  105. #endif
  106. }
  107. }