Example #1
0
        public override Monad <C> Com <B, C>(Monad <Func <A, B, Monad <C> > > functionMonad, Monad <B> mOther)
        {
            Monad <C> result = new Nothing <C>();

            if (!isNothing && !(mOther is Nothing <B>))         // other is no maybe and this is not nothing.
            {
                result = null;
                foreach (var function in functionMonad)
                {
                    foreach (var otherValue in mOther)
                    {
                        if (result == null)       // Make result monad the monad type of the function result
                        {
                            result = function(aValue, otherValue);
                        }
                        else
                        {
                            var fResult = function(aValue, otherValue);
                            if (!(fResult is Nothing <B>))
                            {
                                result = result.Concatenate(fResult);
                            }
                        }
                    }
                }
                if (result == null)
                {
                    result = new Nothing <C>();
                }
            }

            return(result);
        }
Example #2
0
        public override Monad <C> Com <B, C>(Func <A, B, Monad <C> > function, Monad <B> mOther)
        {
            Monad <C> result = new Nothing <C>();  // New Nothing<B> maybe

            if (!isNothing && !(mOther is Nothing <B>))
            {
                result = null;
                foreach (var otherValue in mOther)
                {
                    if (result == null)
                    {
                        result = function(aValue, otherValue);
                    }
                    else
                    {
                        var fResult = function(aValue, otherValue);
                        if (!(fResult is Nothing <B>))
                        {
                            result = result.Concatenate(fResult);
                        }
                    }
                }
                if (result == null)
                {
                    result = new Nothing <C>();
                }
            }
            return(result);
        }
Example #3
0
        public override Monad <B> App <B>(Monad <Func <A, Monad <B> > > functionMonad)
        {
            Monad <B> result = new Nothing <B>();

            if (this is Just <A> && functionMonad != null)
            {
                result = null;
                //result = functionMonad.Return()(aValue).Return();
                foreach (var function in functionMonad)
                {
                    if (function != null)
                    {
                        if (result == null)  // if first time or first time (and second...) was Nothing
                        {
                            result = function(aValue);
                        }
                        else
                        {
                            var fResult = function(aValue);
                            if (!(fResult is Nothing <B>))        // skip if result is nothing
                            {
                                result = result.Concatenate(fResult);
                            }
                        }
                    }
                }
                if (result == null) // If some function returned null
                {
                    result = new Nothing <B>();
                }
            }

            return(result);
        }
Example #4
0
        public override Monad <C> Com <B, C>(Func <A, B, C> function, Monad <B> mOther)
        {
            Monad <C> resultMonad = new Nothing <C>();  // New Nothing<B> maybe

            if (!isNothing && !(mOther is Nothing <B>))
            {
                foreach (var otherValue in mOther)
                {
                    resultMonad = new Just <C>(function(aValue, otherValue));
                }
            }
            return(resultMonad);
        }
Example #5
0
        public override Monad <B> Fmap <B>(Func <A, int, B> function)
        {
            Maybe <B> resMaybe = new Nothing <B>();

            if (this is Just <A> && function != null)
            {
                resMaybe = function(aValue, 0);        // implicit operator makes it automatically a Just or a Nothing depending on the function result.
            }
            if (resMaybe == null)
            {
                resMaybe = new Nothing <B>();
            }
            return(resMaybe);
        }
Example #6
0
        public override Monad <B> App <B>(Monad <Func <A, B> > functionMonad)
        {
            Maybe <B> result = new Nothing <B>();

            if (this is Just <A> && functionMonad != null)
            {
                foreach (var function in functionMonad)
                {
                    if (function != null)
                    {
                        result = new Just <B>(functionMonad.Return()(aValue));
                    }
                }
                if (result == null)
                {
                    result = new Nothing <B>();
                }
            }
            return(result);
        }
Example #7
0
        public override Monad <C> Com <B, C>(Monad <Func <A, B, C> > functionMonad, Monad <B> mOther)
        {
            Maybe <C> result = new Nothing <C>();

            if (!isNothing && !(mOther is Nothing <B>))         // other no nothing monad.
            {
                foreach (var function in functionMonad)
                {
                    if (function != null)
                    {
                        foreach (var otherValue in mOther)
                        {
                            result = function(aValue, otherValue);
                        }
                    }
                }
                if (result == null)
                {
                    result = new Nothing <C>();
                }
            }

            return(result);
        }