Package fj
Class Monoid<A>
java.lang.Object
fj.Monoid<A>
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))
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Primitives functions of Monoid: alternative minimal definition and overridable methods.static interface
Primitives functions of Monoid: minimal definition and overridable methods. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Monoid
<BigDecimal> A monoid that adds big decimals.static final Monoid
<BigDecimal> A monoid that multiplies big decimals.static final Monoid
<BigInteger> A monoid that adds big integers.static final Monoid
<BigInteger> A monoid that multiplies big integers.A monoid that ANDs booleans.private final Monoid.Definition
<A> A monoid that ORs booleans.A monoid that XORs booleans.A monoid that adds integers.A monoid for the maximum of two integers.A monoid for the minimum of two integers.A monoid that multiplies integers.A monoid that adds longs.A monoid that multiplies longs.A monoid that adds natural numbers.A monoid that multiplies natural numbers.static final Monoid
<StringBuffer> A monoid that appends string buffers.static final Monoid
<StringBuilder> A monoid that appends string builders.A monoid that appends strings.A monoid for the Unit value. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionA monoid for arrays.Composes this monoid with another.<B,
C> Monoid <C> dual()
Swaps the arguments when summing.A monoid for options that take the first available value.functionMonoid
(Monoid<B> mb) A monoid for functions.A monoid for IO values.Intersperses the given value between each two elements of the iterable, and sums the result.A monoid for options that take the last available value.A monoid for lists.static <A> Monoid
<A> Constructs a monoid from the given sum function and zero value, which must follow the monoidal laws.static <A> Monoid
<A> Constructs a monoid from the given sum function and zero value, which must follow the monoidal laws.static <A> Monoid
<A> monoidDef
(Monoid.AltDefinition<A> def) Constructs a monoid from the given definition, which must follow the monoidal laws.static <A> Monoid
<A> monoidDef
(Monoid.Definition<A> def) Constructs a monoid from the given definition, which must follow the monoidal laws.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.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.Returns a value summedn
times (a + a + ...
optionMonoid
(Semigroup<A> aSemigroup) Lift aSemigroup<A>
for A to aMonoid<Option<A>>
, using Option.none() as zero.Returns a semigroup projection of this monoid.setIntersectionMonoid
(Bounded<A> bounded, Enumerator<A> enumerator) A intersection monoid for sets.A union monoid for sets.A monoid for streams.sum()
Returns a function that sums according to this monoid.Returns a function that sums the given value according to this monoid.Sums the two given arguments.sumLeft()
Returns a function that sums the given values with left-fold.Sums the given values with left-fold.Sums the given values with left-fold.sumLeftS()
Returns a function that sums the given values with left-fold.sumRight()
Returns a function that sums the given values with right-fold.Sums the given values with right-fold.Sums the given values with right-fold.<B> Monoid
<B> Maps the given functions across this monoid as an invariant functor.zero()
The zero value for this monoid.
-
Field Details
-
def
-
intAdditionMonoid
A monoid that adds integers. -
intMultiplicationMonoid
A monoid that multiplies integers. -
bigintAdditionMonoid
A monoid that adds big integers. -
bigintMultiplicationMonoid
A monoid that multiplies big integers. -
bigdecimalAdditionMonoid
A monoid that adds big decimals. -
bigdecimalMultiplicationMonoid
A monoid that multiplies big decimals. -
naturalAdditionMonoid
A monoid that adds natural numbers. -
naturalMultiplicationMonoid
A monoid that multiplies natural numbers. -
longAdditionMonoid
A monoid that adds longs. -
longMultiplicationMonoid
A monoid that multiplies longs. -
disjunctionMonoid
A monoid that ORs booleans. -
exclusiveDisjunctionMonoid
A monoid that XORs booleans. -
conjunctionMonoid
A monoid that ANDs booleans. -
stringMonoid
A monoid that appends strings. -
stringBufferMonoid
A monoid that appends string buffers. -
stringBuilderMonoid
A monoid that appends string builders. -
intMaxMonoid
A monoid for the maximum of two integers. -
intMinMonoid
A monoid for the minimum of two integers. -
unitMonoid
A monoid for the Unit value.
-
-
Constructor Details
-
Monoid
-
-
Method Details
-
compose
Composes this monoid with another. -
semigroup
Returns a semigroup projection of this monoid.- Returns:
- A semigroup projection of this monoid.
-
xmap
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
-
sum
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
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
Returns a function that sums according to this monoid.- Returns:
- A function that sums according to this monoid.
-
zero
The zero value for this monoid.- Returns:
- The zero value for this monoid.
-
multiply
Returns a value summedn
times (a + a + ... + a
). The default definition uses peasant multiplication, exploiting associativity to only requireO(log n)
uses ofsum(Object, Object)
.- Parameters:
n
- multipliera
- the value to be reapeatly summed- Returns:
a
summedn
times. Ifn <= 0
, returnszero()
-
sumRight
Sums the given values with right-fold.- Parameters:
as
- The values to sum.- Returns:
- The sum of the given values.
-
sumRight
Sums the given values with right-fold.- Parameters:
as
- The values to sum.- Returns:
- The sum of the given values.
-
sumLeft
Sums the given values with left-fold.- Parameters:
as
- The values to sum.- Returns:
- The sum of the given values.
-
sumLeft
Sums the given values with left-fold.- Parameters:
as
- The values to sum.- Returns:
- The sum of the given values.
-
sumLeft
Returns a function that sums the given values with left-fold.- Returns:
- a function that sums the given values with left-fold.
-
sumRight
Returns a function that sums the given values with right-fold.- Returns:
- a function that sums the given values with right-fold.
-
sumLeftS
Returns a function that sums the given values with left-fold.- Returns:
- a function that sums the given values with left-fold.
-
join
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
Swaps the arguments when summing. -
monoidDef
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
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
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
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
Constructs a monoid from the given sum function and zero value, which must follow the monoidal laws. Java 8+ users: usemonoidDef(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
Constructs a monoid from the given sum function and zero value, which must follow the monoidal laws. Java 8+ users: usemonoidDef(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
A monoid for functions.- Parameters:
mb
- The monoid for the function codomain.- Returns:
- A monoid for functions.
-
listMonoid
A monoid for lists.- Returns:
- A monoid for lists.
-
optionMonoid
Lift aSemigroup<A>
for A to aMonoid<Option<A>>
, using Option.none() as zero.- Returns:
- A monoid for option.
-
firstOptionMonoid
A monoid for options that take the first available value.- Returns:
- A monoid for options that take the first available value.
-
lastOptionMonoid
A monoid for options that take the last available value.- Returns:
- A monoid for options that take the last available value.
-
streamMonoid
A monoid for streams.- Returns:
- A monoid for streams.
-
arrayMonoid
A monoid for arrays.- Returns:
- A monoid for arrays.
-
ioMonoid
A monoid for IO values. -
setMonoid
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 elementsenumerator
- An enumerator for all possible elements- Returns:
- A monoid for sets whose elements have the given order.
-