private FList(T head, FList <T> tail)
 {
     Head = head;
     Tail = tail.IsEmpty
         ? Empty
         : tail;
     IsEmpty = false;
 }
Exemple #2
0
        public static void Run()
        {
            var data = new int[1000000];

            for (int i = 0; i < data.Length; i++)
            {
                data[i] = i;
            }

            Stopwatch stopwatch = new Stopwatch();

            // Begin timing.
            stopwatch.Start();
            long total1 =
                data.AsParallel()
                .Where(n => n % 2 == 0)
                .Select(n => n + n)
                .Sum(x => (long)x);

            stopwatch.Stop();

            // Write result.
            Console.WriteLine($"Time elapsed for normal: {stopwatch.Elapsed} with result = {total1}");

            stopwatch.Start();
            long total2 = data.AsParallel()
                          .Aggregate(0L, (acc, n) => n % 2 == 0 ? acc + (n + n) : acc);

            stopwatch.Stop();

            // Write result.
            Console.WriteLine($"Time elapsed for deforested: {stopwatch.Elapsed} with result = {total2}");

            Console.WriteLine(Greeting("Richard"));
            System.Threading.Thread.Sleep(2000);
            Console.WriteLine(Greeting("Paul"));
            System.Threading.Thread.Sleep(2000);
            Console.WriteLine(Greeting("Richard"));

            FList <int> list1 = FList <int> .Empty;
            FList <int> list2 = list1.Cons(1).Cons(2).Cons(3);
            FList <int> list3 = FList <int> .Cons(1, FList <int> .Empty);

            FList <int> list4 = list2.Cons(2).Cons(3);

            Func <int, bool> isPrime = n => {
                if (n == 1)
                {
                    return(false);
                }
                if (n == 2)
                {
                    return(true);
                }
                var boundary = (int)Math.Floor(Math.Sqrt(n));
                for (int i = 2; i <= boundary; ++i)
                {
                    if (n % i == 0)
                    {
                        return(false);
                    }
                }
                return(true);
            };

            int  len = 10000000, count = 0;
            long total = 0;

            Parallel.For(0, len,
                         //i => {
                         //if (isPrime(i))
                         //{
                         //    total += i;
                         //    count += 1;
                         //}});
                         () => 0,
                         (int i, ParallelLoopState loopState, long tlsValue) => isPrime(i) ? tlsValue += i : tlsValue,
                         value =>
            {
                Interlocked.Add(ref total, value);
                if (value > 0)
                {
                    Interlocked.Increment(ref count);
                }
            });// TODO: how do I calculate rigorously the total in the same parallel enumeration?


            Console.WriteLine($"total={total} count={count}");

            Console.WriteLine(Greeting("Richard"));
            Thread.Sleep(2000);
            Console.WriteLine(Greeting("Paul"));
            Thread.Sleep(2000);
            Console.WriteLine(Greeting("Richard"));

            Func <string, string> grFunc = (name) => $"Warm greetings {name}, the time is {DateTime.Now:hh:mm:ss}";
            var greetingMemoize          = grFunc.Memoize(); // FuncExtensionMethods.Memoize<string, string>(Greeting);

            Console.WriteLine(greetingMemoize("Richard"));
            System.Threading.Thread.Sleep(2000);
            Console.WriteLine(greetingMemoize("Paul"));
            System.Threading.Thread.Sleep(2000);
            Console.WriteLine(greetingMemoize("Richard"));

            var greetingMemoize2 = grFunc.MemoizeLazyThreadSafe();

            Console.WriteLine(greetingMemoize2("Richard"));
            System.Threading.Thread.Sleep(2000);
            Console.WriteLine(greetingMemoize2("Paul"));
            System.Threading.Thread.Sleep(2000);

            Console.WriteLine(greetingMemoize("Richard"));
        }
 public static FList <T> Cons(T head, FList <T> tail)
 {
     return(tail.IsEmpty
         ? new FList <T>(head, Empty)
         : new FList <T>(head, tail));
 }