Partitioning a sequence into sublists This function divides a sequence into partitions, where a partition is a list of consecutive matching elements.

Example
Input: (A, A, B, B, B, A, C, C)
Output: ((A, A), (B, B, B), (A), (C, C))

I've tried to make this code &quot;obviously correct&quot;, but it still doesn't look that way to me.
public static IEnumerable&lt;List&lt;T&gt;&gt; PartitionBy&lt;T, PK&gt;(this IEnumerable&lt;T&gt; sequence, Func&lt;T, PK&gt; partitionKey)
{
    return sequence.PartitionBy(partitionKey, EqualityComparer&lt;PK&gt;.Default);
}

public static IEnumerable&lt;List&lt;T&gt;&gt; PartitionBy&lt;T&gt;(this IEnumerable&lt;T&gt; sequence, IEqualityComparer&lt;T&gt; comparer)
{
    return sequence.PartitionBy(item =&gt; item, comparer);
}

public static IEnumerable&lt;List&lt;T&gt;&gt; PartitionBy&lt;T, X&gt;(this IEnumerable&lt;T&gt; sequence, Func&lt;T, X&gt; partitionKey, IEqualityComparer&lt;X&gt; comparer)
{
    var itr = sequence.GetEnumerator();
    if (!itr.MoveNext())
    {
        // empty sequence was passed in, so return empty sequence
        yield break;
    }

    // Start the first partition.
    var currentList = new List&lt;T&gt;(new[] { itr.Current });

    while (itr.MoveNext())
    {
        var key1 = partitionKey(currentList[0]);
        var key2 = partitionKey(itr.Current);

        if (comparer.Equals(key1, key2))
        {
            // continue current partition
            currentList.Add(itr.Current);
        }
        else
        {
            // yield current partition and start a new one
            yield return currentList;
            currentList = new List&lt;T&gt;(new[] { itr.Current });
        }
    }

    // We know it has at least 1 element here.
    yield return currentList;
}
Just a small point to start:

new List&lt;T&gt;(new[] { itr.Current });


You don't need the array too, you can just do:

new List&lt;T&gt; { itr.Current };


I'd suggest that you aim for consistency with your generic type names too. Why X vs PK? I'd suggest TKey for both.

You could do it just with a foreach:

public static IEnumerable&lt;IEnumerable&lt;T&gt;&gt; Partition&lt;T, TKey&gt;(
    this IEnumerable&lt;T&gt; items, 
    Func&lt;T, TKey&gt; keySelector, 
    IEqualityComparer&lt;TKey&gt; comparer)
{
    List&lt;T&gt; currentPartition = null;
    foreach (var item in items)
    {
        if (currentPartition != null 
            &amp;&amp; comparer.Equals(keySelector(item), keySelector(currentPartition[0])))
        {
            currentPartition.Add(item);
        }
        else 
        {
            if (currentPartition != null)
            {
                yield return currentPartition;
            }
            currentPartition = new List&lt;T&gt; { item };
        }
    }
    if (currentPartition != null)
    {
        yield return currentPartition;
    }
}


Is it clearer than your code? I'm not convinced it is.