Mega Code Archive

 
Categories / C# / Collections Data Structure
 

Creates a list by applying a delegate to pairs of items in the IEnumerableT

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