com.jnape.palatable.lambda.optics.functions.Over Maven / Gradle / Ivy
Show all versions of lambda Show documentation
package com.jnape.palatable.lambda.optics.functions;
import com.jnape.palatable.lambda.functions.Fn1;
import com.jnape.palatable.lambda.functions.Fn2;
import com.jnape.palatable.lambda.functions.Fn3;
import com.jnape.palatable.lambda.functor.builtin.Identity;
import com.jnape.palatable.lambda.optics.Optic;
/**
* Given an {@link Optic}, a function from A
to B
, and a "larger" value S
,
* produce a T
by retrieving the A
from the S
, applying the function, and
* updating the S
with the B
resulting from the function.
*
* This function is similar to {@link Set}, except that it allows the setting value B
to be derived from
* S
via function application, rather than provided.
*
* @param the type of the larger value
* @param the type of the larger updated value
* @param the type of the smaller retrieving value
* @param the type of the smaller setting value
* @see Set
* @see View
*/
public final class Over implements
Fn3, ? super Identity>, S, T, A, B>, Fn1 super A, ? extends B>, S, T> {
private static final Over, ?, ?, ?> INSTANCE = new Over<>();
private Over() {
}
@Override
public T checkedApply(Optic super Fn1, ?>, ? super Identity>, S, T, A, B> optic,
Fn1 super A, ? extends B> fn,
S s) {
return optic., Identity>, Identity, Identity, Fn1>, Fn1>>apply(
a -> new Identity<>(fn.apply(a))).apply(s).runIdentity();
}
@SuppressWarnings("unchecked")
public static Over over() {
return (Over) INSTANCE;
}
public static Fn2, S, T> over(
Optic super Fn1, ?>, ? super Identity>, S, T, A, B> optic) {
return Over.over().apply(optic);
}
public static Fn1 over(Optic super Fn1, ?>, ? super Identity>, S, T, A, B> optic,
Fn1 super A, ? extends B> fn) {
return over(optic).apply(fn);
}
public static T over(Optic super Fn1, ?>, ? super Identity>, S, T, A, B> optic,
Fn1 super A, ? extends B> fn, S s) {
return over(optic, fn).apply(s);
}
}