Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting LINQ query based on predicate

Tags:

c#

linq

I'd like a method that would split an IEnumerable at a predicate, grouping items together by their index relative to the predicate. For example, it could split a List<string> at items satisfying x => MyRegex.Match(x).Success, with items "in between" such matches being grouped together.

Its signature could look something line

public static IEnumerable<IEnumerable<TSource>> Split<TSource>(
    this IEnumerable<TSource> source,
    Func<TSource, bool> predicate,
    int bool count
)

, possibly with an extra element of the output containing all of the dividers.

Is there a more efficient and/or compact way to implement this than a foreach loop? I feel like it should be possible to implement with LINQ methods, but I can't put my finger on it.

Example:

string[] arr = {"One", "Two", "Three", "Nine", "Four", "Seven", "Five"};
arr.Split(x => x.EndsWith("e"));

Either of the following would be OK:

IEnumerable<string> {{}, {"Two"}, {}, {"Four", "Seven"}, {}}
IEnumerable<string> {{"Two"}, {"Four", "Seven"}}

the optional element for storing matches would be {"One", "Three", "Nine", "Five"}.

like image 703
Arithmomaniac Avatar asked Jul 11 '12 17:07

Arithmomaniac


4 Answers

If you're looking to avoid the extension method, you could always use:

var arr = new[] {"One", "Two", "Three", "Nine", "Four", "Seven", "Five"};

var result = arr.ToLookup(x => x.EndsWith("e"));

// result[true]  == One Three Nine Five
// result[false] == Two Four Seven
like image 144
Andy Avatar answered Nov 09 '22 23:11

Andy


You should do this through an extension method (this method assumes you ignore the partitioned item):

/// <summary>Splits an enumeration based on a predicate.</summary>
/// <remarks>
/// This method drops partitioning elements.
/// </remarks>
public static IEnumerable<IEnumerable<TSource>> Split<TSource>(
    this IEnumerable<TSource> source,
    Func<TSource, bool> partitionBy,
    bool removeEmptyEntries = false,
    int count = -1)
{
    int yielded = 0;
    var items = new List<TSource>();
    foreach (var item in source)
    {
        if (!partitionBy(item))
            items.Add(item);
        else if (!removeEmptyEntries || items.Count > 0)
        {
            yield return items.ToArray();
            items.Clear();

            if (count > 0 && ++yielded == count) yield break;
        }
    }

    if (items.Count > 0) yield return items.ToArray();
}
like image 25
user7116 Avatar answered Nov 09 '22 23:11

user7116


public static IEnumerable<IEnumerable<TSource>> Split<TSource>(
    this IEnumerable<TSource> source,
    Func<TSource, bool> predicate)
{
    List<TSource> group = new List<TSource>();
    foreach (TSource item in source)
    {
        if (predicate(item))
        {
            yield return group.AsEnumerable();
            group = new List<TSource>();
        }
        else
        {
            group.Add(item);
        }
    }
    yield return group.AsEnumerable();
}
like image 3
Servy Avatar answered Nov 10 '22 00:11

Servy


public static IEnumerable<IEnumerable<TSource>> Partition<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
    yield return source.Where(predicate);
    yield return source.Where(x => !predicate(x));
}

Example:

var list = new List<int> { 1, 2, 3, 4, 5 };
var parts = list.Partition(x => x % 2 == 0);
var even = parts.ElementAt(0); // contains 2, 4
var odd = parts.ElementAt(1); // contains 1, 3, 5
like image 2
knr Avatar answered Nov 09 '22 23:11

knr