fj.Semigroup Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionaljava Show documentation
Show all versions of functionaljava Show documentation
Functional Java is an open source library that supports closures for the Java programming language
package fj;
import static fj.Function.curry;
import fj.data.Array;
import fj.data.List;
import fj.data.Natural;
import fj.data.NonEmptyList;
import fj.data.Option;
import fj.data.Set;
import fj.data.Stream;
import java.math.BigInteger;
import java.math.BigDecimal;
/**
* Implementations must satisfy the law of associativity:
*
* - Associativity; forall x. forall y. forall z. sum(sum(x, y), z) == sum(x, sum(y, z))
*
*
* @version %build.number%
*/
public final class Semigroup {
private final F> sum;
private Semigroup(final F> sum) {
this.sum = sum;
}
/**
* Sums the two given arguments.
*
* @param a1 A value to sum with another.
* @param a2 A value to sum with another.
* @return The of the two given arguments.
*/
public A sum(final A a1, final A a2) {
return sum.f(a1).f(a2);
}
/**
* Returns a function that sums the given value according to this semigroup.
*
* @param a1 The value to sum.
* @return A function that sums the given value according to this semigroup.
*/
public F sum(final A a1) {
return sum.f(a1);
}
/**
* Returns a function that sums according to this semigroup.
*
* @return A function that sums according to this semigroup.
*/
public F> sum() {
return sum;
}
/**
* Constructs a semigroup from the given function.
*
* @param sum The function to construct this semigroup with.
* @return A semigroup from the given function.
*/
public static Semigroup semigroup(final F> sum) {
return new Semigroup(sum);
}
/**
* Constructs a semigroup from the given function.
*
* @param sum The function to construct this semigroup with.
* @return A semigroup from the given function.
*/
public static Semigroup semigroup(final F2 sum) {
return new Semigroup(curry(sum));
}
/**
* A semigroup that adds integers.
*/
public static final Semigroup intAdditionSemigroup = semigroup((i1, i2) -> i1 + i2);
/**
* A semigroup that adds doubles.
*/
public static final Semigroup doubleAdditionSemigroup = semigroup((d1, d2) -> d1 + d2);
/**
* A semigroup that multiplies integers.
*/
public static final Semigroup intMultiplicationSemigroup = semigroup((i1, i2) -> i1 * i2);
/**
* A semigroup that multiplies doubles.
*/
public static final Semigroup doubleMultiplicationSemigroup = semigroup((d1, d2) -> d1 * d2);
/**
* A semigroup that yields the maximum of integers.
*/
public static final Semigroup intMaximumSemigroup = semigroup(Ord.intOrd.max);
/**
* A semigroup that yields the minimum of integers.
*/
public static final Semigroup intMinimumSemigroup = semigroup(Ord.intOrd.min);
/**
* A semigroup that adds big integers.
*/
public static final Semigroup bigintAdditionSemigroup =
semigroup((i1, i2) -> i1.add(i2));
/**
* A semigroup that multiplies big integers.
*/
public static final Semigroup bigintMultiplicationSemigroup =
semigroup((i1, i2) -> i1.multiply(i2));
/**
* A semigroup that yields the maximum of big integers.
*/
public static final Semigroup bigintMaximumSemigroup = semigroup(Ord.bigintOrd.max);
/**
* A semigroup that yields the minimum of big integers.
*/
public static final Semigroup bigintMinimumSemigroup = semigroup(Ord.bigintOrd.min);
/**
* A semigroup that adds big decimals.
*/
public static final Semigroup bigdecimalAdditionSemigroup =
semigroup((i1, i2) -> i1.add(i2));
/**
* A semigroup that multiplies big decimals.
*/
public static final Semigroup bigdecimalMultiplicationSemigroup =
semigroup((i1, i2) -> i1.multiply(i2));
/**
* A semigroup that yields the maximum of big decimals.
*/
public static final Semigroup bigDecimalMaximumSemigroup = semigroup(Ord.bigdecimalOrd.max);
/**
* A semigroup that yields the minimum of big decimals.
*/
public static final Semigroup bigDecimalMinimumSemigroup = semigroup(Ord.bigdecimalOrd.min);
/**
* A semigroup that multiplies natural numbers.
*/
public static final Semigroup naturalMultiplicationSemigroup =
semigroup((n1, n2) -> n1.multiply(n2));
/**
* A semigroup that multiplies natural numbers.
*/
public static final Semigroup naturalAdditionSemigroup =
semigroup((n1, n2) -> n1.add(n2));
/**
* A semigroup that yields the maximum of natural numbers.
*/
public static final Semigroup naturalMaximumSemigroup = semigroup(Ord.naturalOrd.max);
/**
* A semigroup that yields the minimum of natural numbers.
*/
public static final Semigroup naturalMinimumSemigroup = semigroup(Ord.naturalOrd.min);
/**
* A semigroup that adds longs.
*/
public static final Semigroup longAdditionSemigroup = semigroup((x, y) -> x + y);
/**
* A semigroup that multiplies longs.
*/
public static final Semigroup longMultiplicationSemigroup = semigroup((x, y) -> x * y);
/**
* A semigroup that yields the maximum of longs.
*/
public static final Semigroup longMaximumSemigroup = semigroup(Ord.longOrd.max);
/**
* A semigroup that yields the minimum of longs.
*/
public static final Semigroup longMinimumSemigroup = semigroup(Ord.longOrd.min);
/**
* A semigroup that ORs booleans.
*/
public static final Semigroup disjunctionSemigroup = semigroup((b1, b2) -> b1 || b2);
/**
* A semigroup that XORs booleans.
*/
public static final Semigroup exclusiveDisjunctionSemiGroup = semigroup((p, q) -> p && !q || !p && q);
/**
* A semigroup that ANDs booleans.
*/
public static final Semigroup conjunctionSemigroup = semigroup((b1, b2) -> b1 && b2);
/**
* A semigroup that appends strings.
*/
public static final Semigroup stringSemigroup = semigroup((s1, s2) -> s1 + s2);
/**
* A semigroup that appends string buffers.
*/
public static final Semigroup stringBufferSemigroup =
semigroup((s1, s2) -> new StringBuffer(s1).append(s2));
/**
* A semigroup that appends string builders.
*/
public static final Semigroup stringBuilderSemigroup =
semigroup((s1, s2) -> new StringBuilder(s1).append(s2));
/**
* A semigroup for functions.
*
* @param sb The smeigroup for the codomain.
* @return A semigroup for functions.
*/
public static Semigroup> functionSemigroup(final Semigroup sb) {
return semigroup((a1, a2) -> a -> sb.sum(a1.f(a), a2.f(a)));
}
/**
* A semigroup for lists.
*
* @return A semigroup for lists.
*/
public static Semigroup> listSemigroup() {
return semigroup((a1, a2) -> a1.append(a2));
}
/**
* A semigroup for non-empty lists.
*
* @return A semigroup for non-empty lists.
*/
public static Semigroup> nonEmptyListSemigroup() {
return semigroup((a1, a2) -> a1.append(a2));
}
/**
* A semigroup for optional values.
** @return A semigroup for optional values.
*/
public static Semigroup