Esempio n. 1
0
        /// <seealso cref="MonadOr.Lift{T1, T2, T3, T4, T5, TResult}"/>
        public static MonadOr <TResult> ZipWith <T1, T2, T3, T4, T5, TResult>(
            this MonadOr <T1> @this,
            MonadOr <T2> second,
            MonadOr <T3> third,
            MonadOr <T4> fourth,
            MonadOr <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))));
        }
Esempio n. 2
0
 public static MonadOr <TResult> ContinueWith <TSource, TResult>(
     this MonadOr <TSource> @this,
     MonadOr <TResult> other)
 {
     Require.NotNull(@this, nameof(@this));
     return(@this.Bind(_ => other));
 }
Esempio n. 3
0
 public static MonadOr <TSource> Where <TSource>(
     this MonadOr <TSource> @this,
     Func <TSource, bool> predicate)
 {
     Require.NotNull(@this, nameof(@this));
     Require.NotNull(predicate, nameof(predicate));
     return(@this.Bind(val => predicate(val) ? MonadOr <TSource> .η(val) : MonadOr <TSource> .None));
 }
Esempio n. 4
0
 public static MonadOr <TResult> Select <TSource, TResult>(
     this MonadOr <TSource> @this,
     Func <TSource, TResult> selector)
 {
     Require.NotNull(@this, nameof(@this));
     Require.NotNull(selector, nameof(selector));
     return(@this.Bind(val => MonadOr <TResult> .η(selector(val))));
 }
Esempio n. 5
0
 /// <seealso cref="Apply{TSource, TResult}(MonadOr{Func{TSource, TResult}}, MonadOr{TSource})" />
 public static MonadOr <TResult> Gather <TSource, TResult>(
     this MonadOr <TSource> @this,
     MonadOr <Func <TSource, TResult> > applicative)
 {
     Require.NotNull(@this, nameof(@this));
     Require.NotNull(applicative, nameof(applicative));
     return(applicative.Bind(func => @this.Select(func)));
 }
Esempio n. 6
0
 // Bind() with automatic resource management.
 public static MonadOr <TResult> Using <TSource, TResult>(
     this MonadOr <TSource> @this,
     Func <TSource, MonadOr <TResult> > binder)
     where TSource : IDisposable
 {
     Require.NotNull(@this, nameof(@this));
     Require.NotNull(binder, nameof(binder));
     return(@this.Bind(val => { using (val) { return binder(val); } }));
 }
Esempio n. 7
0
        // Generalizes both Bind() and ZipWith<T1, T2, TResult>().
        public static MonadOr <TResult> SelectMany <TSource, TMiddle, TResult>(
            this MonadOr <TSource> @this,
            Func <TSource, MonadOr <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))));
        }
Esempio n. 8
0
        /// <seealso cref="MonadOr.Lift{T1, T2, TResult}"/>
        public static MonadOr <TResult> ZipWith <T1, T2, TResult>(
            this MonadOr <T1> @this,
            MonadOr <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))));
        }
Esempio n. 9
0
        /// <seealso cref="MonadOr.Lift{T1, T2, T3, TResult}"/>
        public static MonadOr <TResult> ZipWith <T1, T2, T3, TResult>(
            this MonadOr <T1> @this,
            MonadOr <T2> second,
            MonadOr <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))));
        }
Esempio n. 10
0
 internal static MonadOr <T> μ(MonadOr <MonadOr <T> > square)
 => square.Bind(Stubs <MonadOr <T> > .Ident);