Sfoglia il codice sorgente

Added generic Range enumerable

Kenric Nugteren 1 settimana fa
parent
commit
aeb2b7e836
1 ha cambiato i file con 26 aggiunte e 0 eliminazioni
  1. 26 0
      InABox.Core/CoreUtils.cs

+ 26 - 0
InABox.Core/CoreUtils.cs

@@ -2762,6 +2762,8 @@ namespace InABox.Core
             return newArr;
         }
 
+        #region Generators
+
         public static IEnumerable<T> AnyOr<T>(this IEnumerable<T> enumerable, IEnumerable<T> or)
         {
             var any = false;
@@ -2788,6 +2790,30 @@ namespace InABox.Core
             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)
         {
             var trueResult = new List<T>();