Beispiel #1
0
        static void Main(string[] args)
        {
            PureList <int?> pureList = new PureList <int?>(1, 2, 3, 4, 5, 6, 7, 8, 9);

            WriteLine("Calling Head => ");
            WriteLine(pureList.Head());
            WriteLine();

            WriteLine("Calling Tail => ");
            PureList <int?> TailList = pureList.Tail();

            for (int i = 0; i < TailList.Length; i++)
            {
                Write(TailList[i] + " ");
            }
            WriteLine();
            WriteLine();

            WriteLine("Callng Cons => ");
            PureList <int?> ConsList = pureList.Cons(99);

            for (int i = 0; i < ConsList.Length; i++)
            {
                Write(ConsList[i] + " ");
            }
            WriteLine();
            WriteLine();

            WriteLine("Calling Drop => ");
            PureList <int?> DropList = pureList.Drop(2);

            for (int i = 0; i < DropList.Length; i++)
            {
                Write(DropList[i] + " ");
            }
            WriteLine();
            WriteLine();

            WriteLine("Callinh Reverse => ");
            PureList <int?> ReverseList = pureList.Reverse();

            for (int i = 0; i < ReverseList.Length; i++)
            {
                Write(ReverseList[i] + " ");
            }
            WriteLine();
            WriteLine();

            WriteLine("Calling Filter");
            PureList <int?> EvenElementList = pureList.Filter(e => e % 2 == 0);

            for (int i = 0; i < EvenElementList.Length; i++)
            {
                Write(EvenElementList[i] + " ");
            }
            WriteLine();
        }
Beispiel #2
0
 private static T[] CopyTail <T>(PureList <T> s, T[] d, int N)
 {
     if (N < 0)
     {
         return(d);
     }
     d[N] = s[N + 1];
     return(CopyTail(s, d, --N));
 }
Beispiel #3
0
 private static T[] ReverseList <T>(PureList <T> list, T[] reverseList, int start, int end)
 {
     if (start > end)
     {
         return(reverseList);
     }
     reverseList[start] = list[end];
     reverseList[end]   = list[start];
     return(ReverseList(list, reverseList, start + 1, end - 1));
 }
Beispiel #4
0
        public static PureList <T> Cons <T>(this PureList <T> pureList, T element)
        {
            var consContent = new T[pureList.Length + 1];

            consContent[0] = element;
            return(new PureList <T>(CopyCons(
                                        pureList,
                                        consContent,
                                        pureList.Length - 1)));
        }
Beispiel #5
0
 private static T[] Copy <T>(PureList <T> source, T[] destination, int sourceStart, int sourceEnd, int destinationEnd)
 {
     if (sourceStart > sourceEnd)
     {
         return(destination);
     }
     destination[destinationEnd] = source[sourceEnd];
     return(Copy(
                source,
                destination,
                sourceStart,
                sourceEnd - 1,
                destinationEnd - 1));
 }
Beispiel #6
0
 public static PureList <T> Filter <T>(this PureList <T> pureList, FilterPredicate <T> predicate) =>
 FilterApply(
     pureList,
     predicate,
     new PureList <T>(),
     pureList.Length - 1);
Beispiel #7
0
 private static PureList <T> FilterApply <T>(PureList <T> list, FilterPredicate <T> predicate, PureList <T> output, int n) =>
 (n < 0) ? output : FilterApply(
     list,
     predicate,
     predicate(list[n]) ? output.Cons(list[n]) : output,
     n - 1);
Beispiel #8
0
 public static T Head <T>(this PureList <T> purelist) => purelist.Length > 0 ? purelist[0] : default(T);
Beispiel #9
0
 public static PureList <T> Reverse <T>(this PureList <T> pureList) => new PureList <T>(ReverseList(
                                                                                            pureList,
                                                                                            new T[pureList.Length],
                                                                                            0,
                                                                                            pureList.Length - 1));
Beispiel #10
0
 public static PureList <T> Drop <T>(this PureList <T> pureList, int n) => new PureList <T>(Copy(
                                                                                                pureList,
                                                                                                new T[pureList.Length - n],
                                                                                                n,
                                                                                                pureList.Length - 1,
                                                                                                pureList.Length - n - 1));
Beispiel #11
0
 public static PureList <T> Tail <T>(this PureList <T> pureList) => new PureList <T>(CopyTail(
                                                                                         pureList,
                                                                                         new T[pureList.Length - 1],
                                                                                         pureList.Length - 2));