Untitled

 avatar
unknown
plain_text
3 years ago
1.1 kB
4
Indexable
private static <S> Monad<__.µ<µ, S>> monad() {
    return new MonadAbstract<__.µ<µ, S>>() {

        @Override
        public <A, B> _<__.µ<µ, S>, B> map(F1<A, B> fn, _<__.µ<µ, S>, A> nestedA) {
            return narrow(nestedA).rightMap(fn);
        }

        @Override
        public <A> _<__.µ<µ, S>, A> pure(A a) {
            return Right(a);
        }

        @Override
        public <A, B> _<__.µ<µ, S>, B> ap(_<__.µ<µ, S>, F1<A, B>> fn, _<__.µ<µ, S>, A> nestedA) {
            Either<S, F1<A, B>> eitherFn = narrow(fn);
            if (eitherFn.isLeft())
                return Left(eitherFn.getLeft());
            Either<S, A> eitherA = narrow(nestedA);
            return eitherA.isRight() ? pure(eitherFn.getRight().$(eitherA.getRight())) : Either.<S, B>Left(eitherA.getLeft());
        }

        @Override
        public <A, B> _<__.µ<µ, S>, B> bind(_<__.µ<µ, S>, A> a, F1<A, _<__.µ<µ, S>, B>> fn) {
            Either<S, A> either = narrow(a);
            return either.isRight() ? fn.$(either.getRight()) : Either.<S, B>Left(either.getLeft());
        }
    };
}
Editor is loading...