Skip to content

Instantly share code, notes, and snippets.

@khmylov
Created April 22, 2012 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khmylov/2463179 to your computer and use it in GitHub Desktop.
Save khmylov/2463179 to your computer and use it in GitHub Desktop.
C# implementation of Seq.pairwise
public static IEnumerable<Tuple<T, T>> Pairwise<T>(this IEnumerable<T> source)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
var previous = default(T);
using (var enumerator = source.GetEnumerator())
{
if (enumerator.MoveNext())
{
previous = enumerator.Current;
}
while (enumerator.MoveNext())
{
yield return Tuple.Create(previous, enumerator.Current);
previous = enumerator.Current;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment