Тогда
[*][1] -> [(1)]
[0] -> []
[*][1, 0] -> [(1)]
[*] [0, 1] -> [(1)]
[*][0, 0] -> []
[*][1, 1, 0] -> [(1, 1)]
[*][1, 0, 1] -> [(1), (1)]
[*][ 1, 1, 0, 0, 1, 0, 1, 1, 1] -> [(1, 1), (1), (1, 1, 1)]
По сути, это возврат смежных подсегментов, в которых предикат является ложным.
Я думал, это сработает
Код: Выделить всё
internal static IEnumerable PartitionBy(this IEnumerable source, Func condition)
{
IEnumerator mover = source.GetEnumerator();
for (; mover.MoveNext() ; )
{
var chunk = mover.MoveUntil(condition);
if (chunk.Any())
{
yield return chunk;
}
}
}
private static IEnumerable MoveUntil(this IEnumerator mover, Func condition)
{
bool hitCondition = false;
do
{
if (condition(mover.Current))
{
hitCondition = true;
}
else
{
yield return mover.Current;
}
}
while (!hitCondition && mover.MoveNext());
}
Код: Выделить всё
var chunk = mover.MoveUntil(condition);
Подробнее здесь: https://stackoverflow.com/questions/566 ... e-complexi
Мобильная версия