Exemplo n.º 1
0
        /// <summary>
        /// Allows values from multiple maybe instances to be combined in the select portion of a LINQ query.
        /// </summary>
        /// <typeparam name="T">The type of the first maybe instance.</typeparam>
        /// <typeparam name="TOther">The type of the second maybe instance.</typeparam>
        /// <typeparam name="TResult">The type of the result of combining the two types.</typeparam>
        /// <param name="maybe">A maybe instance to combine.</param>
        /// <param name="converter">A function that takes the value of <paramref name="maybe"/> (when non-empty) and returns a Maybe{TOther} result.</param>
        /// <param name="combiner">Function that combines values of T and TOther to return a TResult.</param>
        /// <returns>A Maybe{TResult} containing the result of the <paramref name="combiner"/> function. Will be Mabye{T}.Not</returns>
        /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="converter"/> or <paramref name="combiner"/> is null.</exception>
        public static Maybe <TResult> SelectMany <T, TOther, TResult>(this Maybe <T> maybe, Func <T, Maybe <TOther> > converter, Func <T, TOther, TResult> combiner)
        {
            if (converter == null)
            {
                throw new ArgumentNullException(nameof(converter));
            }
            if (combiner == null)
            {
                throw new ArgumentNullException(nameof(combiner));
            }

            return(maybe.Bind <TResult>
                   (
                       (a) => converter(a).Bind <TResult>
                       (
                           (b) => (Maybe <TResult>)combiner(a, b)
                       )
                   ));
        }
Exemplo n.º 2
0
        internal static Maybe <TAccumulate> FoldImpl <TSource, TAccumulate>(
            this IEnumerable <TSource> source,
            TAccumulate seed,
            Func <TAccumulate, TSource, Maybe <TAccumulate> > accumulator)
        {
            Debug.Assert(source != null);
            Debug.Assert(accumulator != null);

            Maybe <TAccumulate> retval = Maybe <TAccumulate> .η(seed);

            using (var iter = source.GetEnumerator())
            {
                while (iter.MoveNext())
                {
                    retval = retval.Bind(val => accumulator(val, iter.Current));
                }
            }

            return(retval);
        }
        // Safe Version
        public static Maybe <List <IpsDataSet> > MGetEachDataSetWith(this Maybe <string> ipspath, Maybe <string> klapath)
        {
            var klaDataDict = klapath
                              .Bind(x => Just(GetDirectoryName(x)))
                              .Bind(x => Just(GetFiles(x)))
                              .Match(
                () => null,
                x => GetKlaData(x));

            var toAllPathList = ToAllPathListWith.Apply(klaDataDict);

            return(ipspath
                   .Lift(GetDirectoryName)
                   .Bind(MGetAllDirs)
                   .Bind(MGetAllFileNames)
                   .Bind(MGetIpsDataSetPaths)                       //[ SCalss , ResPath , RfltPath ]
                   .Bind(x => x.Select(toAllPathList))
                   .Lift(ToIpsDataSet)
                   .ToList());
        }
Exemplo n.º 4
0
        internal static Maybe <TSource> ReduceImpl <TSource>(
            this IEnumerable <TSource> source,
            Func <TSource, TSource, Maybe <TSource> > accumulator)
        {
            Debug.Assert(source != null);
            Debug.Assert(accumulator != null);

            using (var iter = source.GetEnumerator())
            {
                if (!iter.MoveNext())
                {
                    throw new InvalidOperationException("Source sequence was empty.");
                }

                Maybe <TSource> retval = Maybe <TSource> .η(iter.Current);

                while (iter.MoveNext())
                {
                    retval = retval.Bind(val => accumulator(val, iter.Current));
                }

                return(retval);
            }
        }
Exemplo n.º 5
0
Arquivo: Bind.cs Projeto: jedahu/Jib
 public static Maybe <T> Join <T>(this Maybe <Maybe <T> > maybe)
 {
     return(maybe.Bind(a => a));
 }
