2026-01-07 14:47:20 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2026-01-08 09:14:46 +00:00
|
|
|
using System.Linq;
|
2026-01-07 14:47:20 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SharpCompress;
|
|
|
|
|
|
2026-01-08 09:14:46 +00:00
|
|
|
public static class AsyncEnumerableEx
|
|
|
|
|
{
|
|
|
|
|
public static async IAsyncEnumerable<T> Empty<T>()
|
|
|
|
|
where T : notnull
|
|
|
|
|
{
|
2026-01-08 09:41:48 +00:00
|
|
|
await Task.CompletedTask;
|
|
|
|
|
yield break;
|
2026-01-08 09:14:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-08 09:39:04 +00:00
|
|
|
public static class EnumerableExtensions
|
|
|
|
|
{
|
|
|
|
|
public static async IAsyncEnumerable<T> ToAsyncEnumerable<T>(this IEnumerable<T> source)
|
|
|
|
|
{
|
|
|
|
|
await Task.CompletedTask;
|
|
|
|
|
foreach (var item in source)
|
|
|
|
|
{
|
|
|
|
|
yield return item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 14:47:20 +00:00
|
|
|
public static class AsyncEnumerableExtensions
|
|
|
|
|
{
|
2026-01-12 13:45:21 +00:00
|
|
|
public static async ValueTask<List<T>> ToListAsync<T>(this IAsyncEnumerable<T> source)
|
2026-01-07 14:47:20 +00:00
|
|
|
{
|
2026-01-12 13:45:21 +00:00
|
|
|
var list = new List<T>();
|
|
|
|
|
await foreach (var item in source)
|
2026-01-08 09:39:04 +00:00
|
|
|
{
|
2026-01-12 13:45:21 +00:00
|
|
|
list.Add(item);
|
2026-01-08 09:39:04 +00:00
|
|
|
}
|
2026-01-12 13:45:21 +00:00
|
|
|
return list;
|
|
|
|
|
}
|
2026-01-08 09:41:48 +00:00
|
|
|
|
2026-01-12 14:14:46 +00:00
|
|
|
public static async IAsyncEnumerable<TResult> CastAsync<TResult>(
|
|
|
|
|
this IAsyncEnumerable<object?> source
|
|
|
|
|
)
|
2026-01-12 13:45:21 +00:00
|
|
|
where TResult : class
|
|
|
|
|
{
|
|
|
|
|
await foreach (var item in source)
|
2026-01-08 09:14:46 +00:00
|
|
|
{
|
2026-01-12 13:45:21 +00:00
|
|
|
yield return (item as TResult).NotNull();
|
2026-01-08 09:14:46 +00:00
|
|
|
}
|
2026-01-12 13:45:21 +00:00
|
|
|
}
|
2026-01-08 09:41:48 +00:00
|
|
|
|
2026-01-12 13:45:21 +00:00
|
|
|
public static async ValueTask<TAccumulate> AggregateAsync<TAccumulate, T>(
|
|
|
|
|
this IAsyncEnumerable<T> source,
|
|
|
|
|
TAccumulate seed,
|
|
|
|
|
Func<TAccumulate, T, TAccumulate> func
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
TAccumulate result = seed;
|
|
|
|
|
await foreach (var element in source)
|
|
|
|
|
{
|
|
|
|
|
result = func(result, element);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-12 14:14:46 +00:00
|
|
|
public static async ValueTask<bool> AllAsync<T>(
|
|
|
|
|
this IAsyncEnumerable<T> source,
|
|
|
|
|
Func<T, bool> predicate
|
|
|
|
|
)
|
2026-01-12 13:45:21 +00:00
|
|
|
{
|
2026-01-12 14:14:46 +00:00
|
|
|
await foreach (var item in source)
|
2026-01-08 09:14:46 +00:00
|
|
|
{
|
2026-01-12 14:14:46 +00:00
|
|
|
if (!predicate(item))
|
2026-01-08 09:14:46 +00:00
|
|
|
{
|
2026-01-12 14:14:46 +00:00
|
|
|
return false;
|
2026-01-08 09:14:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-01-08 09:41:48 +00:00
|
|
|
|
2026-01-12 14:14:46 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IAsyncEnumerable<T> Where<T>(
|
|
|
|
|
this IAsyncEnumerable<T> source,
|
|
|
|
|
Func<T, bool> predicate
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
return WhereIterator(source, predicate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async IAsyncEnumerable<T> WhereIterator<T>(
|
|
|
|
|
IAsyncEnumerable<T> source,
|
|
|
|
|
Func<T, bool> predicate
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
await foreach (var item in source)
|
2026-01-07 14:47:20 +00:00
|
|
|
{
|
2026-01-12 14:14:46 +00:00
|
|
|
if (predicate(item))
|
2026-01-07 14:47:20 +00:00
|
|
|
{
|
2026-01-12 14:14:46 +00:00
|
|
|
yield return item;
|
2026-01-07 14:47:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-01-12 14:14:46 +00:00
|
|
|
}
|
2026-01-07 14:47:20 +00:00
|
|
|
|
2026-01-12 14:14:46 +00:00
|
|
|
public static async IAsyncEnumerable<T> WhereAsync<T>(
|
|
|
|
|
this IAsyncEnumerable<T> source,
|
|
|
|
|
Func<T, bool> predicate
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
await foreach (var item in source)
|
2026-01-12 12:08:25 +00:00
|
|
|
{
|
2026-01-12 14:14:46 +00:00
|
|
|
if (predicate(item))
|
2026-01-12 12:08:25 +00:00
|
|
|
{
|
2026-01-12 14:14:46 +00:00
|
|
|
yield return item;
|
2026-01-12 12:08:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-01-12 14:14:46 +00:00
|
|
|
}
|
2026-01-12 12:08:25 +00:00
|
|
|
|
2026-01-12 14:14:46 +00:00
|
|
|
public static async ValueTask<T> FirstAsync<T>(this IAsyncEnumerable<T> source)
|
|
|
|
|
{
|
|
|
|
|
await foreach (var item in source)
|
2026-01-07 14:47:20 +00:00
|
|
|
{
|
2026-01-12 14:14:46 +00:00
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
throw new InvalidOperationException("The source sequence is empty.");
|
|
|
|
|
}
|
2026-01-07 14:47:20 +00:00
|
|
|
|
2026-01-12 14:14:46 +00:00
|
|
|
public static async ValueTask<T?> FirstOrDefaultAsync<T>(this IAsyncEnumerable<T> source)
|
|
|
|
|
{
|
|
|
|
|
await foreach (var item in source)
|
|
|
|
|
{
|
|
|
|
|
return item;
|
2026-01-08 09:14:46 +00:00
|
|
|
}
|
2026-01-12 14:14:46 +00:00
|
|
|
|
|
|
|
|
return default;
|
2026-01-07 14:47:20 +00:00
|
|
|
}
|
|
|
|
|
}
|