Last active
January 21, 2022 02:40
-
-
Save ChuckSavage/5858943 to your computer and use it in GitHub Desktop.
It seems an oversight on the .Net team to not enable multiple arrays to be passed to Enumerable.Zip. So I've created up to seven overloaded Zip extensions that borrow from the code by Eric Lippert here: http://blogs.msdn.com/b/ericlippert/archive/2009/05/07/zip-me-up.aspx
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
using System; | |
using System.Collections.Generic; | |
namespace ZipMany | |
{ | |
/// <summary> | |
/// Zip multiple arrays (more than 2 since there is a built in Zip for 2 with .Net 4.0) | |
/// </summary> | |
public static class ZipMultiple | |
{ | |
#region Zip Three | |
public static IEnumerable<TResult> Zip<T1, T2, T3, TResult> | |
( | |
this IEnumerable<T1> first, | |
IEnumerable<T2> second, | |
IEnumerable<T3> third, | |
Func<T1, T2, T3, TResult> result | |
) | |
{ | |
if (first == null) throw new ArgumentNullException("first"); | |
if (second == null) throw new ArgumentNullException("second"); | |
if (third == null) throw new ArgumentNullException("third"); | |
if (result == null) throw new ArgumentNullException("result"); | |
using (IEnumerator<T1> e1 = first.GetEnumerator()) | |
using (IEnumerator<T2> e2 = second.GetEnumerator()) | |
using (IEnumerator<T3> e3 = third.GetEnumerator()) | |
while (e1.MoveNext() | |
&& e2.MoveNext() | |
&& e3.MoveNext() | |
) | |
yield return result(e1.Current, e2.Current, e3.Current); | |
} | |
#endregion | |
#region Zip Four | |
public static IEnumerable<TResult> Zip<T1, T2, T3, T4, TResult> | |
( | |
this IEnumerable<T1> first, | |
IEnumerable<T2> second, | |
IEnumerable<T3> third, | |
IEnumerable<T4> forth, | |
Func<T1, T2, T3, T4, TResult> result | |
) | |
{ | |
if (first == null) throw new ArgumentNullException("first"); | |
if (second == null) throw new ArgumentNullException("second"); | |
if (third == null) throw new ArgumentNullException("third"); | |
if (forth == null) throw new ArgumentNullException("forth"); | |
if (result == null) throw new ArgumentNullException("result"); | |
using (IEnumerator<T1> e1 = first.GetEnumerator()) | |
using (IEnumerator<T2> e2 = second.GetEnumerator()) | |
using (IEnumerator<T3> e3 = third.GetEnumerator()) | |
using (IEnumerator<T4> e4 = forth.GetEnumerator()) | |
while (e1.MoveNext() | |
&& e2.MoveNext() | |
&& e3.MoveNext() | |
&& e4.MoveNext() | |
) | |
yield return result(e1.Current, e2.Current, e3.Current, e4.Current); | |
} | |
#endregion | |
#region Zip Five | |
public static IEnumerable<TResult> Zip<T1, T2, T3, T4, T5, TResult> | |
( | |
this IEnumerable<T1> first, | |
IEnumerable<T2> second, | |
IEnumerable<T3> third, | |
IEnumerable<T4> forth, | |
IEnumerable<T5> fifth, | |
Func<T1, T2, T3, T4, T5, TResult> result | |
) | |
{ | |
if (first == null) throw new ArgumentNullException("first"); | |
if (second == null) throw new ArgumentNullException("second"); | |
if (third == null) throw new ArgumentNullException("third"); | |
if (forth == null) throw new ArgumentNullException("forth"); | |
if (fifth == null) throw new ArgumentNullException("fifth"); | |
if (result == null) throw new ArgumentNullException("result"); | |
using (IEnumerator<T1> e1 = first.GetEnumerator()) | |
using (IEnumerator<T2> e2 = second.GetEnumerator()) | |
using (IEnumerator<T3> e3 = third.GetEnumerator()) | |
using (IEnumerator<T4> e4 = forth.GetEnumerator()) | |
using (IEnumerator<T5> e5 = fifth.GetEnumerator()) | |
while (e1.MoveNext() | |
&& e2.MoveNext() | |
&& e3.MoveNext() | |
&& e4.MoveNext() | |
&& e5.MoveNext() | |
) | |
yield return result(e1.Current, e2.Current, e3.Current, e4.Current, e5.Current); | |
} | |
#endregion | |
#region Zip Six | |
public static IEnumerable<TResult> Zip<T1, T2, T3, T4, T5, T6, TResult> | |
( | |
this IEnumerable<T1> first, | |
IEnumerable<T2> second, | |
IEnumerable<T3> third, | |
IEnumerable<T4> forth, | |
IEnumerable<T5> fifth, | |
IEnumerable<T6> sixth, | |
Func<T1, T2, T3, T4, T5, T6, TResult> result | |
) | |
{ | |
if (first == null) throw new ArgumentNullException("first"); | |
if (second == null) throw new ArgumentNullException("second"); | |
if (third == null) throw new ArgumentNullException("third"); | |
if (forth == null) throw new ArgumentNullException("forth"); | |
if (fifth == null) throw new ArgumentNullException("fifth"); | |
if (sixth == null) throw new ArgumentNullException("sixth"); | |
if (result == null) throw new ArgumentNullException("result"); | |
using (IEnumerator<T1> e1 = first.GetEnumerator()) | |
using (IEnumerator<T2> e2 = second.GetEnumerator()) | |
using (IEnumerator<T3> e3 = third.GetEnumerator()) | |
using (IEnumerator<T4> e4 = forth.GetEnumerator()) | |
using (IEnumerator<T5> e5 = fifth.GetEnumerator()) | |
using (IEnumerator<T6> e6 = sixth.GetEnumerator()) | |
while (e1.MoveNext() | |
&& e2.MoveNext() | |
&& e3.MoveNext() | |
&& e4.MoveNext() | |
&& e5.MoveNext() | |
&& e6.MoveNext() | |
) | |
yield return result(e1.Current, e2.Current, e3.Current, e4.Current, e5.Current, e6.Current); | |
} | |
#endregion | |
#region Zip Seven | |
public static IEnumerable<TResult> Zip<T1, T2, T3, T4, T5, T6, T7, TResult> | |
( | |
this IEnumerable<T1> first, | |
IEnumerable<T2> second, | |
IEnumerable<T3> third, | |
IEnumerable<T4> forth, | |
IEnumerable<T5> fifth, | |
IEnumerable<T6> sixth, | |
IEnumerable<T7> seventh, | |
Func<T1, T2, T3, T4, T5, T6, T7, TResult> result | |
) | |
{ | |
if (first == null) throw new ArgumentNullException("first"); | |
if (second == null) throw new ArgumentNullException("second"); | |
if (third == null) throw new ArgumentNullException("third"); | |
if (forth == null) throw new ArgumentNullException("forth"); | |
if (fifth == null) throw new ArgumentNullException("fifth"); | |
if (sixth == null) throw new ArgumentNullException("sixth"); | |
if (seventh == null) throw new ArgumentNullException("seventh"); | |
if (result == null) throw new ArgumentNullException("result"); | |
using (IEnumerator<T1> e1 = first.GetEnumerator()) | |
using (IEnumerator<T2> e2 = second.GetEnumerator()) | |
using (IEnumerator<T3> e3 = third.GetEnumerator()) | |
using (IEnumerator<T4> e4 = forth.GetEnumerator()) | |
using (IEnumerator<T5> e5 = fifth.GetEnumerator()) | |
using (IEnumerator<T6> e6 = sixth.GetEnumerator()) | |
using (IEnumerator<T7> e7 = seventh.GetEnumerator()) | |
while (e1.MoveNext() | |
&& e2.MoveNext() | |
&& e3.MoveNext() | |
&& e4.MoveNext() | |
&& e5.MoveNext() | |
&& e6.MoveNext() | |
&& e7.MoveNext() | |
) | |
yield return result(e1.Current, | |
e2.Current, | |
e3.Current, | |
e4.Current, | |
e5.Current, | |
e6.Current, | |
e7.Current | |
); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment