A
- A type.public interface Monoid<A> extends Semigroup<A>
A Monoid is a Semigroup (types with an associative binary operation) that has an
identity element zero
.
Given a type A
, instances of Monoid should satisfy the following laws:
combine(combine(x,y),z) == combine(x,combine(y,z))
for any x,y,z
of type
A
.combine(zero(), x) == x == combine(x, zero())
for any x
of type A
.Example: String is a Monoid with zero ""
(empty String) and String concatenation
+
as combine operation.
Please note that some types can be viewed as a monoid in more than one way, e.g. both addition and multiplication on numbers.
Folding:Modifier and Type | Method and Description |
---|---|
static <A> Monoid<Function<A,A>> |
endoMonoid()
The monoid of endomorphisms under composition.
|
static <T> T |
fold(Monoid<T> monoid,
Foldable<T> foldable)
Folds the elements of
Foldable from the left, starting with monoid.zero()
and successively calling monoid::combine . |
static <T> T |
foldLeft(Monoid<T> monoid,
Foldable<T> foldable)
Folds the elements of
Foldable from the left, starting with monoid.zero()
and successively calling monoid::combine . |
static <T,U> U |
foldMap(Monoid<U> monoid,
Foldable<T> foldable,
Function<? super T,? extends U> mapper)
Maps this elements to a
Monoid and applies foldLeft , starting with monoid.zero() : |
static <T> T |
foldRight(Monoid<T> monoid,
Foldable<T> foldable)
Folds this elements from the right, starting with
monoid.zero() and successively calling monoid::combine . |
static <A> Monoid<A> |
of(A zero,
Semigroup<A> semigroup)
Factory method for monoids, taking a zero and a Semigroup.
|
A |
zero()
The unique neutral element regarding Semigroup.combine(Object, Object).
|
static <A> Monoid<A> of(A zero, Semigroup<A> semigroup)
A
- Value typezero
- The zero of the Monoid.semigroup
- The associative binary operation of the Monoid. Please note that
Semigroup is a FunctionalInterface.NullPointerException
- if semigroup
is nullstatic <A> Monoid<Function<A,A>> endoMonoid()
A
- Value typeA zero()
static <T> T fold(Monoid<T> monoid, Foldable<T> foldable)
Foldable
from the left, starting with monoid.zero()
and successively calling monoid::combine
.T
- type of the foldable elementsmonoid
- A monoid, providing a zero
and a combine
function.foldable
- A foldableNullPointerException
- if monoid
or foldable
is nullstatic <T> T foldLeft(Monoid<T> monoid, Foldable<T> foldable)
Foldable
from the left, starting with monoid.zero()
and successively calling monoid::combine
.T
- type of the foldable elementsmonoid
- A monoid, providing a zero
and a combine
function.foldable
- A foldableNullPointerException
- if monoid
is nullstatic <T,U> U foldMap(Monoid<U> monoid, Foldable<T> foldable, Function<? super T,? extends U> mapper)
Monoid
and applies foldLeft
, starting with monoid.zero()
:
foldLeft(monoid.zero(), (ys, x) -> monoid.combine(ys, mapper.apply(x)));
T
- type of the foldable elementsU
- Component type of the given monoid.monoid
- A Monoidfoldable
- A foldablemapper
- A mapperNullPointerException
- if monoid
or mapper
is nullstatic <T> T foldRight(Monoid<T> monoid, Foldable<T> foldable)
monoid.zero()
and successively calling monoid::combine
.T
- type of the foldable elementsmonoid
- A monoid, providing a zero
and a combine
function.foldable
- A foldableNullPointerException
- if monoid
is nullCopyright © 2017. All Rights Reserved.