mirror of
https://github.com/nsnail/ns-ext.git
synced 2025-08-03 09:37:59 +08:00
feat: ✨ 异步累加器函数 (#17)
This commit is contained in:
@ -5,6 +5,26 @@ namespace NSExt.Extensions;
|
||||
/// </summary>
|
||||
public static class EnumerableExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 异步累加器函数
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidOperationException">InvalidOperationException</exception>
|
||||
public static async Task<TSource> AggregateAsync<TSource>( //
|
||||
this IEnumerable<TSource> source, Func<TSource, TSource, Task<TSource>> func)
|
||||
{
|
||||
using var e = source.GetEnumerator();
|
||||
if (!e.MoveNext()) {
|
||||
throw new InvalidOperationException("Sequence contains no elements");
|
||||
}
|
||||
|
||||
var result = e.Current;
|
||||
while (e.MoveNext()) {
|
||||
result = await func(result, e.Current).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将列表转成分隔符分隔的字符串
|
||||
/// </summary>
|
||||
|
Reference in New Issue
Block a user