Is there any way I can separate a List
into several separate lists of SomeObject
, using the item index as the delimiter of each s
completely lazy, no counting or copying:
public static class EnumerableExtensions
{
public static IEnumerable> Split(this IEnumerable source, int len)
{
if (len == 0)
throw new ArgumentNullException();
var enumer = source.GetEnumerator();
while (enumer.MoveNext())
{
yield return Take(enumer.Current, enumer, len);
}
}
private static IEnumerable Take(T head, IEnumerator tail, int len)
{
while (true)
{
yield return head;
if (--len == 0)
break;
if (tail.MoveNext())
head = tail.Current;
else
break;
}
}
}