using System.Linq;
using System.Diagnostics;
namespace System.Collections.Generic
{
public static class EnumeratorExtensions
{
///
/// Creates a list by applying a delegate to pairs of items in the IEnumerable<T>.
///
///
///
/// The source list of items.
/// The delegate to use to combine items.
///
public static IEnumerable Scan(this IEnumerable source, Func combine)
{
if (source == null)
throw new ArgumentNullException("source");
if (combine == null)
throw new ArgumentNullException("combine");
return ScanIterator(source, combine);
}
private static IEnumerable ScanIterator(IEnumerable source, Func combine)
{
Debug.Assert(source != null, "source is null.");
Debug.Assert(combine != null, "combine is null.");
using (IEnumerator data = source.GetEnumerator())
if (data.MoveNext())
{
T first = data.Current;
while (data.MoveNext())
{
yield return combine(first, data.Current);
first = data.Current;
}
}
}
}
}