Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 3
0
 public override Func <CacheEntry <K, V>, Monad <C> > Kleisli <B, C>(Func <CacheEntry <K, V>, Monad <B> > fAtB, Func <B, Monad <C> > fBtC)
 {
     return((a) =>
     {
         Monad <B> result = null;
         Lock.EnterWriteLock();
         try
         {
             foreach (var element in cacheDict.Values)
             {
                 if (result == null)
                 {
                     result = fAtB(element);
                 }
                 else
                 {
                     result.Concatenate(fAtB(element));
                 }
             }
         }
         finally
         {
             Lock.ExitWriteLock();
         }
         return result.Bind(fBtC);
     });
 }
Exemplo n.º 4
0
        public override Monad <C> Com <B, C>(Monad <Func <CacheEntry <K, V>, B, Monad <C> > > functionMonad, Monad <B> mOther)
        {
            Monad <C> result = null;

            Lock.EnterWriteLock();
            try
            {
                foreach (var element in cacheDict.Values)
                {
                    foreach (var func in functionMonad)
                    {
                        foreach (var elementB in mOther)
                        {
                            if (result == null)
                            {
                                result = func(element, elementB);
                            }
                            else
                            {
                                result = result.Concatenate(func(element, elementB));
                            }
                        }
                    }
                }
            }
            finally
            {
                Lock.ExitWriteLock();
            }
            return(result);
        }
Exemplo n.º 5
0
        public override Monad <C> Com <B, C>(Monad <Func <A, B, Monad <C> > > functionMonad, Monad <B> mOther)
        {
            Monad <C> resultMonad = null;

            foreach (var function in functionMonad)
            {
                if (function != null)
                {
                    foreach (var value in mOther)
                    {
                        if (resultMonad == null)
                        {
                            resultMonad = function(idValue, value);
                        }
                        else
                        {
                            resultMonad = resultMonad.Concatenate(function(idValue, value));
                        }
                    }
                }
            }
            if (resultMonad == null)
            {
                resultMonad = new Identity <C>();
            }

            return(resultMonad);
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
0
 /// <summary>
 /// If this is not nothing, then the result monad is a new Maybe<A> with the value inside the other monad.
 /// </summary>
 /// <param name="otherMonad">The other monad.</param>
 /// <returns>The new monad.</returns>
 public override Monad <A> Concatenate(Monad <A> otherMonad)
 {
     if (!isNothing)
     {
         aValue = otherMonad.Return();
     }
     return(this);
 }
Exemplo n.º 8
0
 public override Monad <A> Visit <B>(Action <A, B> action, Monad <B> mOther)
 {
     foreach (var element in mOther)
     {
         action(idValue, element);
     }
     return(this);
 }
Exemplo n.º 9
0
        public static ListMonad <T> ToListMonad <T>(this Monad <T> value)
        {
            ListMonad <T> result = new ListMonad <T>();

            foreach (T element in value)
            {
                result.Add(element);
            }
            return(result);
        }
Exemplo n.º 10
0
 public override Monad <A> Visit <B>(Action <A, B> action, Monad <B> mOther)
 {
     if (this is Just <A> && action != null && mOther != null)
     {
         foreach (var element in mOther)
         {
             action(aValue, element);
         }
     }
     return(this);
 }
Exemplo n.º 11
0
 public override Monad <A> Visit <B>(Action <A, B> action, Monad <B> mOther)
 {
     foreach (var aElement in list)
     {
         foreach (var otherElement in mOther)
         {
             action(aElement, otherElement);
         }
     }
     return(this);
 }
Exemplo n.º 12
0
        public virtual Monad <A> Where(Func <A, bool> predicate)
        {
            Monad <A> result = (Monad <A>) this.GetType().GetConstructor(new Type[] { }).Invoke(null);

            foreach (A element in this)
            {
                if (predicate(element))
                {
                    result.Append(element);
                }
            }
            return(result);
        }
Exemplo n.º 13
0
        public override Monad <B> App <B>(Monad <Func <A, B> > functionMonad)
        {
            Identity <B> resultIdentity = new Identity <B>();

            foreach (var function in functionMonad)
            {
                if (function != null)
                {
                    resultIdentity = function(idValue);
                }
            }
            return(resultIdentity);
        }
Exemplo n.º 14
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);
        }
Exemplo n.º 15
0
        public override Monad <C> Com <B, C>(Func <A, B, Monad <C> > function, Monad <B> mOther)
        {
            ListMonad <C> result = new ListMonad <C>();

            foreach (A a in list)
            {
                foreach (B b in mOther)
                {
                    result.Concatenate(function(a, b));
                }
            }

            return(result);
        }
Exemplo n.º 16
0
        public override Monad <C> Com <B, C>(Func <A, B, C> function, Monad <B> mOther)
        {
            ListMonad <C> resultListMonad = new ListMonad <C>();

            foreach (A elementThis in list)
            {
                foreach (B elementOther in mOther)
                {
                    resultListMonad.Append(function(elementThis, elementOther));
                }
            }

            return(resultListMonad);
        }
Exemplo n.º 17
0
        public override Monad <C> Com <B, C>(Func <A, B, C> function, Monad <B> mOther)
        {
            Identity <C> resultMonad = new Identity <C>();

            foreach (var value in mOther)
            {
                resultMonad = function(idValue, value);
            }

            if (resultMonad == null)
            {
                resultMonad = new Identity <C>();
            }

            return(resultMonad);
        }
