using System.Linq;
using System.Diagnostics;
namespace System.Collections.Generic
{
public static class EnumeratorExtensions
{
///
/// Creates a list by combining two other lists into one.
///
///
///
///
/// One of the lists to zip.
/// One of the lists to zip.
/// The delegate used to combine items.
/// A new list with the combined items.
public static IEnumerable Zip(this IEnumerable source1, IEnumerable source2, Func combine)
{
if (source1 == null)
throw new ArgumentNullException("source1");
if (source2 == null)
throw new ArgumentNullException("source2");
if (combine == null)
throw new ArgumentNullException("combine");
return ZipIterator(source1, source2, combine);
}
private static IEnumerable ZipIterator(IEnumerable source1, IEnumerable source2, Func combine)
{
Debug.Assert(source1 != null, "source1 is null.");
Debug.Assert(source2 != null, "source2 is null.");
Debug.Assert(combine != null, "combine is null.");
using (IEnumerator data1 = source1.GetEnumerator())
using (IEnumerator data2 = source2.GetEnumerator())
while (data1.MoveNext() && data2.MoveNext())
{
yield return combine(data1.Current, data2.Current);
}
}
}
}