예제 #1
0
        /// <summary>
        /// Implements the LINQ select many operation for the <see cref="IMaybe{T}"/> monad.
        /// </summary>
        /// <param name="value">
        /// The <see cref="IMaybe{T}"/> value to transform.
        /// </param>
        /// <param name="transformation">
        /// The transformation function, which takes a value of type <typeparamref name="T1"/> contained in the monad and transforms it
        /// into a new monad of type <see cref="IMaybe{T2}"/>.
        /// </param>
        /// <param name="collation">
        /// The collation function, which takes a value of type <typeparamref name="T1"/> and a value of type <typeparamref name="T2"/>
        /// and combines them into a value of type <typeparamref name="TResult"/>.
        /// </param>
        /// <typeparam name="T1">
        /// The type of the value contained in the monad to transform.
        /// </typeparam>
        /// <typeparam name="T2">
        /// The type of the value contained in the monad resulting from the transformation.
        /// </typeparam>
        /// <typeparam name="TResult">
        /// The type of the value contained in the monad resulting from collation.
        /// </typeparam>
        /// <returns>
        /// The <see cref="IMaybe{TResult}"/> which results from transforming <paramref name="value"/> using <paramref name="transformation"/>
        /// and then collating using <paramref name="collation"/>.
        /// </returns>
        public static IMaybe <TResult> SelectMany <T1, T2, TResult>(this IMaybe <T1> value, Func <T1, IMaybe <T2> > transformation, Func <T1, T2, TResult> collation)
        {
            return(value.Bind(firstValue =>
            {
                IMaybe <T2> result = transformation(firstValue);
                if (result == null)
                {
                    throw new InvalidOperationException("The transformation function may not return null.");
                }

                return result.Bind(secondValue => Maybe.Just(collation(firstValue, secondValue)));
            }));
        }
예제 #2
0
 public static IMaybe <B> Select <A, B>(this IMaybe <A> a, Func <A, IMaybe <B> > func)
 {
     return(a.Bind(func));
 }
예제 #3
0
 public static IMaybe <C> SelectMany <A, B, C>(this IMaybe <A> a, Func <A, IMaybe <B> > func, Func <A, B, C> select)
 {
     return(a.Bind(aval =>
                   func(aval).Bind(bval =>
                                   select(aval, bval).ToMaybe())));
 }
예제 #4
0
 public static IMaybe <V> SelectMany <T, U, V>(this IMaybe <T> m, Func <T, IMaybe <U> > func, Func <T, U, V> select)
 {
     return(m.Bind(a => func(a).Bind(b => select(a, b).ToMaybe())));
 }
예제 #5
0
 public static IMaybe <TB> Map <TA, TB>(this IMaybe <TA> a, Func <TA, TB> selector)
 => a.Bind(v => selector(v).ToMaybe());
예제 #6
0
 public static IMaybe <TR> SelectMany <TA, TB, TR>(this IMaybe <TA> a,
                                                   Func <TA, IMaybe <TB> > selector,
                                                   Func <TA, TB, TR> resultSelector)
 => a.Bind(v => selector(v).Bind(b => resultSelector(v, b).ToMaybe()));
예제 #7
0
 public static IMaybe <TB> SelectMany <TA, TB>(this IMaybe <TA> a,
                                               Func <TA, IMaybe <TB> > selector)
 => a.Bind(selector);
예제 #8
0
파일: Maybe.cs 프로젝트: Infarh/MathCore
 public static IMaybe <TC> SelectMany <TA, TB, TC>(this IMaybe <TA> ma, Func <TA, IMaybe <TB> > maybeSelector, Func <TA, TB, TC> resultSelector) => ma.Bind(a => maybeSelector(a).Select(b => resultSelector(a, b)));
예제 #9
0
파일: Maybe.cs 프로젝트: Infarh/MathCore
 public static IMaybe <T> Where <T>(this IMaybe <T> maybe, Func <T, bool> predicate) => maybe.Bind(x => predicate(x) ? Return(x) : Nothing <T>());
예제 #10
0
파일: Maybe.cs 프로젝트: Infarh/MathCore
 public static IMaybe <TResult> Select <TArg, TResult>(this IMaybe <TArg> maybe, Func <TArg, TResult> func) => maybe.Bind(value => Return(func(value)));
예제 #11
0
 /// <summary>
 /// TODO: (DG) fill...
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <typeparam name="TResult">The type of the result.</typeparam>
 /// <param name="instance">The maybe <typeparamref name="T"/> instance.</param>
 /// <param name="selectFunc">The selectFunc function.</param>
 /// <returns><see cref="IMaybe{TResult}"/>.</returns>
 public static IMaybe <TResult> Select <T, TResult>(this IMaybe <T> instance, Func <T, IMaybe <TResult> > selectFunc)
     where T : IEnumerable
     where TResult : IEnumerable
 {
     return(instance.Bind(selectFunc));
 }
예제 #12
0
 /// <summary>
 ///     Map an <see cref="IMaybe{T}"/> value using a mapping function if a value exists, or propogate the nothing value if it does not.
 /// </summary>
 /// <param name="a">The <see cref="IMaybe{T}"/> value to transform.</param>
 /// <param name="f">The function to transform the <see cref="IMaybe{T}"/> input value <paramref name="a"/>.</param>
 /// <typeparam name="T1">The type of the maybe input value.</typeparam>
 /// <typeparam name="TResult">The type of the maybe result value.</typeparam>
 /// <returns>
 ///     The <see cref="IMaybe{TResult}"/> which results from transforming <paramref name="a"/> using <paramref name="f"/>.
 /// </returns>
 public static IMaybe <TResult> Map <T1, TResult>(this IMaybe <T1> a, Func <T1, TResult> f)
 {
     return(a.Bind(v => Maybe.Just(f(v))));
 }