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); }
public static Maybe2 <T> ToMaybe2 <T>(this T value) { return(Maybe2 <T> .Just(value)); }
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()))); }
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()); }