Aggregate.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. public static Task<TSource> AggregateAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator, CancellationToken cancellationToken = default)
  12. {
  13. if (source == null)
  14. throw Error.ArgumentNull(nameof(source));
  15. if (accumulator == null)
  16. throw Error.ArgumentNull(nameof(accumulator));
  17. return Core(source, accumulator, cancellationToken);
  18. async Task<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSource, TSource, TSource> _accumulator, CancellationToken _cancellationToken)
  19. {
  20. var e = _source.GetAsyncEnumerator(_cancellationToken);
  21. try
  22. {
  23. if (!await e.MoveNextAsync().ConfigureAwait(false))
  24. {
  25. throw Error.NoElements();
  26. }
  27. var acc = e.Current;
  28. while (await e.MoveNextAsync().ConfigureAwait(false))
  29. {
  30. acc = _accumulator(acc, e.Current);
  31. }
  32. return acc;
  33. }
  34. finally
  35. {
  36. await e.DisposeAsync().ConfigureAwait(false);
  37. }
  38. }
  39. }
  40. public static Task<TSource> AggregateAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> accumulator, CancellationToken cancellationToken = default)
  41. {
  42. if (source == null)
  43. throw Error.ArgumentNull(nameof(source));
  44. if (accumulator == null)
  45. throw Error.ArgumentNull(nameof(accumulator));
  46. return Core(source, accumulator, cancellationToken);
  47. async Task<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSource, TSource, ValueTask<TSource>> _accumulator, CancellationToken _cancellationToken)
  48. {
  49. var e = _source.GetAsyncEnumerator(_cancellationToken);
  50. try
  51. {
  52. if (!await e.MoveNextAsync().ConfigureAwait(false))
  53. {
  54. throw Error.NoElements();
  55. }
  56. var acc = e.Current;
  57. while (await e.MoveNextAsync().ConfigureAwait(false))
  58. {
  59. acc = await _accumulator(acc, e.Current).ConfigureAwait(false);
  60. }
  61. return acc;
  62. }
  63. finally
  64. {
  65. await e.DisposeAsync().ConfigureAwait(false);
  66. }
  67. }
  68. }
  69. #if !NO_DEEP_CANCELLATION
  70. public static Task<TSource> AggregateAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, ValueTask<TSource>> accumulator, CancellationToken cancellationToken = default)
  71. {
  72. if (source == null)
  73. throw Error.ArgumentNull(nameof(source));
  74. if (accumulator == null)
  75. throw Error.ArgumentNull(nameof(accumulator));
  76. return Core(source, accumulator, cancellationToken);
  77. async Task<TSource> Core(IAsyncEnumerable<TSource> _source, Func<TSource, TSource, CancellationToken, ValueTask<TSource>> _accumulator, CancellationToken _cancellationToken)
  78. {
  79. var e = _source.GetAsyncEnumerator(_cancellationToken);
  80. try
  81. {
  82. if (!await e.MoveNextAsync().ConfigureAwait(false))
  83. {
  84. throw Error.NoElements();
  85. }
  86. var acc = e.Current;
  87. while (await e.MoveNextAsync().ConfigureAwait(false))
  88. {
  89. acc = await _accumulator(acc, e.Current, _cancellationToken).ConfigureAwait(false);
  90. }
  91. return acc;
  92. }
  93. finally
  94. {
  95. await e.DisposeAsync().ConfigureAwait(false);
  96. }
  97. }
  98. }
  99. #endif
  100. public static Task<TAccumulate> AggregateAsync<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, CancellationToken cancellationToken = default)
  101. {
  102. if (source == null)
  103. throw Error.ArgumentNull(nameof(source));
  104. if (accumulator == null)
  105. throw Error.ArgumentNull(nameof(accumulator));
  106. return Core(source, seed, accumulator, cancellationToken);
  107. async Task<TAccumulate> Core(IAsyncEnumerable<TSource> _source, TAccumulate _seed, Func<TAccumulate, TSource, TAccumulate> _accumulator, CancellationToken _cancellationToken)
  108. {
  109. var acc = _seed;
  110. var e = _source.GetAsyncEnumerator(_cancellationToken);
  111. try
  112. {
  113. while (await e.MoveNextAsync().ConfigureAwait(false))
  114. {
  115. acc = _accumulator(acc, e.Current);
  116. }
  117. }
  118. finally
  119. {
  120. await e.DisposeAsync().ConfigureAwait(false);
  121. }
  122. return acc;
  123. }
  124. }
  125. public static Task<TAccumulate> AggregateAsync<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> accumulator, CancellationToken cancellationToken = default)
  126. {
  127. if (source == null)
  128. throw Error.ArgumentNull(nameof(source));
  129. if (accumulator == null)
  130. throw Error.ArgumentNull(nameof(accumulator));
  131. return Core(source, seed, accumulator, cancellationToken);
  132. async Task<TAccumulate> Core(IAsyncEnumerable<TSource> _source, TAccumulate _seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> _accumulator, CancellationToken _cancellationToken)
  133. {
  134. var acc = _seed;
  135. var e = _source.GetAsyncEnumerator(_cancellationToken);
  136. try
  137. {
  138. while (await e.MoveNextAsync().ConfigureAwait(false))
  139. {
  140. acc = await _accumulator(acc, e.Current).ConfigureAwait(false);
  141. }
  142. }
  143. finally
  144. {
  145. await e.DisposeAsync().ConfigureAwait(false);
  146. }
  147. return acc;
  148. }
  149. }
  150. #if !NO_DEEP_CANCELLATION
  151. public static Task<TAccumulate> AggregateAsync<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> accumulator, CancellationToken cancellationToken = default)
  152. {
  153. if (source == null)
  154. throw Error.ArgumentNull(nameof(source));
  155. if (accumulator == null)
  156. throw Error.ArgumentNull(nameof(accumulator));
  157. return Core(source, seed, accumulator, cancellationToken);
  158. async Task<TAccumulate> Core(IAsyncEnumerable<TSource> _source, TAccumulate _seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> _accumulator, CancellationToken _cancellationToken)
  159. {
  160. var acc = _seed;
  161. var e = _source.GetAsyncEnumerator(_cancellationToken);
  162. try
  163. {
  164. while (await e.MoveNextAsync().ConfigureAwait(false))
  165. {
  166. acc = await _accumulator(acc, e.Current, _cancellationToken).ConfigureAwait(false);
  167. }
  168. }
  169. finally
  170. {
  171. await e.DisposeAsync().ConfigureAwait(false);
  172. }
  173. return acc;
  174. }
  175. }
  176. #endif
  177. public static Task<TResult> AggregateAsync<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector, CancellationToken cancellationToken = default)
  178. {
  179. if (source == null)
  180. throw Error.ArgumentNull(nameof(source));
  181. if (accumulator == null)
  182. throw Error.ArgumentNull(nameof(accumulator));
  183. if (resultSelector == null)
  184. throw Error.ArgumentNull(nameof(resultSelector));
  185. return Core(source, seed, accumulator, resultSelector, cancellationToken);
  186. async Task<TResult> Core(IAsyncEnumerable<TSource> _source, TAccumulate _seed, Func<TAccumulate, TSource, TAccumulate> _accumulator, Func<TAccumulate, TResult> _resultSelector, CancellationToken _cancellationToken)
  187. {
  188. var acc = _seed;
  189. var e = _source.GetAsyncEnumerator(_cancellationToken);
  190. try
  191. {
  192. while (await e.MoveNextAsync().ConfigureAwait(false))
  193. {
  194. acc = _accumulator(acc, e.Current);
  195. }
  196. }
  197. finally
  198. {
  199. await e.DisposeAsync().ConfigureAwait(false);
  200. }
  201. return _resultSelector(acc);
  202. }
  203. }
  204. public static Task<TResult> AggregateAsync<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> accumulator, Func<TAccumulate, ValueTask<TResult>> resultSelector, CancellationToken cancellationToken = default)
  205. {
  206. if (source == null)
  207. throw Error.ArgumentNull(nameof(source));
  208. if (accumulator == null)
  209. throw Error.ArgumentNull(nameof(accumulator));
  210. if (resultSelector == null)
  211. throw Error.ArgumentNull(nameof(resultSelector));
  212. return Core(source, seed, accumulator, resultSelector, cancellationToken);
  213. async Task<TResult> Core(IAsyncEnumerable<TSource> _source, TAccumulate _seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> _accumulator, Func<TAccumulate, ValueTask<TResult>> _resultSelector, CancellationToken _cancellationToken)
  214. {
  215. var acc = _seed;
  216. var e = _source.GetAsyncEnumerator(_cancellationToken);
  217. try
  218. {
  219. while (await e.MoveNextAsync().ConfigureAwait(false))
  220. {
  221. acc = await _accumulator(acc, e.Current).ConfigureAwait(false);
  222. }
  223. }
  224. finally
  225. {
  226. await e.DisposeAsync().ConfigureAwait(false);
  227. }
  228. return await _resultSelector(acc).ConfigureAwait(false);
  229. }
  230. }
  231. #if !NO_DEEP_CANCELLATION
  232. public static Task<TResult> AggregateAsync<TSource, TAccumulate, TResult>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> accumulator, Func<TAccumulate, CancellationToken, ValueTask<TResult>> resultSelector, CancellationToken cancellationToken = default)
  233. {
  234. if (source == null)
  235. throw Error.ArgumentNull(nameof(source));
  236. if (accumulator == null)
  237. throw Error.ArgumentNull(nameof(accumulator));
  238. if (resultSelector == null)
  239. throw Error.ArgumentNull(nameof(resultSelector));
  240. return Core(source, seed, accumulator, resultSelector, cancellationToken);
  241. async Task<TResult> Core(IAsyncEnumerable<TSource> _source, TAccumulate _seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> _accumulator, Func<TAccumulate, CancellationToken, ValueTask<TResult>> _resultSelector, CancellationToken _cancellationToken)
  242. {
  243. var acc = _seed;
  244. var e = _source.GetAsyncEnumerator(_cancellationToken);
  245. try
  246. {
  247. while (await e.MoveNextAsync().ConfigureAwait(false))
  248. {
  249. acc = await _accumulator(acc, e.Current, _cancellationToken).ConfigureAwait(false);
  250. }
  251. }
  252. finally
  253. {
  254. await e.DisposeAsync().ConfigureAwait(false);
  255. }
  256. return await _resultSelector(acc, _cancellationToken).ConfigureAwait(false);
  257. }
  258. }
  259. #endif
  260. }
  261. }