Ver Fonte

Added generic Range enumerable

Kenric Nugteren há 1 semana atrás
pai
commit
aeb2b7e836
1 ficheiros alterados com 26 adições e 0 exclusões
  1. 26 0
      InABox.Core/CoreUtils.cs

+ 26 - 0
InABox.Core/CoreUtils.cs

@@ -2762,6 +2762,8 @@ namespace InABox.Core
             return newArr;
             return newArr;
         }
         }
 
 
+        #region Generators
+
         public static IEnumerable<T> AnyOr<T>(this IEnumerable<T> enumerable, IEnumerable<T> or)
         public static IEnumerable<T> AnyOr<T>(this IEnumerable<T> enumerable, IEnumerable<T> or)
         {
         {
             var any = false;
             var any = false;
@@ -2788,6 +2790,30 @@ namespace InABox.Core
             yield return fnc();
             yield return fnc();
         }
         }
 
 
+        /// <summary>
+        /// Range from <paramref name="from"/> to <paramref name="to"/>.
+        /// </summary>
+        public static IEnumerable<T> Range<T>(T from, T to, Func<T, T> step, bool inclusive = false)
+            where T : IComparable<T>
+        {
+            if (inclusive)
+            {
+                for(; from.CompareTo(to) <= 0; from = step(from))
+                {
+                    yield return from;
+                }
+            }
+            else
+            {
+                for(; from.CompareTo(to) < 0; from = step(from))
+                {
+                    yield return from;
+                }
+            }
+        }
+
+        #endregion
+
         public static (List<T>, List<T>) PartitionToList<T>(this IEnumerable<T> enumerable, Func<T, bool> predicate)
         public static (List<T>, List<T>) PartitionToList<T>(this IEnumerable<T> enumerable, Func<T, bool> predicate)
         {
         {
             var trueResult = new List<T>();
             var trueResult = new List<T>();