Exemplo n.º 6
0
 public static Maybe <TC> SelectMany <TA, TB, TC>(this Maybe <TA> ma, Func <TA, Maybe <TB> > f, Func <TA, TB, TC> select)
 => ma.Bind(a => f(a).Map(b => select(a, b)));
Exemplo n.º 7
0
 public static Maybe <TResult> SelectMany <TSource, TResult>(this Maybe <TSource> m, Func <TSource, TResult> f)
 {
     return(m.Bind(x => f(x).ToMaybe()));
 }
Exemplo n.º 8
0
 void RightIdentityHolds(Maybe <object> m) => Assert.Equal(
     m,
     m.Bind(Just));
Exemplo n.º 9
0
 public static Maybe <B> Select <A, B>(this Maybe <A> a, Func <A, Maybe <B> > func)
 {
     return(a.Bind(func));
 }
Exemplo n.º 10
0
        private static string GetStringStacked(Maybe <string> input)
        {
            var stackedString = input.Bind(StringTransformers.StackString);

            return(GetStringIfDefined(stackedString));
        }
Exemplo n.º 11
0
 public void TwoClauseLINQExpr(Maybe <string> maybeA, Maybe <string> maybeB)
 => Assert.Equal(
     from a in maybeA
     from b in maybeB
     select a + b,
     maybeA.Bind(a => maybeB.Map(b => a + b)));
Exemplo n.º 12
0
 public static Func <Maybe <TU>, Maybe <TResult> > Bind <TU, TIntermediate, TResult>([NotNull] Func <TU, Maybe <TIntermediate> > f, [NotNull] Func <Maybe <TIntermediate>, Maybe <TResult> > g)
 {
     return(Combine(Maybe <TU> .Bind(f), g));
 }
Exemplo n.º 13
0
 public static Maybe <R> ApplyInTermsOfBind <T, R>(this Maybe <Func <T, R> > func, Maybe <T> arg)
 => arg.Bind(t => func.Bind <Func <T, R>, R>(f => f(t)));
Exemplo n.º 14
0
 public static Maybe <TResult> SelectMany <TSource, TMaybe, TResult>(this Maybe <TSource> m, Func <TSource, Maybe <TMaybe> > f, Func <TSource, TMaybe, TResult> g)
 {
     return(m.Bind(x => f(x).Bind(y => g(x, y).ToMaybe())));
 }
Exemplo n.º 15
0
 public static Maybe <B> Bind <A, B>(Maybe <A> ma, Func <A, Maybe <B> > amb)
 {
     return(ma.Bind(amb));
 }
Exemplo n.º 16
0
 public static Maybe <C> SelectMany <A, B, C>(this Maybe <A> a, Func <A, Maybe <B> > func, Func <A, B, C> select)
 {
     return(a.Bind(aval =>
                   func(aval).Bind(bval =>
                                   select(aval, bval).ToMaybe())));
 }
Exemplo n.º 17
0
 public static Maybe <B> SelectMany <A, B>(this Maybe <A> maybe, Func <A, Maybe <B> > selectManyFunc)
 {
     return(maybe.Bind(selectManyFunc));
 }
Exemplo n.º 18
0
 public static Maybe <B> Ap <A, B>(this Maybe <Func <A, B> > f, Maybe <A> arg)
 {
     return(f.Bind(f0 => arg.Map(f0)));
 }
Exemplo n.º 19
0
 public static Maybe <C> SelectMany <A, B, C>(this Maybe <A> maybe, Func <A, Maybe <B> > selectManyFunc, Func <A, B, C> combineFunc)
 {
     return(maybe.Bind(a => selectManyFunc(a).Bind(b => Maybe.Just(combineFunc(a, b)))));
 }
Exemplo n.º 20
0
 void AssociativityHolds(Maybe <string> m) => Assert.Equal(
     m.Bind(Double.Parse).Bind(safeSqrt),
     m.Bind(x => Double.Parse(x).Bind(safeSqrt)));
 public static Maybe <TValue> TryGetValue <TKey, TValue>(this Maybe <IDictionary <TKey, TValue> > dictionary, Maybe <TKey> key)
 {
     return(dictionary.Bind(x => key.Bind(x.TryGetValue)));
 }