using System; using System.Collections.Generic; using System.Linq; using EE = System.Collections.Generic.IEnumerable; namespace Gallium { public static class EnumerableEntityExtensions1 { public static EE ForEachEntity( this EE entities, Func lambda) { return entities .Select(x => x.Has() ? lambda(x, x.Get()) : x); } /// /// Runs an inner set of operation for entities that have a specific /// component. /// /// The input to scan. /// /// The list operation for the ones that have those /// components. /// /// The type of the first component. /// /// A combined list of components, with entities without the component /// coming first and the ones with the component coming last after being /// processed by the lambda. /// public static EE ForEntities( this EE entities, Func lambda) { List list = entities.ToList(); EE has = lambda(list.HasComponents()); EE hasNot = list.NotComponents(); return hasNot.Union(has); } public static EE HasComponents(this EE entities) { return entities .Where(x => x.Has()); } public static EE NotComponents(this EE entities) { return entities .Where(x => !x.Has()); } public static EE WhereEntities( this EE entities, Func lambda, bool includeWithoutComponent = true) { return entities .Where( x => x.Has() ? lambda(x, x.Get()) : includeWithoutComponent); } } }