Skip to content

Instantly share code, notes, and snippets.

@gsscoder gsscoder/Pairwise.cs
Last active Dec 15, 2015

Embed
What would you like to do?
IEnumerable<T>::Pairwise extension method for C# modeled like an overload of F# Seq.pairwise<'T> (http://msdn.microsoft.com/it-it/library/ee370361.aspx).
public static IEnumerable<TResult> Pairwise<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TSource, TResult> selector)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
if (selector == null)
{
throw new ArgumentNullException("selector");
}
using (var enumerator = source.GetEnumerator())
{
if (enumerator.MoveNext())
{
var left = enumerator.Current;
while (enumerator.MoveNext())
{
var right = enumerator.Current;
yield return selector(left, right);
left = right;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.