Zip.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.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 Create(Core);
  19. async IAsyncEnumerator<(TFirst, TSecond)> Core(CancellationToken cancellationToken)
  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. public static IAsyncEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> selector)
  31. {
  32. if (first == null)
  33. throw Error.ArgumentNull(nameof(first));
  34. if (second == null)
  35. throw Error.ArgumentNull(nameof(second));
  36. if (selector == null)
  37. throw Error.ArgumentNull(nameof(selector));
  38. return Create(Core);
  39. async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
  40. {
  41. await using var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false);
  42. await using var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false);
  43. while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
  44. {
  45. yield return selector(e1.Current, e2.Current);
  46. }
  47. }
  48. }
  49. internal static IAsyncEnumerable<TResult> ZipAwaitCore<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, ValueTask<TResult>> selector)
  50. {
  51. if (first == null)
  52. throw Error.ArgumentNull(nameof(first));
  53. if (second == null)
  54. throw Error.ArgumentNull(nameof(second));
  55. if (selector == null)
  56. throw Error.ArgumentNull(nameof(selector));
  57. return Create(Core);
  58. async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
  59. {
  60. await using var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false);
  61. await using var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false);
  62. while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
  63. {
  64. yield return await selector(e1.Current, e2.Current).ConfigureAwait(false);
  65. }
  66. }
  67. }
  68. #if !NO_DEEP_CANCELLATION
  69. internal static IAsyncEnumerable<TResult> ZipAwaitWithCancellationCore<TFirst, TSecond, TResult>(this IAsyncEnumerable<TFirst> first, IAsyncEnumerable<TSecond> second, Func<TFirst, TSecond, CancellationToken, ValueTask<TResult>> selector)
  70. {
  71. if (first == null)
  72. throw Error.ArgumentNull(nameof(first));
  73. if (second == null)
  74. throw Error.ArgumentNull(nameof(second));
  75. if (selector == null)
  76. throw Error.ArgumentNull(nameof(selector));
  77. return Create(Core);
  78. async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
  79. {
  80. await using var e1 = first.GetConfiguredAsyncEnumerator(cancellationToken, false);
  81. await using var e2 = second.GetConfiguredAsyncEnumerator(cancellationToken, false);
  82. while (await e1.MoveNextAsync() && await e2.MoveNextAsync())
  83. {
  84. yield return await selector(e1.Current, e2.Current, cancellationToken).ConfigureAwait(false);
  85. }
  86. }
  87. }
  88. #endif
  89. }
  90. }