2020-05-25 14:31:56 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Common
|
|
|
|
|
{
|
|
|
|
|
public static class IAsyncEnumerableExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Same as .Select but expects a function that returns an async result
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="coll"></param>
|
|
|
|
|
/// <param name="mapFn"></param>
|
|
|
|
|
/// <typeparam name="TIn"></typeparam>
|
|
|
|
|
/// <typeparam name="TOut"></typeparam>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async IAsyncEnumerable<TOut> SelectAsync<TIn, TOut>(this IEnumerable<TIn> coll,
|
|
|
|
|
Func<TIn, ValueTask<TOut>> mapFn)
|
|
|
|
|
{
|
|
|
|
|
foreach (var itm in coll)
|
|
|
|
|
{
|
|
|
|
|
yield return await mapFn(itm);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-25 16:30:47 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Same as .Select but expects a function that returns an async result
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="coll"></param>
|
|
|
|
|
/// <param name="mapFn"></param>
|
|
|
|
|
/// <typeparam name="TIn"></typeparam>
|
|
|
|
|
/// <typeparam name="TOut"></typeparam>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static async ValueTask DoAsync<TIn, TOut>(this IEnumerable<TIn> coll,
|
|
|
|
|
Func<TIn, ValueTask<TOut>> mapFn)
|
|
|
|
|
{
|
|
|
|
|
foreach (var itm in coll)
|
|
|
|
|
{
|
|
|
|
|
await mapFn(itm);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-25 14:31:56 +00:00
|
|
|
|
|
|
|
|
|
public static async ValueTask<List<T>> ToList<T>(this IAsyncEnumerable<T> coll)
|
|
|
|
|
{
|
|
|
|
|
var list =new List<T>();
|
|
|
|
|
await foreach (var itm in coll)
|
|
|
|
|
list.Add(itm);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|