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/Lenses.java | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'src/main/java/bjc/optics/Lenses.java') diff --git a/src/main/java/bjc/optics/Lenses.java b/src/main/java/bjc/optics/Lenses.java index 6cebc84..aad5571 100644 --- a/src/main/java/bjc/optics/Lenses.java +++ b/src/main/java/bjc/optics/Lenses.java @@ -76,7 +76,7 @@ public class Lenses { public static LensX, Holder, Part1, Part2> holder() { return immutable((hld) -> hld.getValue(), (hld, val) -> hld.map((vl) -> val)); } - + /** * Create a lens for updating tagged pairs. * @@ -92,7 +92,13 @@ public class Lenses { public static LensX, Pair, A, B> tagged() { return immutable((par) -> par.getRight(), (par, val) -> par.mapRight((vl) -> val)); } - + + // Not entirely sure what I was thinking when I wrote this. Pretty sure this + // would need W1 to be a monoid to make much sense + // public static Lens> both(Lens lhs, + // Lens rhs) { + // } + /** * Creates a lens which focuses on a piece of internal state. * @@ -107,7 +113,7 @@ public class Lenses { Holder hold = Holder.of(val); return immutable((whole) -> hold.getValue(), (whole, vl) -> hold.map((arg) -> vl)); } - + /** * Creates a lens which focuses on a piece of mutable internal state. * @@ -121,6 +127,17 @@ public class Lenses { Holder hold = Holder.of(val); return mutable((whole) -> hold.getValue(), (whole, vl) -> hold.replace(vl)); } + + /** + * Create a lens that focuses on the sign of an integer + * + * @return A lens that focuses on the sign of an integer + */ + public static Lens sign() { + LensX lensX = immutable((num) -> num >= 0, (num, sgn) -> sgn ? Math.abs(num) : -Math.abs(num)); + return (Lens) lensX; + } } final class FunctionalLensX implements LensX { -- cgit v1.2.3