// ReSharper restore FunctionRecursiveOnAllPaths

        public static TMonad Void <TMonad, TA>(IMonad <TA> m)
            where TMonad : IMonad <Unit>
        {
            var monadAdapter = MonadAdapterRegistry.Get(typeof(TMonad));

            return((TMonad)monadAdapter.BindIgnoringLeft(m, monadAdapter.Return(new Unit())));
        }
        // ReSharper disable FunctionRecursiveOnAllPaths
        public static TMonad Forever <TMonad, TA, TB>(IMonad <TA> m)
            where TMonad : IMonad <TB>
        {
            var monadAdapter = MonadAdapterRegistry.Get(typeof(TMonad));

            return((TMonad)monadAdapter.BindIgnoringLeft(m, Forever <TMonad, TA, TB>(m)));
        }
Exemple #3
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);
        }
 public ToCSharpNotEqOp(
     IMonad <string> monad,
     IExpression <string> left,
     IExpression <string> right)
     : base(monad, left, right)
 {
 }
Exemple #5
0
        IMonad <T> IMonadExpressionBuilder.Combine <T> (IMonad <T> m, IMonad <T> n)
        {
            var enumerableMonad1 = (EnumerableMonad <T>)m;
            var enumerableMonad2 = (EnumerableMonad <T>)n;

            return(new EnumerableMonad <T> (enumerableMonad1.Concat(enumerableMonad2)));
        }
Exemple #6
0
 public LessOrEqual(
     IMonad <T> monad,
     IExpression <T> left,
     IExpression <T> right)
     : base(monad, left, right)
 {
 }
Exemple #7
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);
        }
Exemple #8
0
 /// <summary>
 /// A monadic <see cref="CollectionExtensions.Unfold{TSeed, TResult}(TSeed, Func{TSeed, Maybe{ValueTuple{TSeed, TResult}}})"/> which generates a sequence each time the <paramref name="seed"/>-value evaluates to <see cref="Maybe.Just{T}(T)"/>.<br />
 /// Equivalent to:
 /// <code>
 /// while (seed().TryGetValue(_, y)) { yield return y; }
 /// </code>
 /// </summary>
 /// <typeparam name="TSource">The type of the seed/generated elements.</typeparam>
 /// <param name="seed">The monadic seed-value.</param>
 public static IMonad <IEnumerable <TSource> > UnfoldM <TSource>(this IMonad <Maybe <TSource> > seed)
 {
     IMonad <IEnumerable <TSource> > go()
     {
         var q =
             from x in seed
             from result in x.TryGetValue(default, out var y)
Exemple #9
0
 public ToJsTernary(IMonad <string> monad, IExpression <string> condition, IExpression <string> trueExpr, IExpression <string> falseExpr)
 {
     this.Monad     = monad;
     this.condition = condition;
     this.trueExpr  = trueExpr;
     this.falseExpr = falseExpr;
 }
Exemple #10
0
 public ComparableExpr(
     IMonad <T> monad,
     IExpression <T> left,
     IExpression <T> right)
     : base(monad, left, right)
 {
 }
Exemple #11
0
 /// <summary></summary>
 public static IMonad <T> Do <T>(this IMonad <T> monad, Action <T> action)
 => monad.Bind(
     v =>
 {
     action(v);
     return(monad);
 });
Exemple #12
0
 public GreaterThan(
     IMonad <T> monad,
     IExpression <T> left,
     IExpression <T> right)
     : base(monad, left, right)
 {
 }
Exemple #13
0
 public ToJsMatchesOp(
     IMonad <string> monad,
     IExpression <string> left,
     IExpression <string> right)
     : base(monad, left, right)
 {
 }
Exemple #14
0
 public ToJsValueOfOp(string opName, IMonad <string> monad, IExpression <string> left, IExpression <string> right)
 {
     this.opName = opName;
     Monad       = monad;
     this.left   = left;
     this.right  = right;
 }
        public static IMonad <TB> Select <TA, TB>(this IMonad <TA> ma, Func <TA, TB> f)
        {
            var monadAdapter = ma.GetMonadAdapter();

            return(monadAdapter.Bind(
                       ma, a => monadAdapter.Return(f(a))));
        }
Exemple #16
0
 public UnaryMinusExpr(
     IMonad <TMonad> monad,
     IExpression <TMonad> expr)
 {
     Monad     = monad;
     this.expr = expr;
 }
Exemple #17
0
 public NotExpr(
     IMonad <TMonad> monad,
     IExpression <TMonad> expr)
 {
     Monad     = monad;
     this.expr = expr;
 }
Exemple #18
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);
        }
