Created
March 6, 2013 14:35
-
-
Save leppie/5099693 to your computer and use it in GitHub Desktop.
Map with variable number of lists in C#
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
static IEnumerable<T> Map<T>(this Func<IEnumerable<T>, T> f, params IEnumerable<T>[] arr) | |
{ | |
var enums = Array.ConvertAll(arr, x => x.GetEnumerator()); | |
try | |
{ | |
while (enums.All(x => x.MoveNext())) | |
{ | |
yield return f(enums.Select(x => x.Current)); | |
} | |
} | |
finally | |
{ | |
foreach (var e in enums) e.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment