Created
April 22, 2012 09:59
-
-
Save khmylov/2463179 to your computer and use it in GitHub Desktop.
C# implementation of Seq.pairwise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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