
com.jnape.palatable.lambda.semigroup.builtin.MaxBy Maven / Gradle / Ivy
package com.jnape.palatable.lambda.semigroup.builtin;
import com.jnape.palatable.lambda.functions.Fn1;
import com.jnape.palatable.lambda.functions.specialized.SemigroupFactory;
import com.jnape.palatable.lambda.semigroup.Semigroup;
import java.util.function.Function;
import static com.jnape.palatable.lambda.functions.builtin.fn3.LTBy.ltBy;
/**
* Given a mapping function from some type A
to some {@link Comparable} type B
, produce a
* {@link Semigroup} over A
that chooses between two values x
and y
via the
* following rules:
*
* - If
x
is strictly less than y
in terms of B
, return y
* - Otherwise, return
x
*
*
* @param the value type
* @param the mapped comparison type
* @see Max
* @see MinBy
*/
public final class MaxBy> implements SemigroupFactory, A> {
private static final MaxBy INSTANCE = new MaxBy();
private MaxBy() {
}
@Override
public Semigroup apply(Function super A, ? extends B> compareFn) {
return (x, y) -> ltBy(compareFn, y, x) ? y : x;
}
@SuppressWarnings("unchecked")
public static > MaxBy maxBy() {
return INSTANCE;
}
public static > Semigroup maxBy(
Function super A, ? extends B> compareFn) {
return MaxBy.maxBy().apply(compareFn);
}
public static > Fn1 maxBy(Function super A, ? extends B> compareFn, A x) {
return MaxBy.maxBy(compareFn).apply(x);
}
public static > A maxBy(Function super A, ? extends B> compareFn, A x, A y) {
return maxBy(compareFn, x).apply(y);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy