|
| 1 | +package com.jnape.palatable.lambda.monad.transformer.builtin; |
| 2 | + |
| 3 | +import com.jnape.palatable.lambda.functor.Applicative; |
| 4 | +import com.jnape.palatable.lambda.functor.Functor; |
| 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.functor.builtin.Lazy.lazy; |
| 14 | + |
| 15 | +/** |
| 16 | + * A {@link MonadT monad transformer} for {@link Lazy}. Note that {@link LazyT#flatMap(Function)} must force its value. |
| 17 | + * |
| 18 | + * @param <M> the outer {@link Monad} |
| 19 | + * @param <A> the carrier type |
| 20 | + */ |
| 21 | +public class LazyT<M extends Monad<?, M>, A> implements MonadT<M, Lazy<?>, A> { |
| 22 | + |
| 23 | + private final Monad<Lazy<A>, M> mla; |
| 24 | + |
| 25 | + private LazyT(Monad<Lazy<A>, M> mla) { |
| 26 | + this.mla = mla; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * {@inheritDoc} |
| 31 | + */ |
| 32 | + @Override |
| 33 | + public <GA extends Monad<A, Lazy<?>>, FGA extends Monad<GA, M>> FGA run() { |
| 34 | + return mla.<GA>fmap(Functor::coerce).coerce(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * {@inheritDoc} |
| 39 | + */ |
| 40 | + @Override |
| 41 | + public <B> LazyT<M, B> flatMap(Function<? super A, ? extends Monad<B, MonadT<M, Lazy<?>, ?>>> f) { |
| 42 | + return new LazyT<>(mla.flatMap(lazyA -> f.apply(lazyA.value()).<MonadT<M, Lazy<?>, B>>coerce().run())); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * {@inheritDoc} |
| 47 | + */ |
| 48 | + @Override |
| 49 | + public <B> LazyT<M, B> pure(B b) { |
| 50 | + return new LazyT<>(mla.pure(lazy(b))); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * {@inheritDoc} |
| 55 | + */ |
| 56 | + @Override |
| 57 | + public <B> LazyT<M, B> fmap(Function<? super A, ? extends B> fn) { |
| 58 | + return MonadT.super.<B>fmap(fn).coerce(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * {@inheritDoc} |
| 63 | + */ |
| 64 | + @Override |
| 65 | + public <B> LazyT<M, B> zip(Applicative<Function<? super A, ? extends B>, MonadT<M, Lazy<?>, ?>> appFn) { |
| 66 | + return MonadT.super.zip(appFn).coerce(); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * {@inheritDoc} |
| 71 | + */ |
| 72 | + @Override |
| 73 | + public <B> Lazy<LazyT<M, B>> lazyZip( |
| 74 | + Lazy<? extends Applicative<Function<? super A, ? extends B>, MonadT<M, Lazy<?>, ?>>> lazyAppFn) { |
| 75 | + return new Compose<>(mla) |
| 76 | + .lazyZip(lazyAppFn.fmap(lazyT -> new Compose<>( |
| 77 | + lazyT.<LazyT<M, Function<? super A, ? extends B>>>coerce() |
| 78 | + .<Lazy<Function<? super A, ? extends B>>, |
| 79 | + Monad<Lazy<Function<? super A, ? extends B>>, M>>run()))) |
| 80 | + .fmap(compose -> lazyT(compose.getCompose())); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * {@inheritDoc} |
| 85 | + */ |
| 86 | + @Override |
| 87 | + public <B> LazyT<M, B> discardL(Applicative<B, MonadT<M, Lazy<?>, ?>> appB) { |
| 88 | + return MonadT.super.discardL(appB).coerce(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * {@inheritDoc} |
| 93 | + */ |
| 94 | + @Override |
| 95 | + public <B> LazyT<M, A> discardR(Applicative<B, MonadT<M, Lazy<?>, ?>> appB) { |
| 96 | + return MonadT.super.discardR(appB).coerce(); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public boolean equals(Object other) { |
| 101 | + return other instanceof LazyT<?, ?> && Objects.equals(mla, ((LazyT<?, ?>) other).mla); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public int hashCode() { |
| 106 | + return Objects.hash(mla); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public String toString() { |
| 111 | + return "LazyT{mla=" + mla + '}'; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Static factory method for lifting a <code>{@link Monad}<{@link Lazy}<A>, M></code> into a |
| 116 | + * {@link LazyT}. |
| 117 | + * |
| 118 | + * @param mla the {@link Monad}<{@link Lazy}<A>, M> |
| 119 | + * @param <M> the outer {@link Monad} unification parameter |
| 120 | + * @param <A> the carrier type |
| 121 | + * @return the new {@link LazyT} |
| 122 | + */ |
| 123 | + public static <M extends Monad<?, M>, A> LazyT<M, A> lazyT(Monad<Lazy<A>, M> mla) { |
| 124 | + return new LazyT<>(mla); |
| 125 | + } |
| 126 | +} |
0 commit comments