diff --git a/ConfigurationTests/Program.cs b/ConfigurationTests/Program.cs index 199adc4..1719ba2 100644 --- a/ConfigurationTests/Program.cs +++ b/ConfigurationTests/Program.cs @@ -1,13 +1,29 @@ using System; +using System.Collections.Generic; +using System.Linq; using ZeroLevel; namespace ConfigurationTests { class Program { - + static void Main(string[] args) { + var list = new List(); + for (int i = 0; i < 100; i++) + { + list.Add(i); + } + var collection = list.Chunkify(6).ToList(); + foreach (var t in collection) + { + Console.WriteLine(string.Join("; ", t.Select(n => n.ToString("D2")))); + } + Console.ReadKey(); + return; + + var config = Configuration.ReadFromIniFile("config.ini").Bind(); Console.WriteLine(config.Url); Console.WriteLine(config.BatchSize); diff --git a/ZeroLevel/Services/Extensions/EnumerableExtensions.cs b/ZeroLevel/Services/Extensions/EnumerableExtensions.cs index bfb02b7..2da284e 100644 --- a/ZeroLevel/Services/Extensions/EnumerableExtensions.cs +++ b/ZeroLevel/Services/Extensions/EnumerableExtensions.cs @@ -32,23 +32,20 @@ namespace ZeroLevel return !IsEmpty(collection); } - public static IEnumerable Batch(this IEnumerator source, int size) + public static IEnumerable> Chunkify(this IEnumerable source, int size) { - yield return source.Current; - for (var i = 1; i < size && source.MoveNext(); i++) + if (source == null) { - yield return source.Current; + yield break; } - } - - public static IEnumerable> Chunkify(this IEnumerable source, int size) - { - using (var enumerator = source.GetEnumerator()) + if (size <= 0) + { + throw new ArgumentException("chunkSize must be greater than 0."); + } + while (source.Any()) { - while (enumerator.MoveNext()) - { - yield return Batch(enumerator, size); - } + yield return source.Take(size); + source = source.Skip(size); } } }