Beispiel #1
0
        public static bool UpCastCompare(object l, object r, string opName)
        {
            if (!(l is IComparable) || !(r is IComparable))
            {
                return(false);
            }

            var lv = l;
            var rv = r;

            if (lv.GetType() != rv.GetType())
            {
                if (!UpCastEx.UpCast(ref lv, ref rv))
                {
                    return(false);
                }
            }

            switch (opName)
            {
            case ">":
                return(((IComparable)lv).CompareTo(rv) > 0);

            case ">=":
                return(((IComparable)lv).CompareTo(rv) >= 0);

            case "<":
                return(((IComparable)lv).CompareTo(rv) < 0);

            case "<=":
                return(((IComparable)lv).CompareTo(rv) <= 0);
            }

            return(false);
        }
Beispiel #2
0
        private T EvaluateBetween(object l, object v)
        {
            if (l is IComparable left && v is IList list && list.Count == 2)
            {
                if (list[0] is T f && list[1] is T s)
                {
                    return(Monad.Bind(f, s, EvaluateBetweenItems));

                    T EvaluateBetweenItems(object first, object second)
                    {
                        int frCmp = -1;
                        int toCmp = 1;

                        if (first?.GetType() == l?.GetType() || UpCastEx.UpCast(ref first, ref l))
                        {
                            frCmp = left.CompareTo(first);
                        }

                        if (second?.GetType() == l?.GetType() || UpCastEx.UpCast(ref second, ref l))
                        {
                            toCmp = left.CompareTo(second);
                        }

                        return(Monad.Lift(frCmp >= 0 && toCmp <= 0));
                    }
                }
            }
            return(Monad.Lift(false));
        }
Beispiel #3
0
        public override T Evaluate(object l, object r)
        {
            if (l is IComparable cl && r?.GetType() == l?.GetType())
            {
                return(Monad.Lift(cl.CompareTo(r) > 0));
            }

            if (l == null || r == null)
            {
                return(Monad.Unit);
            }

            if (UpCastEx.UpCast(ref l, ref r) && l is IComparable lc)
            {
                return(Monad.Lift(lc.CompareTo(r) > 0));
            }

            return(Monad.Unit);
        }
Beispiel #4
0
        public override T Evaluate(object lv, object rv)
        {
            var l = lv;
            var r = rv;

            if (lv?.GetType() != rv?.GetType() &&
                !UpCastEx.UpCast(ref l, ref r))
            {
                return(Monad.Unit);
            }

            switch (l)
            {
            case double d:
                return(Monad.Lift(d / (double)r));

            case int i:
                return(Monad.Lift(i / (int)r));

            case long lo:
                return(Monad.Lift(lo / (long)r));

            case decimal de:
                return(Monad.Lift(de / (decimal)r));

            case float de:
                return(Monad.Lift(de / (float)r));

            case short de:
                return(Monad.Lift(de / (short)r));

            case byte de:
                return(Monad.Lift(de / (byte)r));
            }
            return(Monad.Unit);
        }
Beispiel #5
0
        public override T Evaluate(object lv, object rv)
        {
            var l = lv;
            var r = rv;

            if (lv?.GetType() != rv?.GetType() &&
                !UpCastEx.UpCast(ref l, ref r))
            {
                return(Monad.Unit);
            }

            switch (l)
            {
            case double d:
                return(Monad.Lift(Math.Pow(d, (double)r)));

            case int i:
                return(Monad.Lift(Math.Pow(i, (int)r)));

            case long lo:
                return(Monad.Lift(Math.Pow(lo, (long)r)));

            case decimal de:
                return(Monad.Lift(Math.Pow((double)de, (double)(decimal)r)));

            case float f:
                return(Monad.Lift(Math.Pow(f, (float)r)));

            case short s:
                return(Monad.Lift(Math.Pow(s, (short)r)));

            case byte b:
                return(Monad.Lift(Math.Pow(b, (byte)r)));
            }
            return(Monad.Unit);
        }
Beispiel #6
0
        public override T Evaluate(object lv, object rv)
        {
            var l = lv;
            var r = rv;

            if (lv is DateTime dt)
            {
                switch (rv)
                {
                case TimeSpan rts:
                    return(Monad.Lift(dt.Add(rts)));

                case int days:
                    return(Monad.Lift(dt.AddDays(days)));

                case double days:
                    return(Monad.Lift(dt.AddDays(days)));
                }
            }

            if (lv is DateTimeOffset dto)
            {
                switch (rv)
                {
                case TimeSpan rts:
                    return(Monad.Lift(dto.Add(rts)));

                case int days:
                    return(Monad.Lift(dto.AddDays(days)));

                case double days:
                    return(Monad.Lift(dto.AddDays(days)));
                }
            }

            if (l is string || r is string)
            {
                return(Monad.Lift("" + l + r));
            }

            if (lv?.GetType() != rv?.GetType() &&
                !UpCastEx.UpCast(ref l, ref r))
            {
                return(Monad.Unit);
            }

            switch (l)
            {
            case double d:
                return(Monad.Lift(d + (double)r));

            case int i:
                return(Monad.Lift(i + (int)r));

            case long lo:
                return(Monad.Lift(lo + (long)r));

            case decimal de:
                return(Monad.Lift(de + (decimal)r));

            case TimeSpan ts:
                return(Monad.Lift(ts + (TimeSpan)r));

            case float de:
                return(Monad.Lift(de + (float)r));

            case short de:
                return(Monad.Lift(de + (short)r));

            case byte de:
                return(Monad.Lift(de + (byte)r));
            }

            return(Monad.Unit);
        }
Beispiel #7
0
        public override T Evaluate(object lv, object rv)
        {
            var l = lv;
            var r = rv;

            if (lv is DateTime dt)
            {
                switch (rv)
                {
                case DateTime rdt:
                    return(Monad.Lift(dt.Subtract(rdt)));

                case TimeSpan rts:
                    return(Monad.Lift(dt.Subtract(rts)));

                case DateTimeOffset rdto:
                    return(Monad.Lift(dt.Subtract(rdto.DateTime)));
                }
            }

            if (lv is DateTimeOffset dto)
            {
                switch (rv)
                {
                case DateTimeOffset rdto:
                    return(Monad.Lift(dto.Subtract(rdto)));

                case DateTime rdt:
                    return(Monad.Lift(dto.Subtract(rdt)));

                case TimeSpan rts:
                    return(Monad.Lift(dto.Subtract(rts)));
                }
            }

            if (lv?.GetType() != rv?.GetType() &&
                !UpCastEx.UpCast(ref l, ref r))
            {
                return(Monad.Unit);
            }

            switch (l)
            {
            case double d:
                return(Monad.Lift(d - (double)r));

            case int i:
                return(Monad.Lift(i - (int)r));

            case long lo:
                return(Monad.Lift(lo - (long)r));

            case decimal de:
                return(Monad.Lift(de - (decimal)r));

            case TimeSpan ts:
                return(Monad.Lift(ts - (TimeSpan)r));

            case float de:
                return(Monad.Lift(de - (float)r));

            case short de:
                return(Monad.Lift(de - (short)r));

            case byte de:
                return(Monad.Lift(de - (byte)r));
            }
            return(Monad.Unit);
        }