Example #1
0
        public void CreateLeft_EitherHasLeftValue()
        {
            var result = Either <string, bool> .CreateLeft("value");

            Assert.IsFalse(result.HasRight);
            Assert.AreEqual("value", result.LeftValue);
        }
        public void SwitchTest()
        {
            var left  = Either.CreateLeft <int, string>(1);
            var right = Either.CreateRight <int, string>("s");

            var assertCount = 0;

            left.Switch(i =>
            {
                Assert.AreEqual(1, i);
                assertCount++;
            }, Fail);
            Assert.AreEqual(1, assertCount);


            right.Switch(Fail, i =>
            {
                Assert.AreEqual("s", i);
                assertCount++;
            });
            Assert.AreEqual(2, assertCount);


            var result1 = left.Switch(i => i * 2, Fail <string, int>);

            Assert.AreEqual(2, result1);

            var result2 = right.Switch(Fail <int, int>, s => 5);

            Assert.AreEqual(5, result2);
        }
Example #3
0
        public static void Main(string[] args)
        {
            var worldOrError = Parser.Default.ParseArguments <CommandOptions>(args).MapResult(
                opts => Either.CreateLeft <Maybe <GameVariables>, string>(MapArgs(opts)),
                err => Either.CreateRight <Maybe <GameVariables>, string>(string.Join(", ", err)));

            worldOrError.Switch(StartGame, Console.Write);
        }
Example #4
0
        public void CreateLeft_EitherDoesNotHaveRightValue()
        {
            var result = Either <string, bool> .CreateLeft("value");

            Assert.IsFalse(result.HasRight);

            var temp = result.RightValue;
        }
Example #5
0
        public void Apply_WithReturn_LeftFuncRun()
        {
            var result = Either <int, int> .CreateLeft(1);

            int branchOutput = -1;

            branchOutput = result.Apply(l => 1, r => 2);

            Assert.AreEqual(1, branchOutput);
        }
Example #6
0
        public void Apply_NoReturn_LeftActionRun()
        {
            var result = Either <int, int> .CreateLeft(1);

            bool leftBranchTaken  = false;
            bool rightBranchTaken = false;

            result.Apply(l => { leftBranchTaken = true; }, r => { rightBranchTaken = false; });

            Assert.IsTrue(leftBranchTaken);
            Assert.IsFalse(rightBranchTaken);
        }
Example #7
0
            protected override IDisposable SubscribeCore(IObserver <TResult> observer)
            {
                var leftSubscription  = new SingleAssignmentDisposable();
                var rightSubscription = new SingleAssignmentDisposable();

                var combiner = combinerSelector(observer, leftSubscription, rightSubscription);
                var gate     = new object();

                leftSubscription.Disposable  = leftSource.Materialize().Select(x => Either <Notification <TLeft>, Notification <TRight> > .CreateLeft(x)).Synchronize(gate).Subscribe(combiner);
                rightSubscription.Disposable = rightSource.Materialize().Select(x => Either <Notification <TLeft>, Notification <TRight> > .CreateRight(x)).Synchronize(gate).Subscribe(combiner);

                return(StableCompositeDisposable.Create(leftSubscription, rightSubscription));
            }
Example #8
0
        public void Rights_RightValuesCollated()
        {
            var result = Either.Rights <string, int>(new List <Either <string, int> >()
            {
                Either <string, int> .CreateRight(1),
                Either <string, int> .CreateLeft("2"),
                Either <string, int> .CreateRight(3),
            });

            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Count());
            Assert.IsTrue(result.Any(i => i == 1));
            Assert.IsTrue(result.Any(i => i == 3));
        }
Example #9
0
        public void Lefts_LeftValuesCollated()
        {
            var result = Either.Lefts <string, int>(new List <Either <string, int> >()
            {
                Either <string, int> .CreateLeft("1"),
                Either <string, int> .CreateRight(2),
                Either <string, int> .CreateLeft("3"),
            });

            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Count());
            Assert.IsTrue(result.Any(i => i == "1"));
            Assert.IsTrue(result.Any(i => i == "3"));
        }
Example #10
0
        private static IObservable <TResult> Combine <TLeft, TRight, TResult>(IObservable <TLeft> leftSource, IObservable <TRight> rightSource, Func <IObserver <TResult>, IDisposable, IDisposable, IObserver <Either <Notification <TLeft>, Notification <TRight> > > > combinerSelector)
        {
            return(new AnonymousObservable <TResult>(observer =>
            {
                var leftSubscription = new SingleAssignmentDisposable();
                var rightSubscription = new SingleAssignmentDisposable();

                var combiner = combinerSelector(observer, leftSubscription, rightSubscription);
                var gate = new object();

                leftSubscription.Disposable = leftSource.Materialize().Select(x => Either <Notification <TLeft>, Notification <TRight> > .CreateLeft(x)).Synchronize(gate).Subscribe(combiner);
                rightSubscription.Disposable = rightSource.Materialize().Select(x => Either <Notification <TLeft>, Notification <TRight> > .CreateRight(x)).Synchronize(gate).Subscribe(combiner);

                return StableCompositeDisposable.Create(leftSubscription, rightSubscription);
            }));
        }
Example #11
0
        public void Partition_ValuesCollated()
        {
            var result = Either.Partition <string, int>(new List <Either <string, int> >()
            {
                Either <string, int> .CreateRight(1),
                Either <string, int> .CreateLeft("2"),
                Either <string, int> .CreateRight(3),
            });

            Assert.IsNotNull(result);

            Assert.AreEqual(1, result.Item1.Count());
            Assert.IsTrue(result.Item1.Any(i => i == "2"));

            Assert.AreEqual(2, result.Item2.Count());
            Assert.IsTrue(result.Item2.Any(i => i == 1));
            Assert.IsTrue(result.Item2.Any(i => i == 3));
        }
 public void CreatesByStaticMethod()
 => ValidateLeftEitherInterface(Either <string, int> .CreateLeft(value));
Example #13
0
 /// <summary>
 /// Left injection
 /// </summary>
 public static Either <left, t> Left <left>(left value) => Either <left, t> .CreateLeft(value);
Example #14
0
        public void Apply_NoReturn_NullLeftActionThrows()
        {
            var result = Either <string, int> .CreateLeft("value");

            result.Apply(null, (r) => { });
        }
Example #15
0
 public void CreateLeft_NullValueThrows()
 {
     var result = Either <string, bool> .CreateLeft(null);
 }
Example #16
0
        public void HasRight_FalseWhenCreatedWithLeft()
        {
            var result = Either <int, int> .CreateLeft(1);

            Assert.IsFalse(result.HasRight);
        }
Example #17
0
        public void RightValue_ThrowsWhenCreatedAsLeft()
        {
            var result = Either <int, int> .CreateLeft(1);

            var temp = result.RightValue;
        }
Example #18
0
        public void LeftValue_DoesNotThrowWhenCreatedAsLeft()
        {
            var result = Either <int, int> .CreateLeft(1);

            Assert.AreEqual(1, result.LeftValue);
        }