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); }
/// <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); }