public void BindFull() { Func <Func <Maybe <int> > > source = () => { int n = 0; return(() => n < 3 ? (Maybe <int>) new Maybe <int> .Some(n ++) : new Maybe <int> .None()); }; Func <string, int, Func <Func <Maybe <string> > > > repeat = (s, n) => () => { int k = 0; return(() => k++ < n ? (Maybe <string>) new Maybe <string> .Some(s) : new Maybe <string> .None()); }; var res = source.Bind(x => repeat(((char)('a' + x)).ToString(), x), (x, s) => s + x); for (int i = 0; i < 2; i++) { var e = res(); Assert.IsTrue(e().Value == "b1", "Expected b1 (" + i + ")"); Assert.IsTrue(e().Value == "c2", "Expected c2 (" + i + ")"); Assert.IsTrue(e().Value == "c2", "Expected c2 again (" + i + ")"); Assert.IsTrue(e() is Maybe <string> .None, "Expected end (" + i + ")"); } }
public void BindEmptyOuter() { Func <Func <Maybe <int> > > source = () => () => new Maybe <int> .None(); Func <Func <Maybe <string> > > some = () => () => new Maybe <string> .Some(""); var res = source.Bind(_ => some, (x, s) => s + x); for (int i = 0; i < 2; i++) { var e = res(); Assert.IsTrue(e() is Maybe <string> .None, "Not empty (" + i + ")"); } }
public void BindEmptyInner() { Func <Func <Maybe <int> > > source = () => { int n = 0; return(() => n == 0 ? (Maybe <int>) new Maybe <int> .Some(n ++) : new Maybe <int> .None()); }; Func <Func <Maybe <string> > > none = () => () => new Maybe <string> .None(); var res = source.Bind(_ => none, (x, s) => s + x); for (int i = 0; i < 2; i++) { var e = res(); Assert.IsTrue(e() is Maybe <string> .None, "Not empty (" + i + ")"); } }