Пример #1
0
        /// <seealso cref="Monad.Lift{T1, T2, T3, T4, T5, TResult}"/>
        public static Monad <TResult> ZipWith <T1, T2, T3, T4, T5, TResult>(
            this Monad <T1> @this,
            Monad <T2> second,
            Monad <T3> third,
            Monad <T4> fourth,
            Monad <T5> fifth,
            Func <T1, T2, T3, T4, T5, TResult> zipper)
        {
            Require.NotNull(@this, nameof(@this));
            Require.NotNull(second, nameof(second));
            Require.NotNull(third, nameof(third));
            Require.NotNull(fourth, nameof(fourth));
            Require.NotNull(fifth, nameof(fifth));
            Require.NotNull(zipper, nameof(zipper));

            // > return @this.Bind(
            // >     arg1 => second.Bind(
            // >         arg2 => third.Bind(
            // >             arg3 => fourth.Bind(
            // >                 arg4 => fifth.Select(
            // >                     arg5 => zipper(arg1, arg2, arg3, arg4, arg5))))));
            return(@this.Bind(
                       arg1 => second.ZipWith(
                           third,
                           fourth,
                           fifth,
                           (arg2, arg3, arg4, arg5) => zipper(arg1, arg2, arg3, arg4, arg5))));
        }
Пример #2
0
 public static Monad <TSource> PassBy <TSource, TOther>(
     this Monad <TSource> @this,
     Monad <TOther> other)
 {
     Require.NotNull(@this, nameof(@this));
     return(@this.ZipWith(other, (arg, _) => arg));
 }
Пример #3
0
 public static Monad <TResult> ContinueWith <TSource, TResult>(
     this Monad <TSource> @this,
     Monad <TResult> other)
 {
     Require.NotNull(@this, nameof(@this));
     return(@this.Bind(_ => other));
 }
Пример #4
0
 public static Monad <TResult> ReplaceBy <TSource, TResult>(
     this Monad <TSource> @this,
     TResult value)
 {
     Require.NotNull(@this, nameof(@this));
     return(@this.Select(_ => value));
 }
Пример #5
0
 /// <seealso cref="Gather{TSource, TResult}" />
 public static Monad <TResult> Apply <TSource, TResult>(
     this Monad <Func <TSource, TResult> > @this,
     Monad <TSource> value)
 {
     Require.NotNull(value, nameof(value));
     return(value.Gather(@this));
 }
Пример #6
0
 public static Monad <TResult> Select <TSource, TResult>(
     this Monad <TSource> @this,
     Func <TSource, TResult> selector)
 {
     Require.NotNull(@this, nameof(@this));
     Require.NotNull(selector, nameof(selector));
     return(@this.Bind(val => Monad <TResult> .η(selector(val))));
 }
Пример #7
0
 /// <seealso cref="Apply{TSource, TResult}(Monad{Func{TSource, TResult}}, Monad{TSource})" />
 public static Monad <TResult> Gather <TSource, TResult>(
     this Monad <TSource> @this,
     Monad <Func <TSource, TResult> > applicative)
 {
     Require.NotNull(@this, nameof(@this));
     Require.NotNull(applicative, nameof(applicative));
     return(applicative.Bind(func => @this.Select(func)));
 }
Пример #8
0
 // Select() with automatic resource management.
 public static Monad <TResult> Using <TSource, TResult>(
     this Monad <TSource> @this,
     Func <TSource, TResult> selector)
     where TSource : IDisposable
 {
     Require.NotNull(@this, nameof(@this));
     Require.NotNull(selector, nameof(selector));
     return(@this.Select(val => { using (val) { return selector(val); } }));
 }
Пример #9
0
 // Bind() with automatic resource management.
 public static Monad <TResult> Using <TSource, TResult>(
     this Monad <TSource> @this,
     Func <TSource, Monad <TResult> > binder)
     where TSource : IDisposable
 {
     Require.NotNull(@this, nameof(@this));
     Require.NotNull(binder, nameof(binder));
     return(@this.Bind(val => { using (val) { return binder(val); } }));
 }
Пример #10
0
        // Generalizes both Bind() and ZipWith<T1, T2, TResult>().
        public static Monad <TResult> SelectMany <TSource, TMiddle, TResult>(
            this Monad <TSource> @this,
            Func <TSource, Monad <TMiddle> > selector,
            Func <TSource, TMiddle, TResult> resultSelector)
        {
            Require.NotNull(@this, nameof(@this));
            Require.NotNull(selector, nameof(selector));
            Require.NotNull(resultSelector, nameof(resultSelector));

            return(@this.Bind(
                       val => selector(val).Select(
                           middle => resultSelector(val, middle))));
        }
Пример #11
0
        /// <seealso cref="Monad.Lift{T1, T2, TResult}"/>
        public static Monad <TResult> ZipWith <T1, T2, TResult>(
            this Monad <T1> @this,
            Monad <T2> second,
            Func <T1, T2, TResult> zipper)
        {
            Require.NotNull(@this, nameof(@this));
            Require.NotNull(second, nameof(second));
            Require.NotNull(zipper, nameof(zipper));

            return(@this.Bind(
                       arg1 => second.Select(
                           arg2 => zipper(arg1, arg2))));
        }
Пример #12
0
        /// <seealso cref="Monad.Lift{T1, T2, T3, TResult}"/>
        public static Monad <TResult> ZipWith <T1, T2, T3, TResult>(
            this Monad <T1> @this,
            Monad <T2> second,
            Monad <T3> third,
            Func <T1, T2, T3, TResult> zipper)
        {
            Require.NotNull(@this, nameof(@this));
            Require.NotNull(second, nameof(second));
            Require.NotNull(third, nameof(third));
            Require.NotNull(zipper, nameof(zipper));

            // This is the same as:
            // > return @this.Bind(
            // >     arg1 => second.Bind(
            // >        arg2 => third.Select(
            // >            arg3 => zipper(arg1, arg2, arg3))));
            // but faster if ZipWith is locally shadowed.
            return(@this.Bind(
                       arg1 => second.ZipWith(
                           third, (arg2, arg3) => zipper(arg1, arg2, arg3))));
        }
Пример #13
0
 internal static Monad <T> μ(Monad <Monad <T> > square)
 => square.Bind(Stubs <Monad <T> > .Ident);
Пример #14
0
 public static Monad <unit> Skip <TSource>(this Monad <TSource> @this)
 {
     Require.NotNull(@this, nameof(@this));
     return(@this.ContinueWith(Monad.Unit));
 }
Пример #15
0
 /// <summary>
 /// Removes one level of structure, projecting its bound value into the outer level.
 /// </summary>
 public static Monad <T> Flatten <T>(this Monad <Monad <T> > @this)
 => Monad <T> .μ(@this);