Interface Semigroup<A>

All Known Subinterfaces:
Monoid<A>

public interface Semigroup<A>
A Semigroup is an algebraic structure consisting of an associative binary operation across the values of a given type (the Semigroup type argument). Implementations must satisfy the law of associativity:
  • Associativity; forall x y z. append(append(x, y), z) == append(x, append(y, z))
Methods sumNonEmpty(Object, Iterable) and multiply1p(int, Object) can be overriden for performance reason, especially if sumNonEmpty(Object, Iterable) can be implemented to not require evaluation of the whole iterable.
Since:
3.1
See Also:
  • Method Details

    • append

      A append(A a1, A a2)
      Combine the two given arguments.
      Parameters:
      a1 - left value to combine
      a2 - right value to combine
      Returns:
      the combination of the left and right value.
    • sumNonEmpty

      default A sumNonEmpty(A head, Iterable<A> tail)
      Reduce a 'non-empty' Iterable with append(Object, Object)
      Parameters:
      head - the head of the 'non-empty' Iterable
      tail - the tail (maybe an empty Iterable).
      Returns:
      the sum of all elements.
    • multiply1p

      default A multiply1p(int n, A a)
      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 append(Object, Object).
      Parameters:
      n - multiplier
      a - the value to be reapeatly summed n + 1 times
      Returns:
      a summed n times. If n <= 0, returns zero()
    • compose

      static <A, B> Semigroup<Pair<A,B>> compose(Semigroup<A> sa, Semigroup<B> sb)
      Composes a semigroup with another.
    • dual

      static <A> Semigroup<A> dual(Semigroup<A> semigroup)
      Return the dual Semigroup of a semigroup
      Parameters:
      semigroup - base semigroup to create the dual from
      Returns:
      a semigroup appending in reverse order