using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using UnityEngine; public static class IEnumerableExtension { #region Get random element from list public static IEnumerable GetRandom(this IEnumerable list, int count) { var random = new System.Random(); var indexList = new List(); for (int i = 0; i < list.ToList().Count; i++) { indexList.Add(i); } for (int i = 0; i < count; i++) { int index = random.Next(0, indexList.Count); int value = indexList[index]; indexList.RemoveAt(index); yield return list.ToList()[value]; } } #endregion #region Map public static List Map(this List list, Func getElement) { return list.Select(x => getElement(x)).ToList(); } public static U[] Map(this T[] array, Func getElement) { return array.Select(x => getElement(x)).ToArray(); } #endregion #region Filter public static List Filter(this List list, Func getElement) { return list.Where(x => getElement(x)).ToList(); } public static T[] Filter(this T[] array, Func getElement) { return array.Where(x => getElement(x)).ToArray(); } #endregion #region Some public static bool Some(this IEnumerable list, Func getElement) { return list.Any(x => getElement(x)); } #endregion #region Flat public static List Flat(this List> list) { return list.SelectMany(x => x).ToList(); } public static T[] Flat(this T[][] array) { return array.SelectMany(x => x).ToArray(); } #endregion #region Reduce public static T Reduce(this IEnumerable list, Func getResult) { return list.Aggregate(getResult); } #endregion #region Clone public static List Clone(this List list) { return list.Map(x => x).ToList(); } public static T[] CloneArray(this T[] array) { return array.Map(x => x).ToArray(); } #endregion #region Every public static bool Every(this IEnumerable list, Func getElement) { return list.All(x => getElement(x)); } #endregion }