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

com.jnape.palatable.lambda.optics.functions.View 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.functor.builtin.Const;
import com.jnape.palatable.lambda.optics.Optic;

/**
 * Given an {@link Optic} and a "larger" value S, retrieve a "smaller" value A by lifting the
 * {@link Optic} into the {@link Const} functor.
 * 

* More idiomatically, this function can be used to treat a {@link Optic} as a "getter" of As from * Ss. * * @param the type of the larger value * @param the type of the larger updated value (unused, but necessary for composition) * @param the type of the smaller retrieving value * @param the type of the smaller setting value (unused, but necessary for composition) * @see Set * @see Over */ public final class View implements Fn2, ? super Const, S, T, A, B>, S, A> { private static final View INSTANCE = new View<>(); private View() { } @Override public A checkedApply(Optic, ? super Const, S, T, A, B> optic, S s) { return optic., Const, Const, Const, Fn1>, Fn1>>apply( Const::new).apply(s).runConst(); } @SuppressWarnings("unchecked") public static View view() { return (View) INSTANCE; } public static Fn1 view(Optic, ? super Const, S, T, A, B> optic) { return View.view().apply(optic); } public static A view(Optic, ? super Const, S, T, A, B> optic, S s) { return view(optic).apply(s); } }