Esempio n. 1
0
        /// <summary>
        /// Projects each element of a <paramref name="indexedSequence"/> into a form provided by the given <paramref name="selector"/>.
        /// In addition to the element of the sequence the action also gets index of it during this processing. Note that
        /// the given index must no be equal to its real index within the underlying collection.
        /// </summary>
        /// <typeparam name="TResult">Type of object resulting the transform function</typeparam>
        /// <param name="indexedSequence">Collection of items</param>
        /// <param name="selector">Transform function</param>
        /// <returns>Collection of transformed items</returns>
        public static IEnumerable <TResult> Select <TResult>(this IIndexedEnumerable indexedSequence,
                                                             Func <int, object, TResult> selector)
        {
            int i = -1;

            for (IEnumerator itr = indexedSequence.GetEnumerator(); itr.MoveNext();)
            {
                yield return(selector(++i, itr.Current));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Applies the given strong-typed <paramref name="action"/> to each element of the given <paramref name="indexedSequence"/>.
        /// In addition to the element of the sequence the action also gets index of it during this processing. Note that
        /// the given index must no be equal to its real index within the underlying collection.
        /// </summary>
        /// <typeparam name="TItem">Type of elements to iterate</typeparam>
        /// <param name="indexedSequence">Collection of items</param>
        /// <param name="action">Action to apply to each element</param>
        public static void ForEach <TItem>(this IIndexedEnumerable <TItem> indexedSequence, Action <int, TItem> action)
        {
            int i = -1;

            using (IEnumerator <TItem> itr = indexedSequence.GetEnumerator()) {
                while (itr.MoveNext())
                {
                    action(++i, itr.Current);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Projects each strong-typed element a <paramref name="indexedSequence"/> into a form provided by the given <paramref name="selector"/>.
        /// In addition to the element of the sequence the action also gets index of it during this processing. Note that
        /// the given index must no be equal to its real index within the underlying collection.
        /// </summary>
        /// <typeparam name="TItem">Type of elements to iterate</typeparam>
        /// <typeparam name="TResult">Type of object resulting the transform function</typeparam>
        /// <param name="indexedSequence">Collection of items</param>
        /// <param name="selector">Transform function</param>
        /// <returns>Collection of transformed items</returns>
        public static IEnumerable <TResult> Select <TItem, TResult>(this IIndexedEnumerable <TItem> indexedSequence,
                                                                    Func <int, TItem, TResult> selector)
        {
            int i = -1;

            using (IEnumerator <TItem> itr = indexedSequence.GetEnumerator()) {
                while (itr.MoveNext())
                {
                    yield return(selector(++i, itr.Current));
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Applies the given <paramref name="action"/> to each element of the given <paramref name="indexedSequence"/>.
        /// In addition to the element of the sequence the action also gets index of it during this processing. Note that
        /// the given index must no be equal to its real index within the underlying collection.
        /// </summary>
        /// <param name="indexedSequence">Collection of items</param>
        /// <param name="action">Action to apply to each element</param>
        public static void ForEach(this IIndexedEnumerable indexedSequence, Action <int, object> action)
        {
            if (indexedSequence == null)
            {
                throw new NullReferenceException();
            }
            IEnumerator itr = indexedSequence.GetEnumerator();

            try {
                int i = -1;
                while (itr.MoveNext())
                {
                    action(++i, itr.Current);
                }
            } finally {
                (itr as IDisposable)?.Dispose();
            }
        }