summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata/theory')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java41
1 files changed, 41 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java
index 3bea135..e8c74fc 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java
@@ -51,6 +51,47 @@ public interface Bifunctor<LeftType, RightType> {
fmapRight(Function<OldRight, NewRight> func);
/**
+ * Lift a pair of functions to a single function that maps over both
+ * parts of a pair
+ *
+ * @param <OldLeft>
+ * The old left type of the pair
+ * @param <OldRight>
+ * The old right type of the pair
+ * @param <NewLeft>
+ * The new left type of the pair
+ * @param <NewRight>
+ * The new right type of the pair
+ * @param leftFunc
+ * The function that maps over the left of the pair
+ * @param rightFunc
+ * The function that maps over the right of the pair
+ * @return A function that maps over both parts of the pair
+ */
+ public default <OldLeft, OldRight, NewLeft, NewRight>
+ Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, NewRight>>
+ bimap(Function<OldLeft, NewLeft> leftFunc,
+ Function<OldRight, NewRight> rightFunc) {
+ Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, NewRight>> bimappedFunc =
+ (argPair) -> {
+ Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, OldRight>> leftMapper =
+ argPair.<OldLeft, OldRight, NewLeft> fmapLeft(
+ leftFunc);
+
+ Bifunctor<NewLeft, OldRight> leftMappedFunctor =
+ leftMapper.apply(argPair);
+ Function<Bifunctor<NewLeft, OldRight>, Bifunctor<NewLeft, NewRight>> rightMapper =
+ leftMappedFunctor
+ .<NewLeft, OldRight, NewRight> fmapRight(
+ rightFunc);
+
+ return rightMapper.apply(leftMappedFunctor);
+ };
+
+ return bimappedFunc;
+ }
+
+ /**
* Get the value contained on the left of this bifunctor
*
* @return The value on the left side of this bifunctor