예제 #1
0
        public static TMonad Sequence <TMonad, TA>(IEnumerable <IMonad <TA> > ms)
            where TMonad : IMonad <IEnumerable <TA> >
        {
            var monadAdapter = MonadAdapterRegistry.Get(typeof(TMonad));
            var z            = monadAdapter.Return(MonadHelpers.Nil <TA>());

            return((TMonad)ms.FoldRight(
                       z, (m, mtick) => monadAdapter.Bind(
                           m, x => monadAdapter.Bind(
                               mtick, xs => monadAdapter.Return(MonadHelpers.Cons(x, xs))))));
        }
예제 #2
0
        public static TMonad FilterM <TMonad, TA>(Func <TA, IMonad <bool> > p, IEnumerable <TA> @as)
            where TMonad : IMonad <IEnumerable <TA> >
        {
            var monadAdapter = MonadAdapterRegistry.Get(typeof(TMonad));

            return((TMonad)@as.HeadAndTail().Match(
                       tuple =>
            {
                var x = tuple.Item1;
                var xs = tuple.Item2;
                return monadAdapter.Bind(
                    p(x), flg => monadAdapter.Bind(
                        FilterM <TMonad, TA>(p, xs),
                        ys => monadAdapter.Return(flg ? MonadHelpers.Cons(x, ys) : ys)));
            },
                       () => monadAdapter.Return(MonadHelpers.Nil <TA>())));
        }
예제 #3
0
파일: Maybe.cs 프로젝트: taylorjg/Monads
 public IEnumerable <TA> ToEnumerable()
 {
     return(Match(MonadHelpers.One, () => MonadHelpers.Nil <TA>()));
 }