All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jnape.palatable.lambda.optics.functions.Over Maven / Gradle / Ivy

There is a newer version: 5.5.0
Show newest version
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, S, T> { private static final Over INSTANCE = new Over<>(); private Over() { } @Override public T checkedApply(Optic, ? super Identity, S, T, A, B> optic, Fn1 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 Identity, S, T, A, B> optic) { return Over.over().apply(optic); } public static Fn1 over(Optic, ? super Identity, S, T, A, B> optic, Fn1 fn) { return over(optic).apply(fn); } public static T over(Optic, ? super Identity, S, T, A, B> optic, Fn1 fn, S s) { return over(optic, fn).apply(s); } }