Skip to main content
Code Review

Return to Answer

changed the second version to a more "tight" layout
Source Link
user73941
user73941

I can't help you with your questions, but below you'll find another implementation which avoids instatiation of new lists. The penalty is though the lst.Count(), but if the frequence of elements satisfying the predicate is high it may be ok?

public static IEnumerable<IEnumerable<T>> SplitBeforeIf2<T>(this IEnumerable<T> input, Func<T, bool> pred)
{
 var lst = input.TakeWhile((x, i) => i == 0 ? true : !pred(x));
 var count = lst.Count();
 if (count > 0)
 {
 yield return lst;
 foreach (var l in input.Skip(count).SplitBeforeIf2(pred))
 yield return l;
 }
}

As the above solution has a rather poor performance for large datasets, I went on and found the solution below which seems to be more effecient:

public static IEnumerable<IEnumerable<T>> SplitBeforeIf2<T>SplitBeforeIf4<T>(this IEnumerable<T> input, Func<T, bool> pred)
{
 int count = 0;
 intIGrouping<bool, tmpCountT> group = 0;
null;
 while (tmpCount >= 0)
 {
 tmpCount = -1;
 var (group = input.Skip(count).TakeWhile((x, i) => i == 0 ? true : !pred(x)).GroupBy(x => true).FirstOrDefault();
 if (group) != null)
 {
 yield return group.Select(x => x);
 tmpCountcount =+= group.Count();
 count += tmpCount;
 }
 }
}

I can't help you with your questions, but below you'll find another implementation which avoids instatiation of new lists. The penalty is though the lst.Count(), but if the frequence of elements satisfying the predicate is high it may be ok?

public static IEnumerable<IEnumerable<T>> SplitBeforeIf2<T>(this IEnumerable<T> input, Func<T, bool> pred)
{
 var lst = input.TakeWhile((x, i) => i == 0 ? true : !pred(x));
 var count = lst.Count();
 if (count > 0)
 {
 yield return lst;
 foreach (var l in input.Skip(count).SplitBeforeIf2(pred))
 yield return l;
 }
}

As the above solution has a rather poor performance for large datasets, I went on and found the solution below which seems to be more effecient:

public static IEnumerable<IEnumerable<T>> SplitBeforeIf2<T>(this IEnumerable<T> input, Func<T, bool> pred)
{
 int count = 0;
 int tmpCount = 0;

 while (tmpCount >= 0)
 {
 tmpCount = -1;
 var group = input.Skip(count).TakeWhile((x, i) => i == 0 ? true : !pred(x)).GroupBy(x => true).FirstOrDefault();
 if (group != null)
 {
 yield return group.Select(x => x);
 tmpCount = group.Count();
 count += tmpCount;
 }
 }
}

I can't help you with your questions, but below you'll find another implementation which avoids instatiation of new lists. The penalty is though the lst.Count(), but if the frequence of elements satisfying the predicate is high it may be ok?

public static IEnumerable<IEnumerable<T>> SplitBeforeIf2<T>(this IEnumerable<T> input, Func<T, bool> pred)
{
 var lst = input.TakeWhile((x, i) => i == 0 ? true : !pred(x));
 var count = lst.Count();
 if (count > 0)
 {
 yield return lst;
 foreach (var l in input.Skip(count).SplitBeforeIf2(pred))
 yield return l;
 }
}

As the above solution has a rather poor performance for large datasets, I went on and found the solution below which seems to be more effecient:

public static IEnumerable<IEnumerable<T>> SplitBeforeIf4<T>(this IEnumerable<T> input, Func<T, bool> pred)
{
 int count = 0;
 IGrouping<bool, T> group = null;
 while ((group = input.Skip(count).TakeWhile((x, i) => i == 0 ? true : !pred(x)).GroupBy(x => true).FirstOrDefault()) != null)
 {
 yield return group.Select(x => x);
 count += group.Count();
 }
}
deleted 37 characters in body
Source Link
user73941
user73941

I can't help you with your questions, but below you'll find another implementation which avoids instatiation of new lists. The penalty is though the lst.Count(), but if the frequence of elements satisfying the predicate is high it may be ok?

public static IEnumerable<IEnumerable<T>> SplitBeforeIf2<T>(this IEnumerable<T> input, Func<T, bool> pred)
{
 var lst = input.Take(1).Concat(input.Skip(1).TakeWhile((x, i) => i == 0 ? true : !pred(x)));
 var count = lst.Count();
 if (count > 0)
 {
 yield return lst;
 foreach (var l in input.Skip(count).SplitBeforeIf2(pred))
 yield return l;
 }
}

As the above solution has a rather poor performance for large datasets, I went on and found the solution below which seems to be more effecient:

