Esempio n. 1
0
 public static Option <Age> Of(int age) => new Some <Age>(Age.Create(age));
Esempio n. 2
0
        static void FunctionalProgrammingExamples()
        {
            try {
                var book = BookStore.Models.Book.Create(null, DateTime.Now);
                Write($"{book.Name} was published in {book.PublishDate.ToString()}");
            } catch (ArgumentNullException nullError) {
                WriteLine(nullError.Message);
            }

            "".ToTime(() => {
                var age1 = Age.Create(12);
                var age2 = Age.Create(23);

                WriteLine($"{age1}>{age2}");
                WriteLine($"{age2} >14");
                return(Unit.Create());
            });
            getMessage();
            string getMessage()
            {
                return("hello");
            }

            try {
                var r = TestOption().Match(
                    (name) => $"Greetings from {name}",
                    () =>
                    throw new System.NullReferenceException("REPLY IS NULL OK")
                    );
                WriteLine("r:" + r);
            } catch (Exception e) {
                WriteLine(e.Message);
            }

            try {
                // Parse value example
                WriteLine("232".Parse().Match((i) => i, () => 0));
                // Parse example with throw exception
                WriteLine("45".Parse().Match(i => i, () =>
                                             throw new InvalidCastException("fail to type cast ...")));
                // Map example
                // foreach (var item in new [] { "One", "Two", "Three", "Double Fees" }.Map(x => $"{x} has {x.Length} characters.")) {
                //     WriteLine(item);
                // }
            } catch (Exception err) {
                WriteLine(err.Message);
            }

            // Map example

            var listOfOptions = new List <Option <string> >();

            listOfOptions.Add(None.Instance);
            listOfOptions.Add(new Some <string>("Good boy 1"));
            listOfOptions.Add(None.Instance);
            listOfOptions.Add(new Some <string>("Good boy 2"));
            listOfOptions.Add(None.Instance);
            listOfOptions.Add(new Some <string>("Good boy 3"));

            foreach (var item in listOfOptions.Map(y => y.Content.Replace(" ", "___")))
            {
                WriteLine(item);
            }
        }