Aggregate.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static Task<TResult> Aggregate<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. if (accumulator == null)
  18. throw new ArgumentNullException(nameof(accumulator));
  19. if (resultSelector == null)
  20. throw new ArgumentNullException(nameof(resultSelector));
  21. return Aggregate(source, seed, accumulator, resultSelector, CancellationToken.None);
  22. }
  23. public static Task<TAccumulate> Aggregate<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)
  24. {
  25. if (source == null)
  26. throw new ArgumentNullException(nameof(source));
  27. if (accumulator == null)
  28. throw new ArgumentNullException(nameof(accumulator));
  29. return Aggregate(source, seed, accumulator, CancellationToken.None);
  30. }
  31. public static Task<TSource> Aggregate<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator)
  32. {
  33. if (source == null)
  34. throw new ArgumentNullException(nameof(source));
  35. if (accumulator == null)
  36. throw new ArgumentNullException(nameof(accumulator));
  37. return Aggregate(source, accumulator, CancellationToken.None);
  38. }
  39. public static Task<TResult> Aggregate<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector, CancellationToken cancellationToken)
  40. {
  41. if (source == null)
  42. throw new ArgumentNullException(nameof(source));
  43. if (accumulator == null)
  44. throw new ArgumentNullException(nameof(accumulator));
  45. if (resultSelector == null)
  46. throw new ArgumentNullException(nameof(resultSelector));
  47. return Aggregate_(source, seed, accumulator, resultSelector, cancellationToken);
  48. }
  49. public static Task<TAccumulate> Aggregate<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, CancellationToken cancellationToken)
  50. {
  51. if (source == null)
  52. throw new ArgumentNullException(nameof(source));
  53. if (accumulator == null)
  54. throw new ArgumentNullException(nameof(accumulator));
  55. return source.Aggregate(seed, accumulator, x => x, cancellationToken);
  56. }
  57. public static Task<TSource> Aggregate<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken)
  58. {
  59. if (source == null)
  60. throw new ArgumentNullException(nameof(source));
  61. if (accumulator == null)
  62. throw new ArgumentNullException(nameof(accumulator));
  63. return Aggregate_(source, accumulator, cancellationToken);
  64. }
  65. private static async Task<TResult> Aggregate_<TSource, TAccumulate, TResult>(IAsyncEnumerable<TSource> source, TAccumulate seed,
  66. Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector, CancellationToken cancellationToken)
  67. {
  68. var acc = seed;
  69. var e = source.GetAsyncEnumerator();
  70. try
  71. {
  72. while (await e.MoveNextAsync(cancellationToken)
  73. .ConfigureAwait(false))
  74. {
  75. acc = accumulator(acc, e.Current);
  76. }
  77. }
  78. finally
  79. {
  80. await e.DisposeAsync().ConfigureAwait(false);
  81. }
  82. return resultSelector(acc);
  83. }
  84. private static async Task<TSource> Aggregate_<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken)
  85. {
  86. var first = true;
  87. var acc = default(TSource);
  88. var e = source.GetAsyncEnumerator();
  89. try
  90. {
  91. while (await e.MoveNextAsync(cancellationToken)
  92. .ConfigureAwait(false))
  93. {
  94. acc = first ? e.Current : accumulator(acc, e.Current);
  95. first = false;
  96. }
  97. }
  98. finally
  99. {
  100. await e.DisposeAsync().ConfigureAwait(false);
  101. }
  102. if (first)
  103. throw new InvalidOperationException(Strings.NO_ELEMENTS);
  104. return acc;
  105. }
  106. }
  107. }