In order to divide an IEnumerable in C# by a predicate, I implemented the following (in a static class):public static IEnumerable<IEnumerable<T>> SplitBeforeIf<T>(this IEnumerable<T> input, Func<T, bool> pred){    //This exists to store the predicate satisfier, which I kept as    //the first T in the inner IEnumerable (except, maybe the first one).    T predSatis = default(T);    while(input.Any())    {        var temp = input.TakeWhile(v => !pred(v));        //I was told that the below is the best test for default-ness.        //(I assume that pred(default(T)) is always false.)        if(predSatis == null || predSatis.Equals(default(T)))            yield return temp;        else            yield return (new List<T>{predSatis}).Concat(temp);        input = input.SkipWhile(v => !pred(v));        predSatis = input.FirstOrDefault();        input = input.Skip(1);    }    if(predSatis != null && !predSatis.Equals(default(T)))        yield return new List<T>{predSatis};}This returns as follows:new List<int>{1,2,3,4,5}.SplitBeforeIf(v => v % 2 == 0) gives {{1}, {2,3}, {4,5}}new List<int>{6,1,2,3,4,5}.SplitBeforeIf(v => v % 2 == 0) gives {{}, {6, 1}, {2,3}, {4, 5}}new List<int>{1,2,3,4,5,6}.SplitBeforeIf(v => v % 2 == 0) gives {{1}, {2,3}, {4,5}, {6}}.I have 3 questions (besides any other comments on the code):I see that, despite my best efforts, this is \$O(n^2)\$. Why? Can this be improved for a generic IEnumerable?This does not support a stateful predicate or a stateful IEnumerable input. Can this be remedied?This looks awfully like Clojure's partition-by. How does Clojure avoid the first issue (it may also have the second, but functional programming dislikes state anyway)?Being a little pedantic about these things, I also would prefer solutions that don't accumulate values into another data structure (just to see if full laziness is possible).
Split IEnumerable by predicate
c#;linq;functional programming