Exemple #1
0
        public static void LinqFuncEitherTest()
        {
            var cxt = new EitherCxt <string>();

            var init        = 5;
            var computation =
                from x in MaybeT.Lift(cxt.FromRight(45))
                from y in MaybeT.Lift(cxt.FromRight(90))
                from _ in MaybeT.HoistEither(cxt, Maybe.JustIf(init > 0, () => new Unit()))
                from z in MaybeT.Lift(cxt.FromRight(100))
                select x + y + z;

            var value = computation.Run;

            Assert.True(value.IsRight);
            Assert.True(value.Right().HasValue);
            Assert.Equal(45 + 90 + 100, value.Right().Value());

            init        = 0;
            computation =
                from x in MaybeT.Lift(cxt.FromRight(45))
                from y in MaybeT.Lift(cxt.FromRight(90))
                from _ in MaybeT.HoistEither(cxt, Maybe.JustIf(init > 0, () => new Unit()))
                from z in MaybeT.Lift(cxt.FromRight(100))
                select x + y + z;

            value = computation.Run;

            Assert.False(value.IsLeft);
        }
Exemple #2
0
        HoistEither <TLeft, TValue>(
            EitherCxt <TLeft> _cxt,
            Maybe <TValue> value)
        {
            var m = new MaybeT <Either <TLeft, TValue>, Either <TLeft, Maybe <TValue> >, Maybe <TValue>, TValue>();

            return((MaybeT <Either <TLeft, TValue>, Either <TLeft, Maybe <TValue> >, Maybe <TValue>, TValue>)m.Hoist(value));
        }