All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jnape.palatable.lambda.semigroup.builtin.MaxBy Maven / Gradle / Ivy

There is a newer version: 5.5.0
Show newest version
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 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 MaxWith * @see MinBy */ public final class MaxBy> implements SemigroupFactory, A> { private static final MaxBy INSTANCE = new MaxBy<>(); private MaxBy() { } @Override public Semigroup checkedApply(Fn1 compareFn) { return (x, y) -> ltBy(compareFn, y, x) ? y : x; } @SuppressWarnings("unchecked") public static > MaxBy maxBy() { return (MaxBy) INSTANCE; } public static > Semigroup maxBy(Fn1 compareFn) { return MaxBy.maxBy().apply(compareFn); } public static > Fn1 maxBy(Fn1 compareFn, A x) { return MaxBy.maxBy(compareFn).apply(x); } public static > A maxBy(Fn1 compareFn, A x, A y) { return maxBy(compareFn, x).apply(y); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy