summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/optics/Lenses.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/bjc/optics/Lenses.java')
-rw-r--r--src/main/java/bjc/optics/Lenses.java23
1 files changed, 20 insertions, 3 deletions
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 <Part1, Part2> LensX<Holder<Part1>, Holder<Part2>, 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 <A, B, T> LensX<Pair<T, A>, Pair<T, B>, 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 <W1, P1, P2> Lens<W1, Pair<P1, P2>> both(Lens<W1, P1> lhs,
+ // Lens<W1, P2> rhs) {
+ // }
+
/**
* Creates a lens which focuses on a piece of internal state.
*
@@ -107,7 +113,7 @@ public class Lenses {
Holder<A> 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<A> 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<Integer, Boolean> sign() {
+ LensX<Integer, Integer, Boolean,
+ Boolean> lensX = immutable((num) -> num >= 0, (num, sgn) -> sgn ? Math.abs(num) : -Math.abs(num));
+ return (Lens<Integer, Boolean>) lensX;
+ }
}
final class FunctionalLensX<W1, W2, P1, P2> implements LensX<W1, W2, P1, P2> {