Fix chunkify

pull/1/head
Ogoun 5 years ago
parent 6a2a09514b
commit 96bc650326

@ -1,4 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using ZeroLevel; using ZeroLevel;
namespace ConfigurationTests namespace ConfigurationTests
@ -8,6 +10,20 @@ namespace ConfigurationTests
static void Main(string[] args) static void Main(string[] args)
{ {
var list = new List<int>();
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<AppConfig>(); var config = Configuration.ReadFromIniFile("config.ini").Bind<AppConfig>();
Console.WriteLine(config.Url); Console.WriteLine(config.Url);
Console.WriteLine(config.BatchSize); Console.WriteLine(config.BatchSize);

@ -32,23 +32,20 @@ namespace ZeroLevel
return !IsEmpty(collection); return !IsEmpty(collection);
} }
public static IEnumerable<T> Batch<T>(this IEnumerator<T> source, int size) public static IEnumerable<IEnumerable<T>> Chunkify<T>(this IEnumerable<T> source, int size)
{ {
yield return source.Current; if (source == null)
for (var i = 1; i < size && source.MoveNext(); i++)
{ {
yield return source.Current; yield break;
} }
} if (size <= 0)
{
public static IEnumerable<IEnumerable<T>> Chunkify<T>(this IEnumerable<T> source, int size) throw new ArgumentException("chunkSize must be greater than 0.");
{ }
using (var enumerator = source.GetEnumerator()) while (source.Any())
{ {
while (enumerator.MoveNext()) yield return source.Take(size);
{ source = source.Skip(size);
yield return Batch(enumerator, size);
}
} }
} }
} }

Loading…
Cancel
Save

Powered by TurnKey Linux.