|
| 1 | +package com.jnape.palatable.lambda.monad.transformer.builtin; |
| 2 | + |
| 3 | +import com.jnape.palatable.lambda.adt.Either; |
| 4 | +import com.jnape.palatable.lambda.functor.Applicative; |
| 5 | +import com.jnape.palatable.lambda.functor.builtin.Compose; |
| 6 | +import com.jnape.palatable.lambda.functor.builtin.Lazy; |
| 7 | +import com.jnape.palatable.lambda.monad.Monad; |
| 8 | +import com.jnape.palatable.lambda.monad.transformer.MonadT; |
| 9 | + |
| 10 | +import java.util.Objects; |
| 11 | +import java.util.function.Function; |
| 12 | + |
| 13 | +import static com.jnape.palatable.lambda.adt.Either.left; |
| 14 | +import static com.jnape.palatable.lambda.adt.Either.right; |
| 15 | + |
| 16 | +/** |
| 17 | + * A {@link MonadT monad transformer} for {@link Either}. |
| 18 | + * |
| 19 | + * @param <M> the outer {@link Monad} |
| 20 | + * @param <L> the left type |
| 21 | + * @param <R> the right type |
| 22 | + */ |
| 23 | +public final class EitherT<M extends Monad<?, M>, L, R> implements MonadT<M, Either<L, ?>, R> { |
| 24 | + |
| 25 | + private final Monad<Either<L, R>, M> melr; |
| 26 | + |
| 27 | + private EitherT(Monad<Either<L, R>, M> melr) { |
| 28 | + this.melr = melr; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritDoc} |
| 33 | + */ |
| 34 | + @Override |
| 35 | + public <GA extends Monad<R, Either<L, ?>>, FGA extends Monad<GA, M>> FGA run() { |
| 36 | + return melr.<GA>fmap(Either::coerce).coerce(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * {@inheritDoc} |
| 41 | + */ |
| 42 | + @Override |
| 43 | + public <R2> EitherT<M, L, R2> flatMap(Function<? super R, ? extends Monad<R2, MonadT<M, Either<L, ?>, ?>>> f) { |
| 44 | + return eitherT(melr.flatMap(lr -> lr.match(l -> melr.pure(left(l)), |
| 45 | + r -> f.apply(r).<EitherT<M, L, R2>>coerce().run()))); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * {@inheritDoc} |
| 50 | + */ |
| 51 | + @Override |
| 52 | + public <R2> EitherT<M, L, R2> pure(R2 r2) { |
| 53 | + return eitherT(melr.pure(right(r2))); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * {@inheritDoc} |
| 58 | + */ |
| 59 | + @Override |
| 60 | + public <R2> EitherT<M, L, R2> fmap(Function<? super R, ? extends R2> fn) { |
| 61 | + return MonadT.super.<R2>fmap(fn).coerce(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * {@inheritDoc} |
| 66 | + */ |
| 67 | + @Override |
| 68 | + public <R2> EitherT<M, L, R2> zip( |
| 69 | + Applicative<Function<? super R, ? extends R2>, MonadT<M, Either<L, ?>, ?>> appFn) { |
| 70 | + return MonadT.super.zip(appFn).coerce(); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * {@inheritDoc} |
| 75 | + */ |
| 76 | + @Override |
| 77 | + public <R2> Lazy<EitherT<M, L, R2>> lazyZip( |
| 78 | + Lazy<? extends Applicative<Function<? super R, ? extends R2>, MonadT<M, Either<L, ?>, ?>>> lazyAppFn) { |
| 79 | + return new Compose<>(melr) |
| 80 | + .lazyZip(lazyAppFn.fmap(maybeT -> new Compose<>( |
| 81 | + maybeT.<EitherT<M, L, Function<? super R, ? extends R2>>>coerce() |
| 82 | + .<Either<L, Function<? super R, ? extends R2>>, |
| 83 | + Monad<Either<L, Function<? super R, ? extends R2>>, M>>run()))) |
| 84 | + .fmap(compose -> eitherT(compose.getCompose())); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * {@inheritDoc} |
| 89 | + */ |
| 90 | + @Override |
| 91 | + public <R2> EitherT<M, L, R2> discardL(Applicative<R2, MonadT<M, Either<L, ?>, ?>> appB) { |
| 92 | + return MonadT.super.discardL(appB).coerce(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * {@inheritDoc} |
| 97 | + */ |
| 98 | + @Override |
| 99 | + public <B> EitherT<M, L, R> discardR(Applicative<B, MonadT<M, Either<L, ?>, ?>> appB) { |
| 100 | + return MonadT.super.discardR(appB).coerce(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public boolean equals(Object other) { |
| 105 | + return other instanceof EitherT<?, ?, ?> && Objects.equals(melr, ((EitherT<?, ?, ?>) other).melr); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public int hashCode() { |
| 110 | + return Objects.hash(melr); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public String toString() { |
| 115 | + return "EitherT{melr=" + melr + '}'; |
| 116 | + } |
| 117 | + |
| 118 | + public static <M extends Monad<?, M>, L, R> EitherT<M, L, R> eitherT(Monad<Either<L, R>, M> melr) { |
| 119 | + return new EitherT<>(melr); |
| 120 | + } |
| 121 | +} |
0 commit comments