wabbajack/Wabbajack.Common/IEnumerableExtensions.cs

94 lines
2.4 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
2021-10-21 03:18:15 +00:00
using System.Threading.Tasks;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Common;
public static class IEnumerableExtensions
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
public static void Do<T>(this IEnumerable<T> coll, Action<T> f)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
foreach (var i in coll) f(i);
}
2021-12-27 16:24:45 +00:00
#region Shuffle
2021-12-27 16:24:45 +00:00
/// https://stackoverflow.com/questions/5807128/an-extension-method-on-ienumerable-needed-for-shuffling
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng)
{
return source.ShuffleIterator(rng);
}
private static IEnumerable<T> ShuffleIterator<T>(
this IEnumerable<T> source, Random rng)
{
var buffer = source.ToList();
for (int i = 0; i < buffer.Count; i++)
{
int j = rng.Next(i, buffer.Count);
yield return buffer[j];
buffer[j] = buffer[i];
}
}
2021-12-27 16:24:45 +00:00
#endregion
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static IEnumerable<TOut> TryKeep<TIn, TOut>(this IEnumerable<TIn> coll, Func<TIn, (bool, TOut)> fn)
{
return coll.Select(fn).Where(p => p.Item1).Select(p => p.Item2);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> coll)
{
var rnd = new Random();
var data = coll.ToArray();
for (var x = 0; x < data.Length; x++)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
var a = rnd.Next(0, data.Length);
var b = rnd.Next(0, data.Length);
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
(data[b], data[a]) = (data[a], data[b]);
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
return data;
}
public static IEnumerable<IEnumerable<T>> Partition<T>(this IEnumerable<T> coll, int size)
{
var asList = coll.ToList();
IEnumerable<T> SkipEnumerable(IList<T> list, int offset, int size)
{
for (var i = offset; i < list.Count; i += size)
{
yield return list[i];
}
}
return Enumerable.Range(0, size).Select(offset => SkipEnumerable(asList, offset, size));
}
2021-10-23 16:51:17 +00:00
public static IEnumerable<T> OnEach<T>(this IEnumerable<T> coll, Action<T> fn)
{
foreach (var itm in coll)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
fn(itm);
yield return itm;
2021-09-27 12:42:46 +00:00
}
2021-10-23 16:51:17 +00:00
}
public static async IAsyncEnumerable<T> OnEach<T>(this IEnumerable<T> coll, Func<T, Task> fn)
{
foreach (var itm in coll)
2021-10-21 03:18:15 +00:00
{
2021-10-23 16:51:17 +00:00
await fn(itm);
yield return itm;
2021-10-21 03:18:15 +00:00
}
2021-09-27 12:42:46 +00:00
}
}