Skip to content

Instantly share code, notes, and snippets.

@leppie
Created March 6, 2013 14:35
Show Gist options
  • Save leppie/5099693 to your computer and use it in GitHub Desktop.
Save leppie/5099693 to your computer and use it in GitHub Desktop.
Map with variable number of lists in C#
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