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

fj.Semigroup Maven / Gradle / Ivy

Go to download

Functional Java is an open source library that supports closures for the Java programming language

There is a newer version: 5.0
Show newest version
package fj;

import fj.data.Array;
import fj.data.DList;
import fj.data.List;
import fj.data.IO;
import fj.data.Natural;
import fj.data.NonEmptyList;
import fj.data.Option;
import fj.data.Set;
import fj.data.Stream;

import java.math.BigDecimal;
import java.math.BigInteger;

import static fj.F1Functions.dimap;
import static fj.Function.constant;
import static fj.Function.identity;
import static fj.Monoid.*;
import static fj.data.DList.listDList;
import static fj.data.Option.none;
import static fj.data.Option.some;

/**
 * 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 { /** * Primitives functions of Semigroup: minimal definition and overridable methods. */ public interface Definition { A append(A a1, A a2); default F prepend(A a) { return a2 -> append(a, a2); } default A sum(A a, F0> as) { return as.f().foldLeft(this::append, a); } default A multiply1p(int n, A a) { if (n <= 0) { return a; } A xTmp = a; int yTmp = n; A zTmp = a; while (true) { if ((yTmp & 1) == 1) { zTmp = append(xTmp, zTmp); if (yTmp == 1) { return zTmp; } } xTmp = append(xTmp, xTmp); yTmp = (yTmp) >>> 1; } } default Definition dual() { return new Definition(){ @Override public A append(A a1, A a2) { return Definition.this.append(a2, a1); } @Override public A multiply1p(int n, A a) { return Definition.this.multiply1p(n, a); } }; } } /** * Primitives functions of Semigroup: alternative minimal definition and overridable methods. */ public interface AltDefinition extends Definition { @Override F prepend(A a); @Override default A append(A a1, A a2) { return prepend(a1).f(a2); } } private final Definition def; private Semigroup(final Definition def) { this.def = def; } /** * 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 def.append(a1, 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 def.prepend(a1); } /** * Returns a function that sums according to this semigroup. * * @return A function that sums according to this semigroup. */ public F> sum() { return def::prepend; } /** * Returns a value summed n + 1 times ( * a + a + ... + a) The default definition uses peasant * multiplication, exploiting associativity to only require `O(log n)` uses of * {@link #sum(Object, Object)}. * * @param n multiplier * @param a the value to be reapeatly summed n + 1 times * @return {@code a} summed {@code n} times. If {@code n <= 0}, returns * {@code zero()} */ public A multiply1p(int n, A a) { return def.multiply1p(n, a); } /** * Sums the given values with left-fold. */ public A sumNel(final NonEmptyList as) { return as.foldLeft1(def::append); } /** * Sums the given values with left-fold, shortcutting the computation as early as possible. */ public A sumStream(A a, F0> as) { return def.sum(a, as); } /** * Swaps the arguments when summing. */ public Semigroup dual() { return semigroupDef(def.dual()); } /** * Lifts the semigroup to obtain a trivial monoid. */ public Monoid> lift() { Definition def = this.def; return monoidDef(new Monoid.Definition>() { @Override public Option empty() { return none(); } @Override public Option append(Option a1, Option a2) { return a1.liftM2(a1, def::append).orElse(a1).orElse(a2); } @Override public Option multiply(int n, Option oa) { return n > 0 ? oa.map(a -> def.multiply1p(n - 1, a)) : none(); } @Override public Option sum(F0>> oas) { Stream as = oas.f().bind(Option::toStream); return as.uncons(none(), h -> tail -> some(def.sum(h, tail::_1))); } }); } /** * Maps the given functions across this monoid as an invariant functor. * * @param f The covariant map. * @param g The contra-variant map. * @return A new monoid. */ public Semigroup xmap(final F f, final F g) { Definition def = this.def; return semigroupDef(new Definition() { @Override public B append(B a1, B a2) { return f.f(def.append(g.f(a1), g.f(a2))); } @Override public F prepend(B b) { return dimap(def.prepend(g.f(b)), g, f); } @Override public B multiply1p(int n, B b) { return f.f(def.multiply1p(n , g.f(b))); } @Override public B sum(B b, F0> bs) { return f.f(def.sum(g.f(b), () -> bs.f().map(g))); } }); } public Semigroup compose(Semigroup sb, final F b, final F a, final F2 c) { Definition saDef = this.def; Definition sbDef = sb.def; return semigroupDef(new Definition() { @Override public C append(C c1, C c2) { return c.f(saDef.append(a.f(c1), a.f(c2)), sbDef.append(b.f(c1), b.f(c2))); } @Override public F prepend(C c1) { F prependA = saDef.prepend(a.f(c1)); F prependB = sbDef.prepend(b.f(c1)); return c2 -> c.f(prependA.f(a.f(c2)), prependB.f(b.f(c2))); } @Override public C multiply1p(int n, C c1) { return c.f(saDef.multiply1p(n, a.f(c1)), sbDef.multiply1p(n, b.f(c1))); } @Override public C sum(C c1, F0> cs) { return c.f(saDef.sum(a.f(c1), () -> cs.f().map(a)), sbDef.sum(b.f(c1), () -> cs.f().map(b))); } }); } /** * Constructs a monoid from this semigroup and a zero value, which must follow the monoidal laws. * * @param zero The zero for the monoid. * @return A monoid instance that uses the given sun function and zero value. */ public Monoid monoid(A zero) { return monoidDef(this.def, zero); } /** * Constructs a semigroup from the given definition. * * @param def The definition to construct this semigroup with. * @return A semigroup from the given definition. */ public static Semigroup semigroupDef(final Definition def) { return new Semigroup<>(def); } /** * Constructs a semigroup from the given definition. * * @param def The definition to construct this semigroup with. * @return A semigroup from the given definition. */ public static Semigroup semigroupDef(final AltDefinition def) { return new Semigroup<>(def); } /** * Constructs a semigroup from the given function. * Java 8+ users: use {@link #semigroupDef(AltDefinition)} instead. * * @param sum The function to construct this semigroup with. * @return A semigroup from the given function. */ public static Semigroup semigroup(final F> sum) { return semigroupDef(sum::f); } /** * Constructs a semigroup from the given function. * Java 8+ users: use {@link #semigroupDef(Definition)} instead. * * @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<>(sum::f); } /** * A semigroup that adds integers. */ public static final Semigroup intAdditionSemigroup = intAdditionMonoid.semigroup(); /** * @deprecated Since 4.7. Due to rounding errors, addition of doubles does not comply with monoid laws */ @Deprecated public static final Semigroup doubleAdditionSemigroup = semigroupDef((d1, d2) -> d1 + d2); /** * A semigroup that multiplies integers. */ public static final Semigroup intMultiplicationSemigroup = intMultiplicationMonoid.semigroup(); /** * @deprecated Since 4.7. Due to rounding errors, addition of doubles does not comply with monoid laws */ @Deprecated public static final Semigroup doubleMultiplicationSemigroup = semigroupDef((d1, d2) -> d1 * d2); /** * A semigroup that yields the maximum of integers. */ public static final Semigroup intMaximumSemigroup = intMaxMonoid.semigroup(); /** * A semigroup that yields the minimum of integers. */ public static final Semigroup intMinimumSemigroup = intMinMonoid.semigroup(); /** * A semigroup that adds big integers. */ public static final Semigroup bigintAdditionSemigroup = bigintAdditionMonoid.semigroup(); /** * A semigroup that multiplies big integers. */ public static final Semigroup bigintMultiplicationSemigroup = semigroup(BigInteger::multiply); /** * A semigroup that yields the maximum of big integers. */ public static final Semigroup bigintMaximumSemigroup = Ord.bigintOrd.maxSemigroup(); /** * A semigroup that yields the minimum of big integers. */ public static final Semigroup bigintMinimumSemigroup = Ord.bigintOrd.minSemigroup(); /** * A semigroup that adds big decimals. */ public static final Semigroup bigdecimalAdditionSemigroup = bigdecimalAdditionMonoid.semigroup(); /** * A semigroup that multiplies big decimals. */ public static final Semigroup bigdecimalMultiplicationSemigroup = bigdecimalMultiplicationMonoid.semigroup(); /** * A semigroup that yields the maximum of big decimals. */ public static final Semigroup bigDecimalMaximumSemigroup = Ord.bigdecimalOrd.maxSemigroup(); /** * A semigroup that yields the minimum of big decimals. */ public static final Semigroup bigDecimalMinimumSemigroup = Ord.bigdecimalOrd.minSemigroup(); /** * A semigroup that multiplies natural numbers. */ public static final Semigroup naturalMultiplicationSemigroup = naturalMultiplicationMonoid.semigroup(); /** * A semigroup that adds natural numbers. */ public static final Semigroup naturalAdditionSemigroup = naturalAdditionMonoid.semigroup(); /** * A semigroup that yields the maximum of natural numbers. */ public static final Semigroup naturalMaximumSemigroup = Ord.naturalOrd.maxSemigroup(); /** * A semigroup that yields the minimum of natural numbers. */ public static final Semigroup naturalMinimumSemigroup = Ord.naturalOrd.minSemigroup(); /** * A semigroup that adds longs. */ public static final Semigroup longAdditionSemigroup = longAdditionMonoid.semigroup(); /** * A semigroup that multiplies longs. */ public static final Semigroup longMultiplicationSemigroup = longMultiplicationMonoid.semigroup(); /** * A semigroup that yields the maximum of longs. */ public static final Semigroup longMaximumSemigroup = Ord.longOrd.maxSemigroup(); /** * A semigroup that yields the minimum of longs. */ public static final Semigroup longMinimumSemigroup = Ord.longOrd.minSemigroup(); /** * A semigroup that ORs booleans. */ public static final Semigroup disjunctionSemigroup = disjunctionMonoid.semigroup(); /** * A semigroup that XORs booleans. */ public static final Semigroup exclusiveDisjunctionSemiGroup = exclusiveDisjunctionMonoid.semigroup(); /** * A semigroup that ANDs booleans. */ public static final Semigroup conjunctionSemigroup = conjunctionMonoid.semigroup(); /** * A semigroup that appends strings. */ public static final Semigroup stringSemigroup = stringMonoid.semigroup(); /** * A semigroup that appends string buffers. */ public static final Semigroup stringBufferSemigroup = stringBufferMonoid.semigroup(); /** * A semigroup that appends string builders. */ public static final Semigroup stringBuilderSemigroup = stringBuilderMonoid.semigroup(); /** * A semigroup which always uses the "first" (left-hand side) value. */ public static Semigroup firstSemigroup() { return semigroupDef(new Definition() { @Override public A append(A a1, A a2) { return a1; } @Override public F prepend(A a) { return constant(a); } @Override public A multiply1p(int n, A a) { return a; } @Override public A sum(A a, F0> as) { return a; } }); } /** * A semigroup which always uses the "last" (right-hand side) value. */ public static Semigroup lastSemigroup() { return semigroupDef(new Definition() { @Override public A append(A a1, A a2) { return a2; } @Override public F prepend(A a) { return identity(); } @Override public A multiply1p(int n, A a) { return a; } }); } /** * A semigroup for functions. * * @param sb The smeigroup for the codomain. * @return A semigroup for functions. */ public static Semigroup> functionSemigroup(final Semigroup sb) { Definition sbDef = sb.def; return semigroupDef((a1, a2) -> a -> sbDef.append(a1.f(a), a2.f(a))); } /** * A semigroup for lists. * * @return A semigroup for lists. */ public static Semigroup> listSemigroup() { return Monoid.listMonoid().semigroup(); } /** * A semigroup for non-empty lists. * * @return A semigroup for non-empty lists. */ public static Semigroup> nonEmptyListSemigroup() { return semigroupDef(new Definition>() { @Override public NonEmptyList append(NonEmptyList a1, NonEmptyList a2) { return a1.append(a1); } @Override public NonEmptyList sum(NonEmptyList nea, F0>> neas) { List tail = neas.f().map(nel -> listDList(nel.toList())).foldLeft(DList::append, DList.nil()).run(); return nea.append(tail); } }); } /** * A semigroup for optional values. * @deprecated since 4.7. Use {@link #firstOptionSemigroup()}. * ** @return A semigroup for optional values. */ public static Semigroup> optionSemigroup() { return firstOptionSemigroup(); } /** * A semigroup for optional values that take the first available value. * * @return A semigroup for optional values that take the first available value. */ public static Semigroup> firstOptionSemigroup() { return Monoid.firstOptionMonoid().semigroup(); } /** * A semigroup for optional values that take the last available value. * * @return A semigroup for optional values that take the last available value. */ public static Semigroup> lastOptionSemigroup() { return Monoid.lastOptionMonoid().semigroup(); } /** * A semigroup for streams. * * @return A semigroup for streams. */ public static Semigroup> streamSemigroup() { return Monoid.streamMonoid().semigroup(); } /** * A semigroup for arrays. * * @return A semigroup for arrays. */ public static Semigroup> arraySemigroup() { return Monoid.arrayMonoid().semigroup(); } /** * A lazy semigroup for unary products. * * @param sa A semigroup for the product's type. * @return A semigroup for unary products. */ public static Semigroup> p1Semigroup(final Semigroup sa) { Definition def = sa.def; return semigroupDef(new Definition>() { @Override public P1 append(P1 a1, P1 a2) { return P.lazy(() -> def.append(a1._1(), a2._1())); } @Override public P1 multiply1p(int n, P1 ap1) { return P.lazy(() -> def.multiply1p(n, ap1._1())); } @Override public P1 sum(P1 ap1, F0>> as) { return P.lazy(() -> def.sum(ap1._1(), () -> as.f().map(P1.__1()))); } }); } /** * A lazy semigroup for binary products. * * @param sa A semigroup for the product's first type. * @param sb A semigroup for the product's second type. * @return A semigroup for binary products. */ public static Semigroup> p2Semigroup(final Semigroup sa, final Semigroup sb) { return semigroupDef((a1, a2) -> P.lazy(() -> sa.sum(a1._1(), a2._1()), () -> sb.sum(a1._2(), a2._2()))); } /** * A semigroup for IO values. */ public static Semigroup> ioSemigroup(final Semigroup sa) { Definition def = sa.def; return semigroupDef((a1, a2) -> () -> def.append(a1.run(), a2.run())); } /** * A semigroup for the Unit value. */ public static final Semigroup unitSemigroup = unitMonoid.semigroup(); /** * A semigroup for sets. * * @return a semigroup for sets. */ public static Semigroup> setSemigroup() { return semigroupDef(Set::union); } }