Exemplo n.º 1
0
 public static Author Create(NonNullValue <string> name, DateTime dateOfPublish) => new Author(name, dateOfPublish);
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var authors = new List <Option <Author> >();

            for (var i = 1; i < 100; i++)
            {
                authors.Add(new Some <Author>(Author.Create(NonNullValue <string> .Create($"Author {i}"), DateTime.Now.AddDays(i))));
            }

            // search example
            var item = authors
                       .Where(z => z.Content.Name.Content == "Khurram")
                       .DefaultIfEmpty(new Option <Author>())
                       .Single();

            try {
                var addedAuthor = item.Match(z => z, () => {
                    var newAuthor = Author.Create(NonNullValue <string> .Create("Khurram"), DateTime.Now);
                    authors.Add(new Some <Author>(newAuthor));
                    return(newAuthor);
                });
                WriteLine($"author:{addedAuthor.Name.Content},Date of publish:{addedAuthor.DateOfPublish}");
            } catch (Exception err) {
                WriteLine(err.Message);
            }

            // Map , Bind examples

            foreach (int v in new [] { 3, 4, 5, 6, 7, 8, 9 }.Map1(i => i * i))
            {
                WriteLine($"Square value:{v}");
            }

            var name = new Option <string>("Khurram Shahzad");
            Func <string, string> message = m => $"welcome {m}";

            WriteLine(name.Map1(message).Content);

            var _ = None.Instance;

            WriteLine(_.Map1(message).Content);

            // For Each example
            new [] { 3, 4, 5, 6, 7, 8, 9 }
            .Map1(i => i * i)
            .ForEach(WriteLine);

            // Bind example
            WriteLine($"Your age is : {getAge().Content}");
            Age getAge() =>
            "Input age:"
            .Prompt()
            .Parse()
            .Bind(Age.Of)
            .Match(x => x, () => getAge());

            TasksExample();

            Func <string, string, string> greet = (a, b) => $"{a}-{b}";

            var applyEx    = greet.Apply("hello");
            var curryingEx = greet.Curry();

            WriteLine(applyEx("boy"));
            WriteLine(curryingEx("cool")("boy"));
        }
Exemplo n.º 3
0
 private Author(NonNullValue <string> name, DateTime dateOfPublish)
 {
     Name          = name;
     DateOfPublish = dateOfPublish;
 }