using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace ZeroLevel { public static class EnumerableExtensions { public static IEnumerable Safe(this IEnumerable collection) { return collection ?? Enumerable.Empty(); } public static bool Contains(this IEnumerable collection, Predicate condition) { return collection.Any(x => condition(x)); } public static bool IsEmpty(this IEnumerable collection) { if (collection == null) return true; var coll = collection as ICollection; if (coll != null) return coll.Count == 0; return !collection.Any(); } public static bool IsNotEmpty(this IEnumerable collection) { return !IsEmpty(collection); } public static IEnumerable Batch(this IEnumerator source, int size) { yield return source.Current; for (var i = 1; i < size && source.MoveNext(); i++) { yield return source.Current; } } public static IEnumerable> Chunkify(this IEnumerable source, int size) { using (var enumerator = source.GetEnumerator()) { while (enumerator.MoveNext()) { yield return Batch(enumerator, size); } } } } }