Exemplo n.º 18
0
 public override Monad <CacheEntry <K, V> > Concatenate(Monad <CacheEntry <K, V> > otherMonad)
 {
     Lock.EnterWriteLock();
     try
     {
         foreach (var entry in otherMonad)
         {
             cacheDict[entry.Key] = entry;
         }
     }
     finally
     {
         Lock.ExitWriteLock();
     }
     return(this);
 }
Exemplo n.º 19
0
        public override Monad <A> Concatenate(Monad <A> otherMonad)
        {
            if (this.Equals(otherMonad))
            {
                return(this);
            }
            else
            {
                foreach (A element in otherMonad)
                {
                    list.Add(element);
                }
            }

            return(this);
        }
Exemplo n.º 20
0
        public override Monad <C> Com <B, C>(Monad <Func <A, B, C> > functionMonad, Monad <B> mOther)
        {
            ListMonad <C> resultListMonad = new ListMonad <C>();

            foreach (Func <A, B, C> f in functionMonad)
            {
                foreach (A a in list)
                {
                    foreach (B b in mOther)
                    {
                        resultListMonad.Append(f(a, b));
                    }
                }
            }

            return(resultListMonad);
        }
Exemplo n.º 21
0
        public override Monad <B> Bind <B>(Func <A, int, Monad <B> > func)
        {
            Monad <B> result = null;

            for (int i = 0; i < list.Count; i++)
            {
                if (result == null)
                {
                    result = func(list[i], i);
                }
                else
                {
                    result.Concatenate(func(list[i], i));
                }
            }

            return(result);
        }
Exemplo n.º 22
0
        /// <summary>
        /// Map each function inside the given Monad over each element in this ListMonad,
        /// and put all the values inside the result monads into a new ListMonad of the type B.
        /// </summary>
        /// <typeparam name="B">The type inside the result ListMonad.</typeparam>
        /// <param name="functionMonad">The monad that has functions inside.</param>
        /// <returns>The new ListMonad of type B.</returns>
        public override Monad <B> App <B>(Monad <Func <A, Monad <B> > > functionMonad)
        {
            ListMonad <B> result = new ListMonad <B>();

            foreach (Func <A, Monad <B> > function in functionMonad)
            {
                // function can be null, for example when the functionMonad is a Maybe with Nothing<Func<A, IMonad<B>> then default(Func<A, IMonad<B>>) returns null
                // we could check for IMonad as Maybe and then check for isNothing, but then ListMonad have to "know" Maybe, i dont like that.
                if (function != null)
                {
                    foreach (A element in list)     // calculate function result for each element in this ListFunctor<T>
                    {
                        result.Concatenate(function(element));
                    }
                }
            }
            return(result);
        }
Exemplo n.º 23
0
        public override Monad <B> Bind <B>(Func <A, Monad <B> > func)
        {
            Monad <B> result = null;

            foreach (A element in list)
            {
                if (result == null)
                {
                    result = func(element);
                }
                else
                {
                    result.Concatenate(func(element));
                }
            }

            return(result);
        }
Exemplo n.º 24
0
        public override Func <A, Monad <C> > Kleisli <B, C>(Func <A, Monad <B> > fAtB, Func <B, Monad <C> > fBtC)
        {
            return((a) =>
            {
                Monad <B> result = null;
                foreach (A element in list)
                {
                    if (result == null)
                    {
                        result = fAtB(element);
                    }
                    else
                    {
                        result.Concatenate(fAtB(element));
                    }
                }

                return result.Bind(fBtC);
            });
        }
Exemplo n.º 25
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);
        }
Exemplo n.º 26
0
        public override Monad <B> App <B>(Monad <Func <CacheEntry <K, V>, B> > functionMonad)
        {
            ListMonad <B> result = new ListMonad <B>();

            Lock.EnterWriteLock();
            try
            {
                foreach (var element in cacheDict.Values)
                {
                    foreach (var func in functionMonad)
                    {
                        result.Add(func(element));
                    }
                }
            }
            finally
            {
                Lock.ExitWriteLock();
            }
            return(result);
        }
Exemplo n.º 27
0
        public override Monad <C> Com <B, C>(Monad <Func <CacheEntry <K, V>, B, C> > functionMonad, Monad <B> mOther)
        {
            ListMonad <C> result = new ListMonad <C>();

            Lock.EnterWriteLock();
            try
            {
                foreach (var element in cacheDict.Values)
                {
                    foreach (var func in functionMonad)
                    {
                        foreach (var elementB in mOther)
                        {
                            result.Add(func(element, elementB));
                        }
                    }
                }
            }
            finally
            {
                Lock.ExitWriteLock();
            }
            return(result);
        }
Exemplo n.º 28
0
        public override Monad <B> App <B>(Monad <Func <A, Monad <B> > > functionMonad)
        {
            Monad <B> resultMonad = null;

            foreach (var function in functionMonad)
            {
                if (function != null)
                {
                    if (resultMonad == null)
                    {
                        resultMonad = function(idValue);
                    }
                    else
                    {
                        resultMonad = resultMonad.Concatenate(function(idValue));
                    }
                }
            }
            if (resultMonad == null)
            {
                resultMonad = new Identity <B>();
            }
            return(resultMonad);
        }
Exemplo n.º 29
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);
        }
Exemplo n.º 30
0
 public static Maybe <T> ToMaybe <T>(this Monad <T> value)
 {
     return(value.Return());
 }