Пример #1
0
        public static void Maybe2Composition()
        {
            Func <int, Maybe2 <int> > add2 = x => (x != 0) ? Maybe2 <int> .Just(x + 2) : Maybe2 <int> .Nothing();

            Func <int, Maybe2 <int> > multiplyBy2 = x => (x != 0) ? Maybe2 <int> .Just(x * 2) : Maybe2 <int> .Nothing();

            Func <int, Maybe2 <int> > add2MultiplyBy2 = x => add2(x).Bind(multiplyBy2);
            var result = add2MultiplyBy2(3);

            Console.WriteLine("result = {0}", result);
        }
Пример #2
0
 public static Maybe2 <T> ToMaybe2 <T>(this T value)
 {
     return(Maybe2 <T> .Just(value));
 }
Пример #3
0
 public static Maybe2 <C> SelectMany <A, B, C>(this Maybe2 <A> a, Func <A, Maybe2 <B> > func, Func <A, B, C> select)
 {
     return(a.Bind(aval => func(aval).Bind(bval => select(aval, bval).ToMaybe2())));
 }
Пример #4
0
 public static Maybe2 <B> Bind <A, B>(this Maybe2 <A> a, Func <A, Maybe2 <B> > func)
 {
     return(a.HasValue ? func(a.Value) : Maybe2 <B> .Nothing());
 }