summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-05-10 17:37:31 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-05-10 17:37:31 -0400
commit9d9677e0a34d412fed209996fde4ad0ae6acd463 (patch)
tree3197c18721f51d47a576466c481e54163e765d83 /BJC-Utils2/src/main/java/bjc/utils/funcdata/theory
parent61fd71f69e080790da722e0e03b71ecd7c2538a2 (diff)
Added ability to bimap over pairs and pair like things
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