|
| 1 | +package com.jnape.palatable.lambda.adt; |
| 2 | + |
| 3 | +import com.jnape.palatable.lambda.adt.coproduct.CoProduct2; |
| 4 | +import com.jnape.palatable.lambda.adt.coproduct.CoProduct3; |
| 5 | +import com.jnape.palatable.lambda.adt.hlist.Tuple2; |
| 6 | +import com.jnape.palatable.lambda.functor.Applicative; |
| 7 | +import com.jnape.palatable.lambda.functor.Bifunctor; |
| 8 | +import com.jnape.palatable.lambda.monad.Monad; |
| 9 | +import com.jnape.palatable.lambda.traversable.Traversable; |
| 10 | + |
| 11 | +import java.util.Objects; |
| 12 | +import java.util.function.Function; |
| 13 | + |
| 14 | +import static com.jnape.palatable.lambda.adt.hlist.HList.tuple; |
| 15 | +import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly; |
| 16 | +import static com.jnape.palatable.lambda.functions.builtin.fn2.Into.into; |
| 17 | + |
| 18 | +/** |
| 19 | + * The coproduct of a coproduct (<code>{@link CoProduct2}<A, B></code>) and its product (<code>{@link |
| 20 | + * Tuple2}<A, B></code>), represented as a <code>{@link CoProduct3}<A, B, {@link Tuple2}<A, |
| 21 | + * B>></code>. |
| 22 | + * |
| 23 | + * @param <A> the first possible type |
| 24 | + * @param <B> the second possible type |
| 25 | + */ |
| 26 | +public abstract class These<A, B> implements CoProduct3<A, B, Tuple2<A, B>, These<A, B>>, Monad<B, These<A, ?>>, Bifunctor<A, B, These<?, ?>>, Traversable<B, These<A, ?>> { |
| 27 | + |
| 28 | + private These() { |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritDoc} |
| 33 | + */ |
| 34 | + @Override |
| 35 | + public final <C, D> These<C, D> biMap(Function<? super A, ? extends C> lFn, |
| 36 | + Function<? super B, ? extends D> rFn) { |
| 37 | + return match(a -> a(lFn.apply(a)), b -> b(rFn.apply(b)), into((a, b) -> both(lFn.apply(a), rFn.apply(b)))); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * {@inheritDoc} |
| 42 | + */ |
| 43 | + @Override |
| 44 | + public final <C> These<A, C> flatMap(Function<? super B, ? extends Monad<C, These<A, ?>>> f) { |
| 45 | + return match(These::a, b -> f.apply(b).coerce(), into((a, b) -> f.apply(b).<These<A, C>>coerce().biMapL(constantly(a)))); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * {@inheritDoc} |
| 50 | + */ |
| 51 | + @Override |
| 52 | + public final <C> These<A, C> pure(C c) { |
| 53 | + return match(a -> both(a, c), b -> b(c), into((a, b) -> both(a, c))); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * {@inheritDoc} |
| 58 | + */ |
| 59 | + @Override |
| 60 | + public <C, App extends Applicative> Applicative<These<A, C>, App> traverse( |
| 61 | + Function<? super B, ? extends Applicative<C, App>> fn, |
| 62 | + Function<? super Traversable<C, These<A, ?>>, ? extends Applicative<? extends Traversable<C, These<A, ?>>, App>> pure) { |
| 63 | + return match(a -> pure.apply(a(a)).fmap(x -> (These<A, C>) x), |
| 64 | + b -> fn.apply(b).fmap(this::pure), |
| 65 | + into((a, b) -> fn.apply(b).fmap(c -> both(a, c)))); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * {@inheritDoc} |
| 70 | + */ |
| 71 | + @Override |
| 72 | + @SuppressWarnings("unchecked") |
| 73 | + public final <Z> These<Z, B> biMapL(Function<? super A, ? extends Z> fn) { |
| 74 | + return (These<Z, B>) Bifunctor.super.biMapL(fn); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * {@inheritDoc} |
| 79 | + */ |
| 80 | + @Override |
| 81 | + @SuppressWarnings("unchecked") |
| 82 | + public final <C> These<A, C> biMapR(Function<? super B, ? extends C> fn) { |
| 83 | + return (These<A, C>) Bifunctor.super.biMapR(fn); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * {@inheritDoc} |
| 88 | + */ |
| 89 | + @Override |
| 90 | + public final <C> These<A, C> fmap(Function<? super B, ? extends C> fn) { |
| 91 | + return Monad.super.<C>fmap(fn).coerce(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * {@inheritDoc} |
| 96 | + */ |
| 97 | + @Override |
| 98 | + public final <C> These<A, C> zip(Applicative<Function<? super B, ? extends C>, These<A, ?>> appFn) { |
| 99 | + return Monad.super.zip(appFn).coerce(); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * {@inheritDoc} |
| 104 | + */ |
| 105 | + @Override |
| 106 | + public final <C> These<A, C> discardL(Applicative<C, These<A, ?>> appB) { |
| 107 | + return Monad.super.discardL(appB).coerce(); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * {@inheritDoc} |
| 112 | + */ |
| 113 | + @Override |
| 114 | + public final <C> These<A, B> discardR(Applicative<C, These<A, ?>> appB) { |
| 115 | + return Monad.super.discardR(appB).coerce(); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Static factory method for wrapping a value of type <code>A</code> in a {@link These}. |
| 120 | + * |
| 121 | + * @param a the value |
| 122 | + * @param <A> the first possible type |
| 123 | + * @param <B> the second possible type |
| 124 | + * @return the wrapped value as a <code>{@link These}<A,B></code> |
| 125 | + */ |
| 126 | + public static <A, B> These<A, B> a(A a) { |
| 127 | + return new _A<>(a); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Static factory method for wrapping a value of type <code>B</code> in a {@link These}. |
| 132 | + * |
| 133 | + * @param b the value |
| 134 | + * @param <A> the first possible type |
| 135 | + * @param <B> the second possible type |
| 136 | + * @return the wrapped value as a <code>{@link These}<A,B></code> |
| 137 | + */ |
| 138 | + public static <A, B> These<A, B> b(B b) { |
| 139 | + return new _B<>(b); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Static factory method for wrapping a value of type <code>A</code> and a value of type <code>B</code> in a {@link |
| 144 | + * These}. |
| 145 | + * |
| 146 | + * @param a the first value |
| 147 | + * @param b the second value |
| 148 | + * @param <A> the first possible type |
| 149 | + * @param <B> the second possible type |
| 150 | + * @return the wrapped values as a <code>{@link These}<A,B></code> |
| 151 | + */ |
| 152 | + public static <A, B> These<A, B> both(A a, B b) { |
| 153 | + return new Both<>(tuple(a, b)); |
| 154 | + } |
| 155 | + |
| 156 | + private static final class _A<A, B> extends These<A, B> { |
| 157 | + |
| 158 | + private final A a; |
| 159 | + |
| 160 | + private _A(A a) { |
| 161 | + this.a = a; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public <R> R match(Function<? super A, ? extends R> aFn, Function<? super B, ? extends R> bFn, |
| 166 | + Function<? super Tuple2<A, B>, ? extends R> cFn) { |
| 167 | + return aFn.apply(a); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public boolean equals(Object other) { |
| 172 | + return other instanceof These._A && Objects.equals(a, ((_A) other).a); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public int hashCode() { |
| 177 | + return Objects.hash(a); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public String toString() { |
| 182 | + return "These{a=" + a + '}'; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + private static final class _B<A, B> extends These<A, B> { |
| 187 | + private final B b; |
| 188 | + |
| 189 | + private _B(B b) { |
| 190 | + this.b = b; |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public <R> R match(Function<? super A, ? extends R> aFn, Function<? super B, ? extends R> bFn, |
| 195 | + Function<? super Tuple2<A, B>, ? extends R> cFn) { |
| 196 | + return bFn.apply(b); |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public boolean equals(Object other) { |
| 201 | + return other instanceof These._B && Objects.equals(b, ((_B) other).b); |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public int hashCode() { |
| 206 | + return Objects.hash(b); |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + public String toString() { |
| 211 | + return "These{b=" + b + '}'; |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + private static final class Both<A, B> extends These<A, B> { |
| 216 | + private final Tuple2<A, B> both; |
| 217 | + |
| 218 | + private Both(Tuple2<A, B> tuple) { |
| 219 | + this.both = tuple; |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public <R> R match(Function<? super A, ? extends R> aFn, Function<? super B, ? extends R> bFn, |
| 224 | + Function<? super Tuple2<A, B>, ? extends R> cFn) { |
| 225 | + return cFn.apply(both); |
| 226 | + } |
| 227 | + |
| 228 | + @Override |
| 229 | + public boolean equals(Object other) { |
| 230 | + return other instanceof Both && Objects.equals(both, ((Both) other).both); |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + public int hashCode() { |
| 235 | + return Objects.hash(both); |
| 236 | + } |
| 237 | + |
| 238 | + @Override |
| 239 | + public String toString() { |
| 240 | + return "These{both=" + both + '}'; |
| 241 | + } |
| 242 | + } |
| 243 | +} |
0 commit comments