public static IEnumerable<IEnumerable<T>> SplitBeforeIf2<T>(this IEnumerable<T> input, Func<T, bool> pred)
{
 int count = 0;
 int tmpCount = 0;
 while (tmpCount >= 0)
 {
 tmpCount = -1;
 foreach (var group in= input.Skip(count).TakeWhile((x, i) => i == 0? true : !pred(x)).GroupBy(x => true).FirstOrDefault();
 if (group != null)
 {
 yield return group.Select(x => x);
 tmpCount = group.Count();
 count += tmpCount;
 if (tmpCount == 0)
 tmpCount = -1;
 }
 }
}

I can't help you with your questions, but below you'll find another implementation which avoids instatiation of new lists. The penalty is though the lst.Count(), but if the frequence of elements satisfying the predicate is high it may be ok?

public static IEnumerable<IEnumerable<T>> SplitBeforeIf2<T>(this IEnumerable<T> input, Func<T, bool> pred)
{
 var lst = input.Take(1).Concat(input.Skip(1).TakeWhile(x => !pred(x)));
 var count = lst.Count();
 if (count > 0)
 {
 yield return lst;
 foreach (var l in input.Skip(count).SplitBeforeIf2(pred))
 yield return l;
 }
}

As the above solution has a rather poor performance for large datasets, I went on and found the solution below which seems to be more effecient:

public static IEnumerable<IEnumerable<T>> SplitBeforeIf2<T>(this IEnumerable<T> input, Func<T, bool> pred)
{
 int count = 0;
 int tmpCount = 0;
 while (tmpCount >= 0)
 {
 tmpCount = -1;
 foreach (var group in input.Skip(count).TakeWhile((x, i) => i == 0? true : !pred(x)).GroupBy(x => true))
 {
 yield return group.Select(x => x);
 tmpCount = group.Count();
 count += tmpCount;
 if (tmpCount == 0)
 tmpCount = -1;
 }
 }
}

I can't help you with your questions, but below you'll find another implementation which avoids instatiation of new lists. The penalty is though the lst.Count(), but if the frequence of elements satisfying the predicate is high it may be ok?

public static IEnumerable<IEnumerable<T>> SplitBeforeIf2<T>(this IEnumerable<T> input, Func<T, bool> pred)
{
 var lst = input.TakeWhile((x, i) => i == 0 ? true : !pred(x));
 var count = lst.Count();
 if (count > 0)
 {
 yield return lst;
 foreach (var l in input.Skip(count).SplitBeforeIf2(pred))
 yield return l;
 }
}

As the above solution has a rather poor performance for large datasets, I went on and found the solution below which seems to be more effecient:

public static IEnumerable<IEnumerable<T>> SplitBeforeIf2<T>(this IEnumerable<T> input, Func<T, bool> pred)
{
 int count = 0;
 int tmpCount = 0;
 while (tmpCount >= 0)
 {
 tmpCount = -1;
 var group = input.Skip(count).TakeWhile((x, i) => i == 0? true : !pred(x)).GroupBy(x => true).FirstOrDefault();
 if (group != null)
 {
 yield return group.Select(x => x);
 tmpCount = group.Count();
 count += tmpCount;
 }
 }
}
added 712 characters in body
Source Link
user73941
user73941

I can't help you with your questions, but below you'll find another implementation which avoids instatiation of new lists. The penalty is though the lst.Count(), but if the frequence of elements satisfying the predicate is high it may be ok?

public static IEnumerable<IEnumerable<T>> SplitBeforeIf2<T>(this IEnumerable<T> input, Func<T, bool> pred)
{
 var lst = input.Take(1).Concat(input.Skip(1).TakeWhile(x => !pred(x)));
 var count = lst.Count();
 
 if (count > 0)
 {
 yield return lst;
 foreach (var l in input.Skip(count).SplitBeforeIf2(pred))
 yield return l;
 }
}

As the above solution has a rather poor performance for large datasets, I went on and found the solution below which seems to be more effecient:

public static IEnumerable<IEnumerable<T>> SplitBeforeIf2<T>(this IEnumerable<T> input, Func<T, bool> pred)
{
 int count = 0;
 int tmpCount = 0;
 while (tmpCount >= 0)
 {
 tmpCount = -1;
 foreach (var group in input.Skip(count).TakeWhile((x, i) => i == 0? true : !pred(x)).GroupBy(x => true))
 {
 yield return group.Select(x => x);
 tmpCount = group.Count();
 count += tmpCount;
 if (tmpCount == 0)
 tmpCount = -1;
 }
 }
}

I can't help you with your questions, but below you'll find another implementation which avoids instatiation of new lists. The penalty is though the lst.Count(), but if the frequence of elements satisfying the predicate is high it may be ok?

public static IEnumerable<IEnumerable<T>> SplitBeforeIf2<T>(this IEnumerable<T> input, Func<T, bool> pred)
{
 var lst = input.Take(1).Concat(input.Skip(1).TakeWhile(x => !pred(x)));
 var count = lst.Count();
 
 if (count > 0)
 {
 yield return lst;
 foreach (var l in input.Skip(count).SplitBeforeIf2(pred))
 yield return l;
 }
}

I can't help you with your questions, but below you'll find another implementation which avoids instatiation of new lists. The penalty is though the lst.Count(), but if the frequence of elements satisfying the predicate is high it may be ok?

public static IEnumerable<IEnumerable<T>> SplitBeforeIf2<T>(this IEnumerable<T> input, Func<T, bool> pred)
{
 var lst = input.Take(1).Concat(input.Skip(1).TakeWhile(x => !pred(x)));
 var count = lst.Count();
 
 if (count > 0)
 {
 yield return lst;
 foreach (var l in input.Skip(count).SplitBeforeIf2(pred))
 yield return l;
 }
}

As the above solution has a rather poor performance for large datasets, I went on and found the solution below which seems to be more effecient:

public static IEnumerable<IEnumerable<T>> SplitBeforeIf2<T>(this IEnumerable<T> input, Func<T, bool> pred)
{
 int count = 0;
 int tmpCount = 0;
 while (tmpCount >= 0)
 {
 tmpCount = -1;
 foreach (var group in input.Skip(count).TakeWhile((x, i) => i == 0? true : !pred(x)).GroupBy(x => true))
 {
 yield return group.Select(x => x);
 tmpCount = group.Count();
 count += tmpCount;
 if (tmpCount == 0)
 tmpCount = -1;
 }
 }
}
added 46 characters in body
Source Link
user73941
user73941
Loading
Post Undeleted by Community Bot
added 46 characters in body
Source Link
user73941
user73941
Loading
Post Deleted by Community Bot
Source Link
user73941
user73941
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /