private static void AssertDoWithLeftAction(IEither <string, int> either)
        {
            var didWithLeft = false;

            either.DoWithLeft(v => { didWithLeft = true; });
            Assert.That(didWithLeft, Is.True);
        }
Esempio n. 2
0
 public IEither <A, E> Join <C, D, E>(IEither <A, C> other, Func <B, D> getThisKey, Func <C, D> getOtherKey, Func <B, C, E> mapper)
     where C : notnull
     where D : notnull
     where E : notnull
 {
     return(new Either <A, E> .Left(this.data));
 }
 public static IEither <TException, T2> SelectMany <TException, T0, T1, T2>(
     this IEither <TException, T0> either,
     Func <T0, IEither <TException, T1> > selector,
     Func <T0, T1, T2> projector)
 {
     return(either.SelectMany(value0 => selector(value0).Select(value1 => projector(value0, value1))));
 }
 public static T Force <TException, T>(this IEither <TException, T> either)
     where TException : Exception
 {
     return(either.Case(
                exception => { throw exception; },
                value => value));
 }
        private static void AssertDoWithRightAction(IEither <int, string> either)
        {
            var didWithRight = false;

            either.DoWithRight(v => { didWithRight = true; });
            Assert.That(didWithRight, Is.True);
        }
Esempio n. 6
0
        public void UseSimpleQuerySyntax()
        {
            IEither <Guid, int> actual = from s in new Right <Guid, string>("foo")
                                         select s.Length;

            Assert.Equal(new Right <Guid, int>(3), actual);
        }
Esempio n. 7
0
 public static void IsError <TValue>(IEither <TValue, ParseError> result)
 {
     if (result.IsSuccess)
     {
         Assert.Fail("Expected Error, got: " + result.FromSuccess());
     }
 }
 private static void AssertIsValidLeftEither(IEither <string, int> either, string expectedValue)
 {
     Assert.That(either, Is.Not.Null);
     Assert.That(either.IsLeft, Is.True);
     Assert.That(either.Left, Is.EqualTo(expectedValue));
     Assert.That(either.IsRight, Is.False);
     Assert.Throws <EitherException>(() => Console.WriteLine(either.Right));
 }
 public static IEither <TException, T1> SelectMany <TException, T0, T1>(
     this IEither <TException, T0> either,
     Func <T0, IEither <TException, T1> > selector)
 {
     return(either.Case(
                exception => Either.Left <TException, T1>(exception),
                value => selector(value)));
 }
Esempio n. 10
0
 public static IEither <TException, T1> Select <TException, T0, T1>(
     this IEither <TException, T0> either,
     Func <T0, T1> selector)
 {
     return(either.Case(
                left: exception => Either.Left <TException, T1>(exception),
                right: value => Either.Right <TException, T1>(selector(value))));
 }
Esempio n. 11
0
 // Bifunctor
 public static IEither <L1, R1> SelectBoth <L, L1, R, R1>(
     this IEither <L, R> source,
     Func <L, L1> selectLeft,
     Func <R, R1> selectRight)
 {
     return(source.Accept(
                new SelectBothVisitor <L, L1, R, R1>(selectLeft, selectRight)));
 }
        private static void AssertDidNotDoWithRight(IEither <string, int> either)
        {
            var didWithRight = false;

            either.DoWithRight(i => didWithRight = true);
            Assert.That(didWithRight, Is.False);
            Assert.That(either.DoWithRight(i => i, 1337), Is.EqualTo(1337));
        }
Esempio n. 13
0
        public void FindWinnerQuerySyntaxExample()
        {
            IEither <VoteError, bool> isVotePositive =
                from i in FindWinner(1, 2, -3, -1, 2, -1, -1)
                select i > 0;

            Assert.Equal(new Right <VoteError, bool>(false), isVotePositive);
        }
