Exemple #1
0
        private static void Main(string[] args)
        {
            var timekeeper = new Timekeeper();  // Timekeeper allows us to measure how long an action takes to run

            var elapsed = timekeeper.Measure(() =>
            {
                // var numbers = new[] { 3, 5, 7, 9, 11, 13 };
                // using GetLazyRandomNumber() now for a stream of numbers
                foreach (var prime in GetLazyRandomNumber(100).Find(IsPrime).Take(2))
                {
                    Console.WriteLine("Prime number found: {0}", prime);
                }
            });

            Console.WriteLine("The elapsed time was {0}", elapsed);
            Console.ReadLine();
            // Partial example

            var client = new WebClient();
            Func <string, string> download = url => client.DownloadString(url);

            var data = download.Partial("https://api.tlopo.com/system/status/").WithRetry();

            Console.WriteLine("Using Partial function with retry returns the following string: \nn" + data);
            Console.ReadLine();
            // Curry example
            // build a Func of string that returns a func of string
            Func <string, Func <string> > downloadCurry = download.Curry();
            var data2 = downloadCurry("https://api.tlopo.com/system/status/").WithRetry();

            Console.WriteLine("Using Curry function with retry returns the following string: \n\n" + data2);

            // Now let's use TPL Async and Task in functional programming.

            elapsed = timekeeper.Measure(() => FindLargePrimes(900000, 1000000));
            Console.WriteLine("Finding all prime numbers took: {0}", elapsed);

            // now use Async()
            elapsed = timekeeper.Measure(() => FindLargePrimesAsynch(900000, 1000000));
            Console.WriteLine("Finding all prime numbers in parallel took: {0}", elapsed);

            // now use Task
            var task  = new Task <IEnumerable <int> >(() => FindLargePrimes(3, 100));
            var task2 = task.ContinueWith((prev) =>
            {
                foreach (var number in prev.Result)
                {
                    Console.WriteLine("The number found was {0}", number);
                }
            });

            task.Start();
            Console.WriteLine("Doing some other work right now");

            task2.Wait();
        }