morelinq / MoreLINQ

Extensions to LINQ to Objects
https://morelinq.github.io/
Apache License 2.0
3.63k stars 409 forks source link

Remove tail items while condition is true #1036

Open atifaziz opened 7 months ago

atifaziz commented 7 months ago

Consider adding an operator that remove items from the tail end of a sequence while a given predicate returns true. The proposed name and signature would be:

public static IEnumerable<T> SkipLastWhile<T>(this IEnumerable<T> source, Func<T, bool> predicate)

Example:

var xs = new[]
{
    1, 2,
    0, 0, // will be preserved (not in tail position)
    3, 4,
    0, 0, // will be removed
};

var result = xs.SkipLastWhile(x => x is 0);

Console.WriteLine(result.ToDelimitedString(", "));

Output:

1, 2, 0, 0, 3, 4