Esempio n. 14
0
 /// <summary>
 /// Applies <paramref name="selector"/> to all the right sides, if all the <see cref="IEither{TL,TR}"/>s are right.
 /// </summary>
 /// <param name="either1">A first <see cref="IEither{TL,TR}"/>.</param>
 /// <param name="either2">A second <see cref="IEither{TL,TR}"/>.</param>
 /// <param name="selector">A selector to apply to the right sides.</param>
 /// <typeparam name="TL">Type of the left sides.</typeparam>
 /// <typeparam name="TR1">Type of the first right side.</typeparam>
 /// <typeparam name="TR2">Type of the second right side.</typeparam>
 /// <typeparam name="TOut">Type of the output.</typeparam>
 /// <returns>A <see cref="Maybe{T}"/> of <typeparamref name="TOut"/>.</returns>
 public static Maybe <TOut> AllRight <TL, TR1, TR2, TOut>(
     this IEither <TL, TR1> either1,
     IEither <TL, TR2> either2,
     Func <TR1, TR2, TOut> selector)
     where TOut : class
 {
     return((either1, either2) switch
     {
         ({ IsRight : true }, { IsRight : true }) => selector(either1.Right, either2.Right),
Esempio n. 15
0
 /// <summary>
 /// Map to a new type.
 /// </summary>
 /// <typeparam name="TLeft">The type of the left.</typeparam>
 /// <typeparam name="TLeftResult">The type of the left result.</typeparam>
 /// <typeparam name="TRight">The type of the right.</typeparam>
 /// <typeparam name="TRightResult">The type of the right result.</typeparam>
 /// <param name="source">The source.</param>
 /// <param name="selectLeft">The select left.</param>
 /// <param name="selectRight">The select right.</param>
 /// <returns></returns>
 public static Either <TLeftResult, TRightResult> Map <TLeft, TLeftResult, TRight, TRightResult>(
     this IEither <TLeft, TRight> source,
     Func <TLeft, TLeftResult> selectLeft,
     Func <TRight, TRightResult> selectRight)
 {
     return(source.Match(
                onLeft: leftValue => new Either <TLeftResult, TRightResult>(selectLeft(leftValue)),
                onRight: rightValue => new Either <TLeftResult, TRightResult>(selectRight(rightValue))));
 }
Esempio n. 16
0
        public static IEither <T, T2> JoinRight <T, T2>(this IEither <T, IEither <T, T2> > either)
        {
            if (either.IsLeft)
            {
                return(new Left <T, T2>(either.GetLeft()));
            }

            return(either.GetRight());
        }
Esempio n. 17
0
        public void SecondFunctorLawHoldsForSelectRight(IEither <string, int> e)
        {
            char f(bool b) => b ? 'T' : 'F';
            bool g(int i) => i % 2 == 0;

            Assert.Equal(
                e.SelectRight(x => f(g(x))),
                e.SelectRight(g).SelectRight(f));
        }
Esempio n. 18
0
        public void SecondFunctorLawHoldsForSelectLeft(IEither <string, int> e)
        {
            bool f(int x) => x % 2 == 0;
            int g(string s) => s.Length;

            Assert.Equal(
                e.SelectLeft(x => f(g(x))),
                e.SelectLeft(g).SelectLeft(f));
        }
Esempio n. 19
0
        public static IEither <T, T2> JoinLeft <T, T2>(this IEither <IEither <T, T2>, T2> either)
        {
            if (either.IsRight)
            {
                return(new Right <T, T2>(either.GetRight()));
            }

            return(either.GetLeft());
        }
Esempio n. 20
0
        public static T3 Fold <T, T2, T3>(this IEither <T, T2> either, Func <T, T3> leftFunc, Func <T2, T3> rightFunc)
        {
            if (either.IsLeft)
            {
                return(leftFunc(either.GetLeft()));
            }

            return(rightFunc(either.GetRight()));
        }
        private static void AssertSwitchedLeft(IEither <string, int> either)
        {
            var switchedLeft  = false;
            var switchedRight = false;

            either.Switch(v => switchedLeft = true, v => switchedRight = true);
            Assert.That(switchedLeft, Is.True);
            Assert.That(switchedRight, Is.False);
        }
Esempio n. 22
0
 public static IEither <L, R1> SelectMany <L, R, R1, U>(
     this IEither <L, R> source,
     Func <R, IEither <L, U> > k,
     Func <R, U, R1> s)
 {
     return(source
            .SelectMany(x => k(x)
                        .SelectMany(y => new Right <L, R1>(s(x, y)))));
 }
Esempio n. 23
0
        public void ConsistencyLawHolds(IEither <string, int> e)
        {
            bool f(string s) => string.IsNullOrWhiteSpace(s);
            DateTime g(int i) => new DateTime(i);

            Assert.Equal(e.SelectBoth(f, g), e.SelectRight(g).SelectLeft(f));
            Assert.Equal(
                e.SelectLeft(f).SelectRight(g),
                e.SelectRight(g).SelectLeft(f));
        }
 private static void ValidateLeftEitherInterface(IEither <string, int> either)
 {
     AssertIsValidLeftEither(either, value);
     AssertDoWithLeftAction(either);
     AssertDoWithLeftFunction(either);
     AssertDidNotDoWithRight(either);
     AssertSwitchedLeft(either);
     AssertMapLeft(either);
     AssertNonAlteringMapLeft(either);
     AssertJoin(either);
 }
Esempio n. 25
0
 public static IEither <TResult, E> Select <S, E, TResult>(this IEither <S, E> either, Func <S, IEither <TResult, E> > func)
 {
     if (either.IsSuccess)
     {
         return(func(either.FromSuccess()));
     }
     else
     {
         return(Either.Error <TResult, E>(either.FromError()));
     }
 }
Esempio n. 26
0
        public void SelectBothCompositionLawHolds(IEither <string, int> e)
        {
            bool f(int x) => x % 2 == 0;
            int g(string s) => s.Length;
            char h(bool b) => b ? 'T' : 'F';
            bool i(int x) => x % 2 == 0;

            Assert.Equal(
                e.SelectBoth(x => f(g(x)), y => h(i(y))),
                e.SelectBoth(g, i).SelectBoth(f, h));
        }
 private static void ValidateRightEitherInterface(IEither <int, string> either)
 {
     AssertIsValidRightEither(either, value);
     AssertDoWithRightAction(either);
     AssertDoWithRightFunction(either);
     AssertDidNotDoWithLeft(either);
     AssertSwitchedRight(either);
     AssertMapRight(either);
     AssertNonAlteringMapRight(either);
     AssertJoin(either);
 }
        public override Task TrainAsync(ClassificationModel classificationModel)
        {
            int numFeatures = classificationModel.FeatureVectors.Count;

            DecisionVariable[] decisionVariables = Enumerable.ToArray(classificationModel.Bands.Select(b => DecisionVariable.Continuous(b.ToString())));

            double[][] input     = new double[numFeatures][];
            int[]      responses = new int[numFeatures];

            for (int featureIndex = 0; featureIndex < classificationModel.FeatureVectors.Count; ++featureIndex)
            {
                var featureVector = classificationModel.FeatureVectors[featureIndex];
                input[featureIndex]     = Array.ConvertAll(featureVector.FeatureVector.BandIntensities, s => (double)s / ushort.MaxValue);
                responses[featureIndex] = featureVector.FeatureClass;
            }

            if (Boosting)
            {
                return(Task.Factory.StartNew(() =>
                {
                    var classifier = new Boost <Weak <DecisionTree> >();

                    var teacher = new AdaBoostM1 <Weak <DecisionTree> >(classifier)
                    {
                        Creation = (weights) =>
                        {
                            var tree = new DecisionTree(decisionVariables, Enum.GetValues(typeof(LandcoverTypeViewModel)).Length);
                            var c45Learning = new C45Learning(tree);
                            c45Learning.Learn(input, responses, weights);
                            return new Weak <DecisionTree>(tree, (s, x) => s.Decide(x));
                        },

                        Iterations = Iterations,
                        Tolerance = 1e-2
                    };

                    teacher.Run(input, responses);
                    _tree = Either.Right <DecisionTree, Boost <Weak <DecisionTree> > >(classifier);
                }));
            }
            else
            {
                return(Task.Factory.StartNew(() =>
                {
                    var tree = new DecisionTree(decisionVariables, Enum.GetValues(typeof(LandcoverTypeViewModel)).Length);
                    C45Learning id3Learning = new C45Learning(tree);
                    id3Learning.Learn(input, responses);

                    _tree = Either.Left <DecisionTree, Boost <Weak <DecisionTree> > >(tree);
                }));
            }
        }
Esempio n. 29
0
        public static IEither <TLeft, TRightTo> Bind <TLeft, TRightFrom, TRightTo>(this IEither <TLeft, TRightFrom> m, Func <TRightFrom, IEither <TLeft, TRightTo> > f)
        {
            switch (m)
            {
            case Left <TLeft, TRightFrom> l:
                return(ToLeft <TLeft, TRightTo>(l.Value));

            case Right <TLeft, TRightFrom> r:
                return(f(r.Value));

            default:
                throw new InvalidCastException("Invalid Either State");
            }
        }
Esempio n. 30
0
        public static bool IsRight <TLeft, TRight>(this IEither <TLeft, TRight> m)
        {
            switch (m)
            {
            case Left <TLeft, TRight> l:
                return(false);

            case Right <TLeft, TRight> r:
                return(true);

            default:
                throw new InvalidCastException("Invalid Either State");
            }
        }