Package io.atlassian.fugue
Interface Monoid<A>
-
- All Superinterfaces:
Semigroup<A>
public interface Monoid<A> extends Semigroup<A>
A Monoid is an algebraic structure consisting of an associative binary operation across the values of a given type (a monoid is aSemigroup) 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)andmultiply(int, Object)can be overriden for performance reason, especially ifsum(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:
Semigroup
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static <A,B>
Monoid<Pair<A,B>>compose(Monoid<A> ma, Monoid<B> mb)Composes a monoid with another.static <A> Monoid<A>dual(Monoid<A> monoid)Return the dual Monoid.default Aintersperse(java.lang.Iterable<? extends A> as, A a)Intersperses the given value between each two elements of the collection, and sums the result.default Amultiply(int n, A a)Returns a value summedntimes (a + a + ...default Amultiply1p(int n, A a)Returns a value summedn + 1times (a + a + ...default Asum(java.lang.Iterable<A> as)Sums the given values.default AsumNonEmpty(A head, java.lang.Iterable<A> tail)Reduce a 'non-empty' Iterable withSemigroup.append(Object, Object)Azero()The identity element value for this monoid.
-
-
-
Method Detail
-
zero
A zero()
The identity element value for this monoid.- Returns:
- The identity element for this monoid.
-
sum
default A sum(java.lang.Iterable<A> as)
Sums the given values.- Parameters:
as- The values to sum.- Returns:
- The sum of the given values.
-
multiply
default A multiply(int n, A a)
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
default A intersperse(java.lang.Iterable<? extends A> as, A a)
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
default A sumNonEmpty(A head, java.lang.Iterable<A> tail)
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
default A multiply1p(int n, A a)
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
static <A,B> Monoid<Pair<A,B>> compose(Monoid<A> ma, Monoid<B> mb)
Composes a monoid with another.
-
-