From 87ae1dfc8d8cb7b51d7bda4750ce841bbe691cfc Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sat, 7 May 2016 12:51:23 -0400 Subject: General changes --- BJC-Utils2/src/main/java/bjc/utils/data/IPair.java | 72 +++++++++++++++++++++- 1 file changed, 69 insertions(+), 3 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/IPair.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java index 316074e..707724b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java @@ -4,6 +4,8 @@ import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.Function; +import bjc.utils.funcdata.theory.Bifunctor; + /** * Represents a pair of values * @@ -14,7 +16,8 @@ import java.util.function.Function; * The type of the right side of the pair * */ -public interface IPair { +public interface IPair + extends Bifunctor { /** * Bind a function across the values in this pair * @@ -68,11 +71,45 @@ public interface IPair { }); } + @Override + default + Function, Bifunctor> + fmapLeft(Function func) { + return (argumentPair) -> { + if (!(argumentPair instanceof IPair)) { + throw new IllegalArgumentException( + "This function can only be applied to instances of IPair"); + } + + IPair argPair = (IPair) argumentPair; + + return argPair.mapLeft(func); + }; + } + + @Override + default + Function, Bifunctor> + + fmapRight(Function func) { + return (argumentPair) -> { + if (!(argumentPair instanceof IPair)) { + throw new IllegalArgumentException( + "This function can only be applied to instances of IPair"); + } + + IPair argPair = (IPair) argumentPair; + + return argPair.mapRight(func); + }; + } + /** * Get the value on the left side of the pair * * @return The value on the left side of the pair */ + @Override public default LeftType getLeft() { return merge((leftValue, rightValue) -> leftValue); } @@ -82,10 +119,39 @@ public interface IPair { * * @return The value on the right side of the pair */ + @Override public default RightType getRight() { return merge((leftValue, rightValue) -> rightValue); } + /** + * Transform the value on the left side of the pair. Doesn't modify the + * pair + * + * @param + * The new type of the left part of the pair + * @param mapper + * The function to use to transform the left part of the + * pair + * @return The pair, with its left part transformed + */ + public IPair + mapLeft(Function mapper); + + /** + * Transform the value on the right side of the pair. Doesn't modify + * the pair + * + * @param + * The new type of the right part of the pair + * @param mapper + * The function to use to transform the right part of the + * pair + * @return The pair, with its right part transformed + */ + public IPair + mapRight(Function mapper); + /** * Merge the two values in this pair into a single value * @@ -95,6 +161,6 @@ public interface IPair { * The function to use for merging * @return The pair, merged into a single value */ - public MergedType merge( - BiFunction merger); + public MergedType + merge(BiFunction merger); } -- cgit v1.2.3