Package fj

Class Monoid<A>

java.lang.Object
fj.Monoid<A>

public final class Monoid<A> extends Object
A monoid abstraction to be defined across types of the given type argument. Implementations must follow the monoidal laws:
  • Left Identity; forall x. sum(zero(), x) == x
  • Right Identity; forall x. sum(x, zero()) == x
  • Associativity; forall x y z. sum(sum(x, y), z) == sum(x, sum(y, z))
  • Field Details

    • def

      private final Monoid.Definition<A> def
    • intAdditionMonoid

      public static final Monoid<Integer> intAdditionMonoid
      A monoid that adds integers.
    • intMultiplicationMonoid

      public static final Monoid<Integer> intMultiplicationMonoid
      A monoid that multiplies integers.
    • bigintAdditionMonoid

      public static final Monoid<BigInteger> bigintAdditionMonoid
      A monoid that adds big integers.
    • bigintMultiplicationMonoid

      public static final Monoid<BigInteger> bigintMultiplicationMonoid
      A monoid that multiplies big integers.
    • bigdecimalAdditionMonoid

      public static final Monoid<BigDecimal> bigdecimalAdditionMonoid
      A monoid that adds big decimals.
    • bigdecimalMultiplicationMonoid

      public static final Monoid<BigDecimal> bigdecimalMultiplicationMonoid
      A monoid that multiplies big decimals.
    • naturalAdditionMonoid

      public static final Monoid<Natural> naturalAdditionMonoid
      A monoid that adds natural numbers.
    • naturalMultiplicationMonoid

      public static final Monoid<Natural> naturalMultiplicationMonoid
      A monoid that multiplies natural numbers.
    • longAdditionMonoid

      public static final Monoid<Long> longAdditionMonoid
      A monoid that adds longs.
    • longMultiplicationMonoid

      public static final Monoid<Long> longMultiplicationMonoid
      A monoid that multiplies longs.
    • disjunctionMonoid

      public static final Monoid<Boolean> disjunctionMonoid
      A monoid that ORs booleans.
    • exclusiveDisjunctionMonoid

      public static final Monoid<Boolean> exclusiveDisjunctionMonoid
      A monoid that XORs booleans.
    • conjunctionMonoid

      public static final Monoid<Boolean> conjunctionMonoid
      A monoid that ANDs booleans.
    • stringMonoid

      public static final Monoid<String> stringMonoid
      A monoid that appends strings.
    • stringBufferMonoid

      public static final Monoid<StringBuffer> stringBufferMonoid
      A monoid that appends string buffers.
    • stringBuilderMonoid

      public static final Monoid<StringBuilder> stringBuilderMonoid
      A monoid that appends string builders.
    • intMaxMonoid

      public static final Monoid<Integer> intMaxMonoid
      A monoid for the maximum of two integers.
    • intMinMonoid

      public static final Monoid<Integer> intMinMonoid
      A monoid for the minimum of two integers.
    • unitMonoid

      public static final Monoid<Unit> unitMonoid
      A monoid for the Unit value.
  • Constructor Details

  • Method Details

    • compose

      public <B> Monoid<P2<A,B>> compose(Monoid<B> m)
      Composes this monoid with another.
    • semigroup

      public Semigroup<A> semigroup()
      Returns a semigroup projection of this monoid.
      Returns:
      A semigroup projection of this monoid.
    • xmap

      public <B> Monoid<B> xmap(F<A,B> f, F<B,A> g)
      Maps the given functions across this monoid as an invariant functor.
      Parameters:
      f - The covariant map.
      g - The contra-variant map.
      Returns:
      A new monoid.
    • compose

      public <B, C> Monoid<C> compose(Monoid<B> mb, F<C,A> a, F<C,B> b, F2<A,B,C> c)
    • sum

      public A sum(A a1, A a2)
      Sums the two given arguments.
      Parameters:
      a1 - A value to sum with another.
      a2 - A value to sum with another.
      Returns:
      The of the two given arguments.
    • sum

      public F<A,A> sum(A a1)
      Returns a function that sums the given value according to this monoid.
      Parameters:
      a1 - The value to sum.
      Returns:
      A function that sums the given value according to this monoid.
    • sum

      public F<A,F<A,A>> sum()
      Returns a function that sums according to this monoid.
      Returns:
      A function that sums according to this monoid.
    • zero

      public A zero()
      The zero value for this monoid.
      Returns:
      The zero value for this monoid.
    • multiply

      public A multiply(int n, A a)
      Returns a value summed n times (a + a + ... + a). The default definition uses peasant multiplication, exploiting associativity to only require O(log n) uses of sum(Object, Object).
      Parameters:
      n - multiplier
      a - the value to be reapeatly summed
      Returns:
      a summed n times. If n <= 0, returns zero()
    • sumRight

      public A sumRight(List<A> as)
      Sums the given values with right-fold.
      Parameters:
      as - The values to sum.
      Returns:
      The sum of the given values.
    • sumRight

      public A sumRight(Stream<A> as)
      Sums the given values with right-fold.
      Parameters:
      as - The values to sum.
      Returns:
      The sum of the given values.
    • sumLeft

      public A sumLeft(List<A> as)
      Sums the given values with left-fold.
      Parameters:
      as - The values to sum.
      Returns:
      The sum of the given values.
    • sumLeft

      public A sumLeft(Stream<A> as)
      Sums the given values with left-fold.
      Parameters:
      as - The values to sum.
      Returns:
      The sum of the given values.
    • sumLeft

      public F<List<A>,A> sumLeft()
      Returns a function that sums the given values with left-fold.
      Returns:
      a function that sums the given values with left-fold.
    • sumRight

      public F<List<A>,A> sumRight()
      Returns a function that sums the given values with right-fold.
      Returns:
      a function that sums the given values with right-fold.
    • sumLeftS

      public F<Stream<A>,A> sumLeftS()
      Returns a function that sums the given values with left-fold.
      Returns:
      a function that sums the given values with left-fold.
    • join

      public A join(Iterable<A> as, A a)
      Intersperses the given value between each two elements of the iterable, and sums the result.
      Parameters:
      as - An iterable of values to sum.
      a - The value to intersperse between values of the given iterable.
      Returns:
      The sum of the given values and the interspersed value.
    • dual

      public Monoid<A> dual()
      Swaps the arguments when summing.
    • monoidDef

      public static <A> Monoid<A> monoidDef(Monoid.Definition<A> def)
      Constructs a monoid from the given definition, which must follow the monoidal laws.
      Parameters:
      def - The definition for the monoid.
      Returns:
      A monoid instance that uses the given sun function and zero value.
    • monoidDef

      public static <A> Monoid<A> monoidDef(Monoid.AltDefinition<A> def)
      Constructs a monoid from the given definition, which must follow the monoidal laws.
      Parameters:
      def - The definition for the monoid.
      Returns:
      A monoid instance that uses the given sun function and zero value.
    • monoidDef

      public static <A> Monoid<A> monoidDef(Semigroup.Definition<A> s, A zero)
      Constructs a monoid from the given semigroup definition and zero value, which must follow the monoidal laws.
      Parameters:
      s - The semigroup definition for the monoid.
      zero - The zero for the monoid.
      Returns:
      A monoid instance that uses the given sun function and zero value.
    • monoidDef

      public static <A> Monoid<A> monoidDef(Semigroup.AltDefinition<A> s, A zero)
      Constructs a monoid from the given semigroup definition and zero value, which must follow the monoidal laws.
      Parameters:
      s - The semigroup definition for the monoid.
      zero - The zero for the monoid.
      Returns:
      A monoid instance that uses the given sun function and zero value.
    • monoid

      public static <A> Monoid<A> monoid(F<A,F<A,A>> sum, A zero)
      Constructs a monoid from the given sum function and zero value, which must follow the monoidal laws. Java 8+ users: use monoidDef(Semigroup.Definition, Object) instead.
      Parameters:
      sum - The sum function for the monoid.
      zero - The zero for the monoid.
      Returns:
      A monoid instance that uses the given sun function and zero value.
    • monoid

      public static <A> Monoid<A> monoid(F2<A,A,A> sum, A zero)
      Constructs a monoid from the given sum function and zero value, which must follow the monoidal laws. Java 8+ users: use monoidDef(Semigroup.Definition, Object) instead.
      Parameters:
      sum - The sum function for the monoid.
      zero - The zero for the monoid.
      Returns:
      A monoid instance that uses the given sun function and zero value.
    • functionMonoid

      public static <A, B> Monoid<F<A,B>> functionMonoid(Monoid<B> mb)
      A monoid for functions.
      Parameters:
      mb - The monoid for the function codomain.
      Returns:
      A monoid for functions.
    • listMonoid

      public static <A> Monoid<List<A>> listMonoid()
      A monoid for lists.
      Returns:
      A monoid for lists.
    • optionMonoid

      public static <A> Monoid<Option<A>> optionMonoid(Semigroup<A> aSemigroup)
      Lift a Semigroup<A> for A to a Monoid<Option<A>>, using Option.none() as zero.
      Returns:
      A monoid for option.
    • firstOptionMonoid

      public static <A> Monoid<Option<A>> firstOptionMonoid()
      A monoid for options that take the first available value.
      Returns:
      A monoid for options that take the first available value.
    • lastOptionMonoid

      public static <A> Monoid<Option<A>> lastOptionMonoid()
      A monoid for options that take the last available value.
      Returns:
      A monoid for options that take the last available value.
    • streamMonoid

      public static <A> Monoid<Stream<A>> streamMonoid()
      A monoid for streams.
      Returns:
      A monoid for streams.
    • arrayMonoid

      public static <A> Monoid<Array<A>> arrayMonoid()
      A monoid for arrays.
      Returns:
      A monoid for arrays.
    • ioMonoid

      public static <A> Monoid<IO<A>> ioMonoid(Monoid<A> ma)
      A monoid for IO values.
    • setMonoid

      public static <A> Monoid<Set<A>> setMonoid(Ord<A> o)
      A union monoid for sets.
      Parameters:
      o - An order for set elements.
      Returns:
      A monoid for sets whose elements have the given order.
    • setIntersectionMonoid

      public static <A> Monoid<Set<A>> setIntersectionMonoid(Bounded<A> bounded, Enumerator<A> enumerator)
      A intersection monoid for sets.
      Parameters:
      bounded - A bound for all possible elements
      enumerator - An enumerator for all possible elements
      Returns:
      A monoid for sets whose elements have the given order.