From 39d54649d1dd7df1b707d412817713c1a080e5cd Mon Sep 17 00:00:00 2001
From: jnape
+ * Example:
+ * {@link TypeSafeKey} -> {@link Semigroup}
+ * {@link MergeHMaps#key(TypeSafeKey, Semigroup) mappings}, defaulting to {@link Last} in case no
+ * {@link Semigroup} has been chosen for a given {@link TypeSafeKey}.
+ */
+public final class MergeHMaps implements MonoidA to some {@link Comparable} type B and two values
@@ -16,6 +18,7 @@
* @param the value type
* @param the mapped comparison type
* @see CmpEq
+ * @see CmpEqWith
* @see LTBy
* @see GTBy
*/
@@ -28,7 +31,7 @@ private CmpEqBy() {
@Override
public Boolean checkedApply(Fn1 super A, ? extends B> compareFn, A x, A y) {
- return compareFn.apply(x).compareTo(compareFn.apply(y)) == 0;
+ return cmpEqWith(comparing(compareFn.toFunction()), x, y);
}
@Override
diff --git a/src/main/java/com/jnape/palatable/lambda/functions/builtin/fn3/CmpEqWith.java b/src/main/java/com/jnape/palatable/lambda/functions/builtin/fn3/CmpEqWith.java
new file mode 100644
index 000000000..0a0c1f1ce
--- /dev/null
+++ b/src/main/java/com/jnape/palatable/lambda/functions/builtin/fn3/CmpEqWith.java
@@ -0,0 +1,62 @@
+package com.jnape.palatable.lambda.functions.builtin.fn3;
+
+import com.jnape.palatable.lambda.functions.Fn3;
+import com.jnape.palatable.lambda.functions.specialized.BiPredicate;
+import com.jnape.palatable.lambda.functions.specialized.Predicate;
+
+import java.util.Comparator;
+
+import static com.jnape.palatable.lambda.functions.builtin.fn3.Compare.compare;
+import static com.jnape.palatable.lambda.functions.ordering.ComparisonRelation.equal;
+import static com.jnape.palatable.lambda.functions.specialized.Predicate.predicate;
+
+/**
+ * Given a {@link Comparator} from some type A and two values of type A, return true
+ * if the first value is strictly equal to the second value (according to {@link Comparator#compare(A, A)}
+ * otherwise, return false.
+ *
+ * @param the value type
+ * @see CmpEqBy
+ * @see LTBy
+ * @see GTBy
+ * @see Compare
+ */
+public final class CmpEqWith implements Fn3A and two values of type A, return a
+ * {@link ComparisonRelation} of the first value with reference to the second value (according to {@link Comparator#compare(A, A)}.
+ * The order of parameters is flipped with respect to {@link Comparator#compare(A, A)} for more idiomatic partial application.
+ *
+ *
+ * {@code
+ * Compare.compare(naturalOrder(), 1, 2); // ComparisonRelation.GreaterThan
+ * Compare.compare(naturalOrder(), 2, 1); // ComparisonRelation.LessThan
+ * Compare.compare(naturalOrder(), 1, 1); // ComparisonRelation.Equal
+ * }
+ *
+ *
A to some {@link Comparable} type B and two values
@@ -16,6 +18,7 @@
* @param the value type
* @param the mapped comparison type
* @see GT
+ * @see GTWith
* @see LTBy
*/
public final class GTBy> implements Fn3A to some {@link Comparable} type B and two values
@@ -18,6 +19,7 @@
* @param the value type
* @param the mapped comparison type
* @see GTE
+ * @see GTEWith
* @see LTEBy
*/
public final class GTEBy> implements Fn3A and two values of type A,
+ * return true if the second value is greater than or equal to the first value in
+ * terms of their mapped B results according to {@link Comparator#compare(A, A)};
+ * otherwise, return false.
+ *
+ * @param the value type
+ * @see GTE
+ * @see GTEBy
+ * @see LTEWith
+ */
+public final class GTEWith implements Fn3A and two values of type A,
+ * return true if the second value is strictly greater than the first value in
+ * terms of their mapped B results; otherwise, return false.
+ *
+ * @param the value type
+ * @see GT
+ * @see GTBy
+ * @see LTWith
+ */
+public final class GTWith implements Fn3A to some {@link Comparable} type B and two values
@@ -27,7 +29,7 @@ private LTBy() {
@Override
public Boolean checkedApply(Fn1 super A, ? extends B> compareFn, A y, A x) {
- return compareFn.apply(x).compareTo(compareFn.apply(y)) < 0;
+ return ltWith(comparing(compareFn.toFunction()), y, x);
}
@Override
diff --git a/src/main/java/com/jnape/palatable/lambda/functions/builtin/fn3/LTEBy.java b/src/main/java/com/jnape/palatable/lambda/functions/builtin/fn3/LTEBy.java
index bc755470d..4c377d583 100644
--- a/src/main/java/com/jnape/palatable/lambda/functions/builtin/fn3/LTEBy.java
+++ b/src/main/java/com/jnape/palatable/lambda/functions/builtin/fn3/LTEBy.java
@@ -6,7 +6,8 @@
import com.jnape.palatable.lambda.functions.specialized.BiPredicate;
import com.jnape.palatable.lambda.functions.specialized.Predicate;
-import static com.jnape.palatable.lambda.functions.builtin.fn3.CmpEqBy.cmpEqBy;
+import static com.jnape.palatable.lambda.functions.builtin.fn3.LTEWith.lteWith;
+import static java.util.Comparator.comparing;
/**
* Given a mapping function from some type A to some {@link Comparable} type B and two values
@@ -28,7 +29,7 @@ private LTEBy() {
@Override
public Boolean checkedApply(Fn1 super A, ? extends B> compareFn, A y, A x) {
- return LTBy.ltBy(compareFn).or(cmpEqBy(compareFn)).apply(y, x);
+ return lteWith(comparing(compareFn.toFunction()), y, x);
}
@Override
diff --git a/src/main/java/com/jnape/palatable/lambda/functions/builtin/fn3/LTEWith.java b/src/main/java/com/jnape/palatable/lambda/functions/builtin/fn3/LTEWith.java
new file mode 100644
index 000000000..06e621d3f
--- /dev/null
+++ b/src/main/java/com/jnape/palatable/lambda/functions/builtin/fn3/LTEWith.java
@@ -0,0 +1,62 @@
+package com.jnape.palatable.lambda.functions.builtin.fn3;
+
+import com.jnape.palatable.lambda.functions.Fn3;
+import com.jnape.palatable.lambda.functions.builtin.fn2.LTE;
+import com.jnape.palatable.lambda.functions.specialized.BiPredicate;
+import com.jnape.palatable.lambda.functions.specialized.Predicate;
+
+import java.util.Comparator;
+
+import static com.jnape.palatable.lambda.functions.builtin.fn3.GTWith.gtWith;
+import static com.jnape.palatable.lambda.functions.specialized.Predicate.predicate;
+
+/**
+ * Given a {@link Comparator} from some type A and two values of type A,
+ * return true if the second value is less than or equal to the first value in
+ * terms of their mapped B results according to {@link Comparator#compare(A, A)};
+ * otherwise, return false.
+ *
+ * @param the value type
+ * @see LTE
+ * @see LTEBy
+ * @see GTEWith
+ */
+public final class LTEWith implements Fn3A and two values of type A,
+ * return true if the second value is strictly less than than the first value in
+ * terms of their mapped B results; otherwise, return false.
+ *
+ * @param the value type
+ * @see LT
+ * @see LTBy
+ * @see GTWith
+ */
+public final class LTWith implements Fn3A, produce a {@link Semigroup} over A that chooses
+ * between two values x and y via the following rules:
+ * x is strictly less than y in terms of B, return yxA, produce a {@link Semigroup} over A that chooses
+ * between two values x and y via the following rules:
+ * x is strictly greater than y in terms of B, return yx