Interface Monoid<A>
- All Superinterfaces:
Semigroup<A>
A Monoid is an algebraic structure consisting of an associative binary
operation across the values of a given type (a monoid is a
Semigroup)
and an identity element for this operation. Implementations must follow the
monoidal laws:
- Left Identity; forall x. append(zero(), x) == x
- Right Identity; forall x. append(x, zero()) == x
- Associativity; forall x y z. append(append(x, y), z) == append(x, append(y, z))
sum(Iterable) and multiply(int, Object) can be
overriden for performance reason, especially if sum(Iterable) can be
implemented to not require evaluation of the whole iterable. All other
default methods should not be overriden.- Since:
- 3.1
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionComposes a monoid with another.static <A> Monoid<A> Return the dual Monoid.default Aintersperse(Iterable<? extends A> as, A a) Intersperses the given value between each two elements of the collection, and sums the result.default AReturns a value summedntimes (a + a + ...default Amultiply1p(int n, A a) Returns a value summedn + 1times (a + a + ...default ASums the given values.default AsumNonEmpty(A head, Iterable<A> tail) Reduce a 'non-empty' Iterable withSemigroup.append(Object, Object)zero()The identity element value for this monoid.
-
Method Details
-
zero
-
sum
-
multiply
Returns a value summedntimes (a + a + ... + a). The default definition uses peasant multiplication, exploiting associativity to only require `O(log n)` uses ofSemigroup.append(Object, Object).- Parameters:
n- multipliera- the value to be reapeatly summed- Returns:
asummedntimes. Ifn <= 0, returnszero()
-
intersperse
Intersperses the given value between each two elements of the collection, and sums the result.- Parameters:
as- An iterable of values.a- The value to intersperse between values of the given iterable.- Returns:
- The sum of the given values and the interspersed value.
-
sumNonEmpty
Description copied from interface:SemigroupReduce a 'non-empty' Iterable withSemigroup.append(Object, Object)- Specified by:
sumNonEmptyin interfaceSemigroup<A>- Parameters:
head- the head of the 'non-empty' Iterabletail- the tail (maybe an empty Iterable).- Returns:
- the sum of all elements.
-
multiply1p
Description copied from interface:SemigroupReturns a value summedn + 1times (a + a + ... + a) The default definition uses peasant multiplication, exploiting associativity to only require `O(log n)` uses ofSemigroup.append(Object, Object).- Specified by:
multiply1pin interfaceSemigroup<A>- Parameters:
n- multipliera- the value to be reapeatly summed n + 1 times- Returns:
asummedntimes. Ifn <= 0, returnszero()
-
compose
-
dual
-