Exemple #19
0
 public ToCSharpInOp(
     IMonad <string> monad,
     IExpression <string> l,
     IExpression <string> r)
     : base(monad, l, r)
 {
 }
Exemple #20
0
 public ToJsCodeInOp(
     IMonad <string> monad,
     IExpression <string> l,
     IExpression <string> r)
     : base(monad, l, r)
 {
 }
Exemple #21
0
 public EqExpr(
     IMonad <T> monad,
     IExpression <T> left,
     IExpression <T> right)
     : base(monad, left, right)
 {
 }
Exemple #22
0
 public ExpressionFactory(
     IMonad <T> monad,
     IFunctions <T> functions = null)
 {
     Monad     = monad;
     Functions = functions ?? new DefaultFunctions <T>();
 }
Exemple #23
0
        public static IPackage MergeRefl <T>(Func <Type, Type> functor, IMonad monad, IVariant <uint> variant)
        {
            switch (variant.Tag)
            {
            case 0U:
            {
                var content = variant.Content.Cast <T>();

                return(monad.Wrap <T>(content));
            }

            case 1U:
            {
                return(variant.Content);
            }

            default:
            {
                variant = new Variant <uint>(variant.Tag - 1, variant.Content);

                var generic = functor(typeof(T));

                var merged = (IPackage)typeof(OptionUtility)
                             .GetMethod("MergeRefl")
                             .MakeGenericMethod(generic)
                             .Invoke(null, new object[] { functor, monad, variant });

                return(monad.Unwrap <T>(merged));
            }
            }
        }
Exemple #24
0
 public MaybeDivide(
     IMonad <Maybe <object> > monad,
     IExpression <Maybe <object> > left,
     IExpression <Maybe <object> > right)
     : base(monad, left, right)
 {
 }
 public static TMonad LiftM <TMonad, TA, TB>(Func <TA, TB> f, IMonad <TA> ma)
     where TMonad : IMonad <TB>
 {
     return((TMonad)
            from a in ma
            select f(a));
 }
 public ListExpressionFactory(
     IMonad <List <object> > monad,
     IFunctions <List <object> > functions = null)
     : base(
         monad,
         functions ?? new DefaultFunctions <List <object> >())
 {
 }
Exemple #27
0
 public ObjectMember(IMonad <TMonad> monad,
                     IExpression <TMonad> obj,
                     IExpression <TMonad> member)
 {
     this.Monad  = monad;
     this.obj    = obj;
     this.member = member;
 }
        public static IMonad <TC> SelectMany <TA, TB, TC>(this IMonad <TA> ma, Func <TA, IMonad <TB> > f1, Func <TA, TB, TC> f2)
        {
            var monadAdapter = ma.GetMonadAdapter();

            return(monadAdapter.Bind(
                       ma, a => monadAdapter.Bind(
                           f1(a), b => monadAdapter.Return(f2(a, b)))));
        }
 public static TMonad LiftM2 <TMonad, TA, TB, TC>(Func <TA, TB, TC> f, IMonad <TA> ma, IMonad <TB> mb)
     where TMonad : IMonad <TC>
 {
     return((TMonad)
            from a in ma
            from b in mb
            select f(a, b));
 }
Exemple #30
0
 public MaybeNotEqExpr(
     IMonad <TMonad> monad,
     IExpression <TMonad> left,
     IExpression <TMonad> right)
 {
     Monad = monad;
     Left  = left;
     Right = right;
 }