Fix chunkify

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

@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ZeroLevel;
namespace ConfigurationTests
@ -8,6 +10,20 @@ namespace ConfigurationTests
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>();
Console.WriteLine(config.Url);
Console.WriteLine(config.BatchSize);

@ -32,23 +32,20 @@ namespace ZeroLevel
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;
for (var i = 1; i < size && source.MoveNext(); i++)
if (source == null)
{
yield return source.Current;
yield break;
}
}
public static IEnumerable<IEnumerable<T>> Chunkify<T>(this IEnumerable<T> 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);
}
}
}

Loading…
Cancel
Save

Powered by TurnKey Linux.