public void Just() { var a = new object(); IMaybe <object> justA = Maybe.Just(a); Assert.That(justA.Exists, Is.True); Assert.That(a, Is.SameAs(justA.Value)); Assert.That(Maybe.Of(a), Is.EqualTo(justA)); Assert.That(justA.Equals(null), Is.False); Assert.That(Maybe.Just(a), Is.EqualTo(justA)); Assert.That(Maybe.Just("b"), Is.Not.EqualTo(justA)); Assert.That(Maybe.Just <object>("b"), Is.Not.EqualTo(justA)); Assert.That(Maybe.Nothing <string>(), Is.Not.EqualTo(justA)); Assert.True(justA.Equals(justA)); Assert.That(Maybe.Just("a").ToString(), Is.EqualTo("Just(a)")); Assert.That(justA.GetHashCode(), Is.EqualTo(justA.Value.GetHashCode())); Assert.That(justA.GetOrElse(new object()), Is.SameAs(a)); bool called = false; justA.GetOrElseGet(() => { called = true; return(new object()); }); Assert.That(called, Is.False); }
public void Equality_IMaybeOfT() { IMaybe <int> a = Maybe.From(123); IMaybe <int> b = Maybe.From(123); IMaybe <int> c = Maybe.From(123); IMaybe <int> d = Maybe.From(456); IMaybe <string> e = Maybe.From("pretzels"); IMaybe <int> f = Maybe.Nothing <int>(); IMaybe <string> g = Maybe.Nothing <string>(); // reflexive Assert.IsTrue(a.Equals(a)); // symmetric Assert.IsTrue(a.Equals(b)); Assert.IsTrue(b.Equals(a)); // reflexive Assert.IsTrue(a.Equals(b)); Assert.IsTrue(b.Equals(c)); Assert.IsTrue(a.Equals(c)); // inequality Assert.IsFalse(a.Equals(d)); Assert.IsFalse(a.Equals(e)); Assert.IsFalse(a.Equals(f)); Assert.IsFalse(a.Equals(g)); // others Assert.IsFalse(a.Equals(null)); Assert.IsFalse(a.Equals(new object())); }
public void Equality_IMaybe() { IMaybe a = Maybe.From(typeof(int), 123); IMaybe b = Maybe.From(typeof(int), 123); IMaybe c = Maybe.From(typeof(int), 123); IMaybe d = Maybe.From(typeof(int), 456); IMaybe e = Maybe.From(typeof(string), "pretzels"); IMaybe f = Maybe.Nothing(typeof(int)); IMaybe g = Maybe.Nothing(typeof(string)); // reflexive Assert.IsTrue(a.Equals(a)); // symmetric Assert.IsTrue(a.Equals(b)); Assert.IsTrue(b.Equals(a)); // reflexive Assert.IsTrue(a.Equals(b)); Assert.IsTrue(b.Equals(c)); Assert.IsTrue(a.Equals(c)); // inequality Assert.IsFalse(a.Equals(d)); Assert.IsFalse(a.Equals(e)); Assert.IsFalse(a.Equals(f)); Assert.IsFalse(a.Equals(g)); // others Assert.IsFalse(a.Equals(null)); Assert.IsFalse(a.Equals(new object())); }
private static Stream <T> Calm <T>(Stream <T> sA, Lazy <IMaybe <T> > init) { return(sA.CollectLazy(init, (a, lastA) => { IMaybe <T> ma = Maybe.Just(a); return ma.Equals(lastA) ? (ReturnValue: Maybe.Nothing <T>(), State: lastA) : (ReturnValue: ma, State: ma); }).FilterMaybe());
private static Stream <T> Calm <T>(Stream <T> sA, Lazy <IMaybe <T> > init) { return(sA.CollectLazy(init, (a, lastA) => { IMaybe <T> ma = Maybe.Just(a); return ma.Equals(lastA) ? Tuple.Create(Maybe.Nothing <T>(), lastA) : Tuple.Create(ma, ma); }).FilterMaybe()); }