Pair-wise iteration in C# or sliding window enumerator?

Rather than require a tuple (pair) type, why not just accept a selector: public static IEnumerable Pairwise(this IEnumerable source, Func resultSelector) { TSource previous = default(TSource); using (var it = source.GetEnumerator()) { if (it.MoveNext()) previous = it. Current; while (it.MoveNext()) yield return resultSelector(previous, previous = it. Current); } } Which allows you to skip the intermediate object if you want: string items = new string { "a", "b", "c", "d" }; var pairs = items.

Pairwise((x, y) => string. Format("{0},{1}", x, y)); foreach(var pair in pairs) Console. WriteLine(pair) Or you can use an anonymous type: var pairs = items.

Pairwise((x, y) => new { First = x, Second = y }).

Rather than require a tuple (pair) type, why not just accept a selector: public static IEnumerable Pairwise(this IEnumerable source, Func resultSelector) { TSource previous = default(TSource); using (var it = source.GetEnumerator()) { if (it.MoveNext()) previous = it. Current; while (it.MoveNext()) yield return resultSelector(previous, previous = it. Current); } } Which allows you to skip the intermediate object if you want: string items = new string { "a", "b", "c", "d" }; var pairs = items.

Pairwise((x, y) => string. Format("{0},{1}", x, y)); foreach(var pair in pairs) Console. WriteLine(pair); Or you can use an anonymous type: var pairs = items.

Pairwise((x, y) => new { First = x, Second = y }).

In . NET 4 this becomes even easier:- var input = new { "a", "b", "c", "d", "e", "f" }; var result = input. Zip(input.

Skip(1), (a, b) => Tuple. Create(a, b)).

Expanding on the previous answer to avoid of O(n2) approach by explicitly using the passed iterator: public static IEnumerable> Tuples(this IEnumerable input, int groupCount) { if (null == input) throw new ArgumentException("input"); if (groupCount Current); } yield return l; } } For C# 2, before extension methods, drop the "this" from the input parameter and call as a static method.

C# 3.0 solution (sorry:) public static IEnumerable> Tuples(this IEnumerable sequence, int nTuple) { if(nTuple Really, the only thing making this a C# 3.0 solution is the .Skip. Take construct, so if you just change that to adding the elements in that range to a list instead, it should be golden for 2.0. That said, it's still not performant.

This is an O(n*n) implementation! For a small 10 item list, the entire list was traversed 20 times – configurator Feb 23 '09 at 14:58 That's true, but it's also two lines of (real) code and is an obviously simple implementation.It's up to the OP to decide whether he needs a fast solution -- maybe he only needs this operation on lists of a couple dozen items. – mquander Feb 23 '09 at 15:04.

Alternate Pairs implementation, using last pair to store previous value: static IEnumerable> Pairs( IEnumerable collection ) { Pair pair = null; foreach( T item in collection ) { if( pair == null ) pair = Pair. Create( default( T ), item ); else yield return pair = Pair. Create( pair.

Second, item ); } } Simple Window implementation (only safe for private use, if caller does not save returned arrays; see note): static IEnumerable Window( IEnumerable collection, int windowSize ) { if( windowSize = windowSize; if( initialized ) //NOTE: For public API, should return array copy to prevent // modifcation by user, or use a different type for the window. Yield return window; } } Example use: for( int I = 0; I . Window( items, I ) ) Console.

WriteLine( string. Join( ", ", window ) ); Console. WriteLine( ); }.

The F# Seq module defines the pairwise function over IEnumerable, but this function is not in the . NET framework. If it were already in the .

NET framework, instead of returning pairs it would probably accept a selector function due to the lack of support for tuples in languages like C# and VB. Var pairs = ns. Pairwise( (a, b) => new { First = a, Second = be }; I don't think any of the answers here really improve on your simple iterator implementation, which seemed the most natural to me (and the poster dahlbyk by the looks of things!) too.

Something like this: public static IEnumerable Pairwise(this IEnumerable enumerable, Func selector) { var previous = enumerable.First(); foreach (var item in enumerable. Skip(1)) { yield return selector(previous, item); previous = item; } }.

Pair = Pair. Yield return pair = Pair. // Shift initialized window to accomodate new item.

// Add current item to window. Int itemIndex = initializing? // modifcation by user, or use a different type for the window.

For( int I = 0; I .

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions