I\'ve got an interresting problem: Given an IEnumerable
, is it possible to yield a sequence of IEnumerable
Here's a solution that I think satisfies your requirements, works with any type of data item, and is quite short and readable:
public static IEnumerable> Partition(this IEnumerable list)
{
var current = list.FirstOrDefault();
while (!Equals(current, default(T))) {
var cur = current;
Func equalsCurrent = item => item.Equals(cur);
yield return list.TakeWhile(equalsCurrent);
list = list.SkipWhile(equalsCurrent);
current = list.FirstOrDefault();
}
}
Notes:
TakeWhile
and SkipWhile
do it).SkipWhile
); it does iterate over the collection once more when you process the returned IEnumerables, but the partitioning itself iterates only once.while
condition to a test for null
.If I am somehow mistaken, I 'd be especially interested in comments pointing out the mistakes!
Very Important Aside:
This solution will not allow you to enumerate the produced enumerables in any order other than the one it provides them in. However, I think the original poster has been pretty clear in comments that this is not a problem.