Created
November 27, 2010 19:24
-
-
Save vcsjones/718189 to your computer and use it in GitHub Desktop.
WP7 Zip
This file contains hidden or 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
//Throws exception if number of items in the collection are different. | |
public static IEnumerable<R> Zip<T, U, R>(this IEnumerable<T> first, IEnumerable<U> second, Func<T, U, R> selector) | |
{ | |
var enumerator1 = first.GetEnumerator(); | |
var enumerator2 = second.GetEnumerator(); | |
while (true) | |
{ | |
var move1 = enumerator1.MoveNext(); | |
var move2 = enumerator2.MoveNext(); | |
if (move1 != move2) | |
{ | |
throw new InvalidOperationException("Zip collections contain different number of items."); | |
} | |
if (!move1) | |
{ | |
yield break; | |
} | |
yield return selector(enumerator1.Current, enumerator2.Current); | |
} | |
} | |
// Does not throw an exception if counts are different, uses the minimum. | |
public static IEnumerable<R> Zip<T, U, R>(this IEnumerable<T> first, IEnumerable<U> second, Func<T, U, R> selector) | |
{ | |
var enumerator1 = first.GetEnumerator(); | |
var enumerator2 = second.GetEnumerator(); | |
while (enumerator1.MoveNext() && enumerator2.MoveNext()) | |
{ | |
yield return selector(enumerator1.Current, enumerator2.Current); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment