Ejemplo n.º 1
0
 /// <summary>Returns new list in reverse order.</summary>
 /// <typeparam name="T">List item type</typeparam> <param name="source">List to reverse.</param>
 /// <returns>New list. If list consist on single element, then the same list.</returns>
 public static ImList <T> Reverse <T>(this ImList <T> source)
 {
     if (source.IsEmpty || source.Tail.IsEmpty)
     {
         return(source);
     }
     return(source.To(ImList <T> .Empty, (it, _) => _.Prep(it)));
 }
Ejemplo n.º 2
0
     /// <summary>Copies list to array.</summary>
     /// <param name="source">list to convert.</param>
     /// <returns>Array with list items.</returns>
     public static T[] ToArray <T>(this ImList <T> source)
     {
         if (source.IsEmpty)
         {
             return(new T[0]);
         }
         if (source.Tail.IsEmpty)
         {
             return new[] { source.Head }
         }
         ;
         return(source.Enumerate().ToArray());
     }
 }
Ejemplo n.º 3
0
        /// <summary>Form of fold function with element index for convenience.</summary>
        /// <typeparam name="T">Type of list item.</typeparam>
        /// <typeparam name="R">Type of result.</typeparam>
        /// <param name="source">List to fold.</param>
        /// <param name="initialValue">From were to start.</param>
        /// <param name="collect">Collects list item into result</param>
        /// <returns>Return result or <paramref name="initialValue"/> for empty list.</returns>
        public static R To <T, R>(this ImList <T> source, R initialValue, Func <T, int, R, R> collect)
        {
            if (source.IsEmpty)
            {
                return(initialValue);
            }
            var value = initialValue;

            for (var i = 0; !source.IsEmpty; source = source.Tail)
            {
                value = collect(source.Head, i++, value);
            }
            return(value);
        }
Ejemplo n.º 4
0
 /// <summary>Maps the items from the first list to the result list with item index.</summary>
 /// <typeparam name="T">source item type.</typeparam>
 /// <typeparam name="R">result item type.</typeparam>
 /// <param name="source">input list.</param> <param name="map">converter func.</param>
 /// <returns>result list.</returns>
 public static ImList <R> Map <T, R>(this ImList <T> source, Func <T, int, R> map)
 {
     return(source.To(ImList <R> .Empty, (it, i, _) => _.Prep(map(it, i))).Reverse());
 }
Ejemplo n.º 5
0
 private ImList(T head, ImList <T> tail)
 {
     Head = head;
     Tail = tail;
 }