示例#1
0
        public IMonad <C> Com <B, C>(IMonad <Func <A, B, IMonad <C> > > functionMonad, IMonad <B> mOther)
        {
            IMonad <C> result = new Nothing <C>();

            if (!isNothing && !(mOther is Nothing <B>))         // other is no maybe and this is not nothing.
            {
                result = null;
                //resultMaybe = functionMonad.Return()(aValue, mOther.Return());
                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.Concat(fResult);
                            }
                        }
                    }
                }
                if (result == null)
                {
                    result = new Nothing <C>();
                }
            }

            return(result);
        }
示例#2
0
        public IMonad <C> Com <B, C>(Func <A, B, IMonad <C> > function, IMonad <B> mOther)
        {
            IMonad <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.Concat(fResult);
                        }
                    }
                }
                if (result == null)
                {
                    result = new Nothing <C>();
                }
            }
            return(result);
        }
示例#3
0
        public IMonad <B> App <B>(IMonad <Func <A, IMonad <B> > > functionMonad)
        {
            IMonad <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.Concat(fResult);
                            }
                        }
                    }
                }
                if (result == null) // If some function returned null
                {
                    result = new Nothing <B>();
                }
            }

            return(result);
        }