From cc34bcb19edb3a79bc87f829e98c3e712ec72b3b Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Tue, 13 Feb 2018 16:52:00 -0500 Subject: [PATCH] Adding extension methods for zipping sequences --- .../Extensions/EnumerableExtensions.cs | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs index 432bd88..f40fa3f 100644 --- a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs @@ -964,5 +964,73 @@ namespace ICD.Common.Utils.Extensions return any; } + + /// + /// Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results. + /// + /// + /// + /// + /// + /// + public static void Zip(this IEnumerable first, + IEnumerable second, + Action callback) + { + if (first == null) + throw new ArgumentNullException("first"); + + if (second == null) + throw new ArgumentNullException("second"); + + if (callback == null) + throw new ArgumentNullException("callback"); + + using (IEnumerator enumerator1 = first.GetEnumerator()) + { + using (IEnumerator enumerator2 = second.GetEnumerator()) + { + while (enumerator1.MoveNext() && enumerator2.MoveNext()) + callback(enumerator1.Current, enumerator2.Current); + } + } + } + +#if SIMPLSHARP + + /// + /// Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results. + /// + /// + /// + /// + /// + /// + /// + /// + public static IEnumerable Zip(this IEnumerable first, + IEnumerable second, + Func callback) + { + if (first == null) + throw new ArgumentNullException("first"); + + if (second == null) + throw new ArgumentNullException("second"); + + if (callback == null) + throw new ArgumentNullException("callback"); + + using (IEnumerator enumerator1 = first.GetEnumerator()) + { + using (IEnumerator enumerator2 = second.GetEnumerator()) + { + while (enumerator1.MoveNext() && enumerator2.MoveNext()) + yield return callback(enumerator1.Current, enumerator2.Current); + } + } + } + +#endif } }