From 44be6e6cd7671dd243056107ffa6201504f7fbce Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Sun, 25 Jun 2023 15:50:38 -0400 Subject: Update a number of things --- src/main/java/bjc/optics/PrismX.java | 67 ++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'src/main/java/bjc/optics/PrismX.java') diff --git a/src/main/java/bjc/optics/PrismX.java b/src/main/java/bjc/optics/PrismX.java index b4986cf..0f4a0e0 100644 --- a/src/main/java/bjc/optics/PrismX.java +++ b/src/main/java/bjc/optics/PrismX.java @@ -17,6 +17,73 @@ */ package bjc.optics; +import java.util.function.Function; + +import bjc.data.Either; + +/** + * Represents a Prism, which is a type of optic. + * + * TODO: Add better description + * + * @author bjcul + * + * @param The type of the first whole + * @param The type of the second whole + * @param The type of the first part + * @param The type of the second part + */ public interface PrismX extends Optic { + /** + * Match against this prism. + * + * @param part The part to match + * @return Either the matched value or the prism + */ + Either match(P1 part); + /** + * Build this prism from a given part. + * + * @param whole The whole to build from + * @return The part that is constructed + */ + P2 build(W2 whole); + + /** + * Create a prism from its component parts + * + * @param The type of the first whole + * @param The type of the second whole + * @param The type of the first part + * @param The type of the second part + * + * @param f The 'match' function for the prism + * @param g The 'build' function for the prism + * + * @return A prism built from the given parts + */ + static PrismX of(Function> f, Function g) { + return new FunctionalPrismX<>(g, f); + } } + +final class FunctionalPrismX implements PrismX { + private final Function g; + private final Function> f; + + public FunctionalPrismX(Function g, Function> f) { + this.g = g; + this.f = f; + } + + @Override + public P2 build(W2 whole) { + return g.apply(whole); + } + + @Override + public Either match(P1 part) { + return f.apply(part); + } +} \ No newline at end of file -- cgit v1.2.3