Beispiel #1
0
        public static ConsList <T> Cons(T head, ConsList <T> tail)
        {
            var list = new ConsList <T>();

            list.variant = Variant <Unit, Tuple <T, ConsList <T> > > .C2(Tuple.Create(head, tail));

            return(list);
        }
Beispiel #2
0
        public static Option <T> Some(T a)
        {
            var option = new Option <T>();

            option.variant = Variant <Unit, T> .C2(a);

            return(option);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Variant生のまま");
            var foo = new[] {
                Variant <int, string> .C1(4),
                Variant <int, string> .C2("foo")
            };

            foo
            .ForEach(either => either
                     .Match(
                         C1: i => i.ToString(),
                         C2: s => s)
                     .Act(Console.WriteLine));

            //Option<T> = Variant<Unit,T>
            Console.WriteLine("\nOption");
            var hoge = new[] {
                Option <int> .None(),
                Option <int> .Some(10)
            };

            hoge
            .ForEach(opt => opt.
                     Match(
                         None: () => - 1,
                         Some: v => v)
                     .Act(Console.WriteLine));

            //ConsList<T> = Variant<Unit,Tuple<T,ConsList<T>>>
            Console.WriteLine("\nConsList");
            var list = new[] { 2, 3, 5, 8, 12 }.ToConsList();

            list
            .Match(
                Nil: () => 0,
                Cons: (head, tail) => head)
            .Act(Console.WriteLine);

            //Sum
            list
            .Catamorphism(
                nil: () => 0,
                cons: (h, t) => h + t)
            .Act(Console.WriteLine);

            Console.ReadLine();
        }
Beispiel #4
0
 public static Either <TLeft, TRight> Right(TRight right)
 {
     return(new Either <TLeft, TRight>(Variant <TLeft, TRight> .C2(right)));
 }
Beispiel #5
0
 internal static ConsList <T> Cons(T head, ConsList <T> tail)
 {
     return(new ConsList <T>(Variant <Product, Product <T, ConsList <T> > > .C2(Product.Create(head, tail))));
 }
Beispiel #6
0
 public static Option <T> None()
 {
     return(new Option <T>(Variant <T, Product> .C2(Product.Create())));
 }