Package fj
Class Monoid<A>
- java.lang.Object
-
- fj.Monoid<A>
-
public final class Monoid<A> extends java.lang.Object
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 Classes Modifier and Type Class Description static interface
Monoid.AltDefinition<A>
Primitives functions of Monoid: alternative minimal definition and overridable methods.static interface
Monoid.Definition<A>
Primitives functions of Monoid: minimal definition and overridable methods.
-
Field Summary
Fields Modifier and Type Field Description static Monoid<java.math.BigDecimal>
bigdecimalAdditionMonoid
A monoid that adds big decimals.static Monoid<java.math.BigDecimal>
bigdecimalMultiplicationMonoid
A monoid that multiplies big decimals.static Monoid<java.math.BigInteger>
bigintAdditionMonoid
A monoid that adds big integers.static Monoid<java.math.BigInteger>
bigintMultiplicationMonoid
A monoid that multiplies big integers.static Monoid<java.lang.Boolean>
conjunctionMonoid
A monoid that ANDs booleans.private Monoid.Definition<A>
def
static Monoid<java.lang.Boolean>
disjunctionMonoid
A monoid that ORs booleans.static Monoid<java.lang.Boolean>
exclusiveDisjunctionMonoid
A monoid that XORs booleans.static Monoid<java.lang.Integer>
intAdditionMonoid
A monoid that adds integers.static Monoid<java.lang.Integer>
intMaxMonoid
A monoid for the maximum of two integers.static Monoid<java.lang.Integer>
intMinMonoid
A monoid for the minimum of two integers.static Monoid<java.lang.Integer>
intMultiplicationMonoid
A monoid that multiplies integers.static Monoid<java.lang.Long>
longAdditionMonoid
A monoid that adds longs.static Monoid<java.lang.Long>
longMultiplicationMonoid
A monoid that multiplies longs.static Monoid<Natural>
naturalAdditionMonoid
A monoid that adds natural numbers.static Monoid<Natural>
naturalMultiplicationMonoid
A monoid that multiplies natural numbers.static Monoid<java.lang.StringBuffer>
stringBufferMonoid
A monoid that appends string buffers.static Monoid<java.lang.StringBuilder>
stringBuilderMonoid
A monoid that appends string builders.static Monoid<java.lang.String>
stringMonoid
A monoid that appends strings.static Monoid<Unit>
unitMonoid
A monoid for the Unit value.
-
Constructor Summary
Constructors Modifier Constructor Description private
Monoid(Monoid.Definition<A> def)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <A> Monoid<Array<A>>
arrayMonoid()
A monoid for arrays.<B> Monoid<P2<A,B>>
compose(Monoid<B> m)
Composes this monoid with another.<B,C>
Monoid<C>compose(Monoid<B> mb, F<C,A> a, F<C,B> b, F2<A,B,C> c)
Monoid<A>
dual()
Swaps the arguments when summing.static <A> Monoid<Option<A>>
firstOptionMonoid()
A monoid for options that take the first available value.static <A,B>
Monoid<F<A,B>>functionMonoid(Monoid<B> mb)
A monoid for functions.static <A> Monoid<IO<A>>
ioMonoid(Monoid<A> ma)
A monoid for IO values.A
join(java.lang.Iterable<A> as, A a)
Intersperses the given value between each two elements of the iterable, and sums the result.static <A> Monoid<Option<A>>
lastOptionMonoid()
A monoid for options that take the last available value.static <A> Monoid<List<A>>
listMonoid()
A monoid for lists.static <A> Monoid<A>
monoid(F<A,F<A,A>> sum, A zero)
Constructs a monoid from the given sum function and zero value, which must follow the monoidal laws.static <A> Monoid<A>
monoid(F2<A,A,A> sum, A zero)
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.A
multiply(int n, A a)
Returns a value summedn
times (a + a + ...
static <A> Monoid<Option<A>>
optionMonoid(Semigroup<A> aSemigroup)
Lift aSemigroup<A>
for A to aMonoid<Option<A>>
, using Option.none() as zero.Semigroup<A>
semigroup()
Returns a semigroup projection of this monoid.static <A> Monoid<Set<A>>
setIntersectionMonoid(Bounded<A> bounded, Enumerator<A> enumerator)
A intersection monoid for sets.static <A> Monoid<Set<A>>
setMonoid(Ord<A> o)
A union monoid for sets.static <A> Monoid<Stream<A>>
streamMonoid()
A monoid for streams.F<A,F<A,A>>
sum()
Returns a function that sums according to this monoid.F<A,A>
sum(A a1)
Returns a function that sums the given value according to this monoid.A
sum(A a1, A a2)
Sums the two given arguments.F<List<A>,A>
sumLeft()
Returns a function that sums the given values with left-fold.A
sumLeft(List<A> as)
Sums the given values with left-fold.A
sumLeft(Stream<A> as)
Sums the given values with left-fold.F<Stream<A>,A>
sumLeftS()
Returns a function that sums the given values with left-fold.F<List<A>,A>
sumRight()
Returns a function that sums the given values with right-fold.A
sumRight(List<A> as)
Sums the given values with right-fold.A
sumRight(Stream<A> as)
Sums the given values with right-fold.<B> Monoid<B>
xmap(F<A,B> f, F<B,A> g)
Maps the given functions across this monoid as an invariant functor.A
zero()
The zero value for this monoid.
-
-
-
Field Detail
-
def
private final Monoid.Definition<A> def
-
intAdditionMonoid
public static final Monoid<java.lang.Integer> intAdditionMonoid
A monoid that adds integers.
-
intMultiplicationMonoid
public static final Monoid<java.lang.Integer> intMultiplicationMonoid
A monoid that multiplies integers.
-
bigintAdditionMonoid
public static final Monoid<java.math.BigInteger> bigintAdditionMonoid
A monoid that adds big integers.
-
bigintMultiplicationMonoid
public static final Monoid<java.math.BigInteger> bigintMultiplicationMonoid
A monoid that multiplies big integers.
-
bigdecimalAdditionMonoid
public static final Monoid<java.math.BigDecimal> bigdecimalAdditionMonoid
A monoid that adds big decimals.
-
bigdecimalMultiplicationMonoid
public static final Monoid<java.math.BigDecimal> bigdecimalMultiplicationMonoid
A monoid that multiplies big decimals.
-
naturalAdditionMonoid
public static final Monoid<Natural> naturalAdditionMonoid
A monoid that adds natural numbers.
-
naturalMultiplicationMonoid
public static final Monoid<Natural> naturalMultiplicationMonoid
A monoid that multiplies natural numbers.
-
longAdditionMonoid
public static final Monoid<java.lang.Long> longAdditionMonoid
A monoid that adds longs.
-
longMultiplicationMonoid
public static final Monoid<java.lang.Long> longMultiplicationMonoid
A monoid that multiplies longs.
-
disjunctionMonoid
public static final Monoid<java.lang.Boolean> disjunctionMonoid
A monoid that ORs booleans.
-
exclusiveDisjunctionMonoid
public static final Monoid<java.lang.Boolean> exclusiveDisjunctionMonoid
A monoid that XORs booleans.
-
conjunctionMonoid
public static final Monoid<java.lang.Boolean> conjunctionMonoid
A monoid that ANDs booleans.
-
stringMonoid
public static final Monoid<java.lang.String> stringMonoid
A monoid that appends strings.
-
stringBufferMonoid
public static final Monoid<java.lang.StringBuffer> stringBufferMonoid
A monoid that appends string buffers.
-
stringBuilderMonoid
public static final Monoid<java.lang.StringBuilder> stringBuilderMonoid
A monoid that appends string builders.
-
intMaxMonoid
public static final Monoid<java.lang.Integer> intMaxMonoid
A monoid for the maximum of two integers.
-
intMinMonoid
public static final Monoid<java.lang.Integer> intMinMonoid
A monoid for the minimum of two integers.
-
-
Constructor Detail
-
Monoid
private Monoid(Monoid.Definition<A> def)
-
-
Method Detail
-
semigroup
public Semigroup<A> semigroup()
Returns a semigroup projection of this monoid.- Returns:
- A semigroup projection of this monoid.
-
xmap
public <B> Monoid<B> xmap(F<A,B> f, F<B,A> g)
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.
-
sum
public A sum(A a1, A a2)
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
public F<A,A> sum(A a1)
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
public F<A,F<A,A>> sum()
Returns a function that sums according to this monoid.- Returns:
- A function that sums according to this monoid.
-
zero
public A zero()
The zero value for this monoid.- Returns:
- The zero value for this monoid.
-
multiply
public A multiply(int n, A a)
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
public A sumRight(List<A> as)
Sums the given values with right-fold.- Parameters:
as
- The values to sum.- Returns:
- The sum of the given values.
-
sumRight
public A sumRight(Stream<A> as)
Sums the given values with right-fold.- Parameters:
as
- The values to sum.- Returns:
- The sum of the given values.
-
sumLeft
public A sumLeft(List<A> as)
Sums the given values with left-fold.- Parameters:
as
- The values to sum.- Returns:
- The sum of the given values.
-
sumLeft
public A sumLeft(Stream<A> as)
Sums the given values with left-fold.- Parameters:
as
- The values to sum.- Returns:
- The sum of the given values.
-
sumLeft
public F<List<A>,A> sumLeft()
Returns a function that sums the given values with left-fold.- Returns:
- a function that sums the given values with left-fold.
-
sumRight
public F<List<A>,A> sumRight()
Returns a function that sums the given values with right-fold.- Returns:
- a function that sums the given values with right-fold.
-
sumLeftS
public F<Stream<A>,A> sumLeftS()
Returns a function that sums the given values with left-fold.- Returns:
- a function that sums the given values with left-fold.
-
join
public A join(java.lang.Iterable<A> as, A a)
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.
-
monoidDef
public static <A> Monoid<A> monoidDef(Monoid.Definition<A> def)
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
public static <A> Monoid<A> monoidDef(Monoid.AltDefinition<A> def)
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
public 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.- 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
public 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.- 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
public static <A> Monoid<A> monoid(F<A,F<A,A>> sum, A zero)
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
public static <A> Monoid<A> monoid(F2<A,A,A> sum, A zero)
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
public static <A,B> Monoid<F<A,B>> functionMonoid(Monoid<B> mb)
A monoid for functions.- Parameters:
mb
- The monoid for the function codomain.- Returns:
- A monoid for functions.
-
listMonoid
public static <A> Monoid<List<A>> listMonoid()
A monoid for lists.- Returns:
- A monoid for lists.
-
optionMonoid
public static <A> Monoid<Option<A>> optionMonoid(Semigroup<A> aSemigroup)
Lift aSemigroup<A>
for A to aMonoid<Option<A>>
, using Option.none() as zero.- Returns:
- A monoid for option.
-
firstOptionMonoid
public static <A> Monoid<Option<A>> firstOptionMonoid()
A monoid for options that take the first available value.- Returns:
- A monoid for options that take the first available value.
-
lastOptionMonoid
public static <A> Monoid<Option<A>> lastOptionMonoid()
A monoid for options that take the last available value.- Returns:
- A monoid for options that take the last available value.
-
streamMonoid
public static <A> Monoid<Stream<A>> streamMonoid()
A monoid for streams.- Returns:
- A monoid for streams.
-
arrayMonoid
public static <A> Monoid<Array<A>> arrayMonoid()
A monoid for arrays.- Returns:
- A monoid for arrays.
-
setMonoid
public static <A> Monoid<Set<A>> setMonoid(Ord<A> o)
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.
-
-