Bug Vanquisher

30 September 2009

Infinite Circular Looping

Filed under: Tips — Tanveer Badar @ 10:19 PM

Want to turn a linear sequence into a circular one so you have go round and round over it as long as needed? Here is how (with the added bonus of resetting and stopping the iteration)

   IEnumerable<T> GetNextInterval<T>( IEnumerable<T> input )
        {
            bool flag = true;
            while( flag )
            {
                input.Reset( );
                foreach( T t in input )
                {
                    if( end )
                    {
                        flag = false;
                        end = false;
                        break;
                    }
                    if( reset )
                    {
                        reset = false;
                        break;
                    }
                    yield return t;
                }
            }
        }

Setting reset to true short circuits the loop and starts from beginning again. Setting end to true terminates it entirely.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: