Click here to Skip to main content
11,721,289 members (86,629 online)
Click here to Skip to main content

LINQ: Implementing the TakeLastWhile Operator

, 19 Oct 2010 CPOL 8K 4
Rate this:
Please Sign up or sign in to vote.
The TakeLastWhile operator returns last contiguous elements from a sequence that satisfy the specified criteria and is implemented as the TakeLastWhile extension methods.
free hit counters LINQ With C# (Portuguese)

Following my last posts (>)(>), in this post I'll introduce the implementation of the TakeLastWhile operator.

The TakeLastWhile operator returns last contiguous elements from a sequence that satisfy the specified criteria and is implemented as the TakeLastWhile extension methods:

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

What this method does, is even simpler. We start with an empty buffer and every item that satisfies the criteria implemented by a predicate. Whenever an item doesn't satisfy the criteria, the buffer is cleared:

var buffer = new List<TSource>();

foreach (var item in source)
{
    if (predicate(item))
    {
        buffer.Add(item);
    }
    else
    {
        buffer.Clear();
    }
}

After traversing the source sequence, we just yield all the items, if any, in the buffer:

foreach (var item in buffer)
{
    yield return item;
}

The overload that takes in account the index of the item only differs in the call the predicate that implements the criteria:

var buffer = >new >List<TSource>();v
var idx = 0;

foreach (>var item >in source)
{
    if (predicate(item, idx++))
    {
        buffer.Add(item);
    }
    >else
    {
        buffer.Clear();
    }
}

foreach (var item in buffer)
{
    yield return item;
}

You can find the complete implementation of this operator (and more) in the CodePlex project for LINQ utilities and operators: PauloMorgado.Linq.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Paulo Morgado
Software Developer (Senior) Paulo Morgado
Portugal Portugal

You may also be interested in...

Comments and Discussions

 
GeneralBetter performance Pin
Partenon18-Nov-10 3:51
memberPartenon18-Nov-10 3:51 
GeneralRe: Better performance Pin
Paulo Morgado21-Nov-10 14:18
memberPaulo Morgado21-Nov-10 14:18 
GeneralRe: Better performance [modified] Pin
Partenon1-Dec-10 4:13
memberPartenon1-Dec-10 4:13 
GeneralRe: Better performance Pin
Paulo Morgado1-Dec-10 13:43
memberPaulo Morgado1-Dec-10 13:43 
Generalthanks. Pin
lxxsumi822-Oct-10 17:09
memberlxxsumi822-Oct-10 17:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150901.1 | Last Updated 19 Oct 2010
Article Copyright 2010 by Paulo Morgado
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid