Пример #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
 public IEnumerable <TA> ToEnumerable()
 {
     return(Match(MonadHelpers.One, () => MonadHelpers.Nil <TA>()));
 }