Example #1
0
        /// <summary>
        /// Applies an accumulator delegate over a sequence starting with a given seed value, yielding each intermediate result. The <paramref name="seed"/> value is the first element of the source sequence. Each element of the source sequence is only evaluated once.
        /// </summary>
        /// <typeparam name="TSource">The type of elements in the source sequence.</typeparam>
        /// <typeparam name="TResult">The type of elements in the resulting sequence.</typeparam>
        /// <param name="source">The source sequence.</param>
        /// <param name="seed">The initial seed value.</param>
        /// <param name="accumulator">The accumulator delegate. The first parameter passed to the accumulator delegate is the previous intermediate result; the second parameter is the current element of the source sequence.</param>
        /// <returns>The results of the accumulator delegate.</returns>
        public static IEnumerable <TResult> Scan <TSource, TResult>(this IEnumerable <TSource> source, TResult seed, Func <TResult, TSource, TResult> accumulator)
        {
            IEnumerable <TResult> scan = EnumerableSource.Defer(() =>
            {
                TResult current = seed;
                return(source.Select(x => current = accumulator(seed, x)));
            });

            return(EnumerableSource.Return(seed).Concat(scan));
        }
Example #2
0
        /// <summary>
        /// Applies an accumulator delegate over a sequence, yielding each intermediate result. The first element of the returned sequence is the first element of the source sequence. Each element of the source sequence is only evaluated once.
        /// </summary>
        /// <typeparam name="T">The type of elements in the sequence.</typeparam>
        /// <param name="source">The source sequence.</param>
        /// <param name="accumulator">The accumulator delegate. The first parameter passed to the accumulator delegate is the previous intermediate result; the second parameter is the current element of the source sequence.</param>
        /// <returns>The results of the accumulator delegate.</returns>
        public static IEnumerable <T> Scan <T>(this IEnumerable <T> source, Func <T, T, T> accumulator)
        {
            return(EnumerableSource.Defer(() =>
            {
                T current = default(T);
                bool currentValid = false;

                return source.Select(x =>
                {
                    if (currentValid)
                    {
                        current = accumulator(current, x);
                    }
                    else
                    {
                        currentValid = true;
                        current = x;
                    }

                    return current;
                });
            }));
        }
 /// <summary>
 /// Converts a single value into a sorted sequence containing that value the specified number of times. The sequence is treated as though it were sorted by the specified comparison object.
 /// </summary>
 /// <typeparam name="T">The type of the value.</typeparam>
 /// <param name="source">The value.</param>
 /// <param name="comparer">The comparison object that defines how the sequence is sorted.</param>
 /// <param name="count">The number of times <paramref name="source"/> is repeated. If <paramref name="count"/> is less than or equal to 0, an empty sequence is returned.</param>
 /// <returns>A sorted sequence containing <paramref name="count"/> elements, all equal to <paramref name="source"/>.</returns>
 public static ISortedEnumerable <T> Repeat <T>(T source, IComparer <T> comparer, int count)
 {
     return(new SortedEnumerableWrapper <T>(EnumerableSource.Repeat(source, count), comparer));
 }
 /// <summary>
 /// Converts a single value into a sorted sequence containing a single value. The sequence is treated as though it were sorted by the specified comparison delegate.
 /// </summary>
 /// <typeparam name="T">The type of the value.</typeparam>
 /// <param name="source">The value.</param>
 /// <param name="comparer">The comparison delegate that defines how the sequence is sorted.</param>
 /// <returns>A sorted sequence containing a single element, <paramref name="source"/>.</returns>
 public static ISortedEnumerable <T> Return <T>(T source, Func <T, T, int> comparer)
 {
     return(new SortedEnumerableWrapper <T>(EnumerableSource.Return(source), A.Comparer(comparer)));
 }
 /// <summary>
 /// Converts a single value into a sorted sequence containing a single value. The sequence is treated as though it were sorted by the default comparison object.
 /// </summary>
 /// <typeparam name="T">The type of the value.</typeparam>
 /// <param name="source">The value.</param>
 /// <returns>A sorted sequence containing a single element, <paramref name="source"/>.</returns>
 public static ISortedEnumerable <T> Return <T>(T source)
 {
     return(new SortedEnumerableWrapper <T>(EnumerableSource.Return(source), Comparer <T> .Default));
 }
 /// <summary>
 /// Converts a single value into a sorted sequence containing a single value. The sequence is treated as though it were sorted by the specified comparison object.
 /// </summary>
 /// <typeparam name="T">The type of the value.</typeparam>
 /// <param name="source">The value.</param>
 /// <param name="comparer">The comparison object that defines how the sequence is sorted.</param>
 /// <returns>A sorted sequence containing a single element, <paramref name="source"/>.</returns>
 public static ISortedEnumerable <T> Return <T>(T source, IComparer <T> comparer)
 {
     return(new SortedEnumerableWrapper <T>(EnumerableSource.Return(source), comparer));
 }