|
| 1 | +package com.jnape.palatable.lambda.lens.functions; |
| 2 | + |
| 3 | +import com.jnape.palatable.lambda.functions.Fn1; |
| 4 | +import com.jnape.palatable.lambda.functions.Fn2; |
| 5 | +import com.jnape.palatable.lambda.functions.Fn3; |
| 6 | +import com.jnape.palatable.lambda.lens.Iso; |
| 7 | +import com.jnape.palatable.lambda.lens.LensLike; |
| 8 | + |
| 9 | +import java.util.function.Function; |
| 10 | + |
| 11 | +/** |
| 12 | + * The inverse of {@link Over}: given an {@link Iso}, a function from <code>T</code> to <code>S</code>, and a "smaller" |
| 13 | + * value <code>B</code>, return a "smaller" value <code>A</code> by traversing around the type ring (<code>B -> T |
| 14 | + * -> S -> A</code>). |
| 15 | + * <p> |
| 16 | + * Note this is only possible for {@link Iso}s and not general {@link LensLike}s because of the mandatory need for the |
| 17 | + * correspondence <code>B -> T</code>. |
| 18 | + * |
| 19 | + * @param <S> the larger type for focusing |
| 20 | + * @param <T> the larger type for mirrored focusing |
| 21 | + * @param <A> the smaller type for focusing |
| 22 | + * @param <B> the smaller type for mirrored focusing |
| 23 | + */ |
| 24 | +public final class Under<S, T, A, B> implements Fn3<Iso<S, T, A, B>, Function<? super T, ? extends S>, B, A> { |
| 25 | + |
| 26 | + private static final Under INSTANCE = new Under(); |
| 27 | + |
| 28 | + private Under() { |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public A apply(Iso<S, T, A, B> iso, Function<? super T, ? extends S> fn, B b) { |
| 33 | + return iso.unIso().into((sa, bt) -> bt.fmap(fn).fmap(sa)).apply(b); |
| 34 | + } |
| 35 | + |
| 36 | + @SuppressWarnings("unchecked") |
| 37 | + public static <S, T, A, B> Under<S, T, A, B> under() { |
| 38 | + return INSTANCE; |
| 39 | + } |
| 40 | + |
| 41 | + public static <S, T, A, B> Fn2<Function<? super T, ? extends S>, B, A> under(Iso<S, T, A, B> iso) { |
| 42 | + return Under.<S, T, A, B>under().apply(iso); |
| 43 | + } |
| 44 | + |
| 45 | + public static <S, T, A, B> Fn1<B, A> under(Iso<S, T, A, B> iso, Function<? super T, ? extends S> fn) { |
| 46 | + return under(iso).apply(fn); |
| 47 | + } |
| 48 | + |
| 49 | + public static <S, T, A, B> A under(Iso<S, T, A, B> iso, Function<? super T, ? extends S> fn, B b) { |
| 50 | + return under(iso, fn).apply(b); |
| 51 | + } |
| 52 | +} |
0 commit comments