From 9d9677e0a34d412fed209996fde4ad0ae6acd463 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Tue, 10 May 2016 17:37:31 -0400 Subject: Added ability to bimap over pairs and pair like things --- .../java/bjc/utils/funcdata/theory/Bifunctor.java | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata/theory') 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 @@ -50,6 +50,47 @@ public interface Bifunctor { Function, Bifunctor> fmapRight(Function func); + /** + * Lift a pair of functions to a single function that maps over both + * parts of a pair + * + * @param + * The old left type of the pair + * @param + * The old right type of the pair + * @param + * The new left type of the pair + * @param + * 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 + Function, Bifunctor> + bimap(Function leftFunc, + Function rightFunc) { + Function, Bifunctor> bimappedFunc = + (argPair) -> { + Function, Bifunctor> leftMapper = + argPair. fmapLeft( + leftFunc); + + Bifunctor leftMappedFunctor = + leftMapper.apply(argPair); + Function, Bifunctor> rightMapper = + leftMappedFunctor + . fmapRight( + rightFunc); + + return rightMapper.apply(leftMappedFunctor); + }; + + return bimappedFunc; + } + /** * Get the value contained on the left of this bifunctor * -- cgit v1.2.3