Copyright 2008 - 2009 Tony Morris, Runar Bjarnason, Tom Adams, Brad Clow, Ricky Clarkson, Nick Partridge, Jason Zaugg

This software is released under an open source BSD licence.

fj.pre
Class Monoid<A>

java.lang.Object
  extended by fj.pre.Monoid<A>

public final class Monoid<A>
extends Object

A monoid abstraction to be defined across types of the given type argument. Implementations must follow the monoidal laws:


Field Summary
static Monoid<BigDecimal> bigdecimalAdditionMonoid
          A monoid that adds big decimals.
static Monoid<BigDecimal> bigdecimalMultiplicationMonoid
          A monoid that multiplies big decimals.
static Monoid<BigInteger> bigintAdditionMonoid
          A monoid that adds big integers.
static Monoid<BigInteger> bigintMultiplicationMonoid
          A monoid that multiplies big integers.
static Monoid<Boolean> conjunctionMonoid
          A monoid that ANDs booleans.
static Monoid<Boolean> disjunctionMonoid
          A monoid that ORs booleans.
static Monoid<Boolean> exclusiveDisjunctionMonoid
          A monoid that XORs booleans.
static Monoid<Integer> intAdditionMonoid
          A monoid that adds integers.
static Monoid<Integer> intMultiplicationMonoid
          A monoid that multiplies integers.
static Monoid<Long> longAdditionMonoid
          A monoid that adds longs.
static Monoid<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<StringBuffer> stringBufferMonoid
          A monoid that appends string buffers.
static Monoid<StringBuilder> stringBuilderMonoid
          A monoid that appends string builders.
static Monoid<String> stringMonoid
          A monoid that appends strings.
 
Method Summary
static
<A> Monoid<Array<A>>
arrayMonoid()
          A monoid for arrays.
static
<A,B> Monoid<F<A,B>>
functionMonoid(Monoid<B> mb)
          A monoid for functions.
 A join(Iterable<A> as, A a)
          Intersperses the given value between each two elements of the iterable, and sums the result.
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>
monoid(Semigroup<A> s, A zero)
          Constructs a monoid from the given semigroup and zero value, which must follow the monoidal laws.
static
<A> Monoid<Option<A>>
optionMonoid()
          A monoid for options.
 Semigroup<A> semigroup()
          Returns a semigroup projection of this monoid.
static
<A> Monoid<Set<A>>
setMonoid(Ord<A> o)
          A 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.
 A zero()
          The zero value for this monoid.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

intAdditionMonoid

public static final Monoid<Integer> intAdditionMonoid
A monoid that adds integers.


intMultiplicationMonoid

public static final Monoid<Integer> intMultiplicationMonoid
A monoid that multiplies integers.


bigintAdditionMonoid

public static final Monoid<BigInteger> bigintAdditionMonoid
A monoid that adds big integers.


bigintMultiplicationMonoid

public static final Monoid<BigInteger> bigintMultiplicationMonoid
A monoid that multiplies big integers.


bigdecimalAdditionMonoid

public static final Monoid<BigDecimal> bigdecimalAdditionMonoid
A monoid that adds big decimals.


bigdecimalMultiplicationMonoid

public static final Monoid<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<Long> longAdditionMonoid
A monoid that adds longs.


longMultiplicationMonoid

public static final Monoid<Long> longMultiplicationMonoid
A monoid that multiplies longs.


disjunctionMonoid

public static final Monoid<Boolean> disjunctionMonoid
A monoid that ORs booleans.


exclusiveDisjunctionMonoid

public static final Monoid<Boolean> exclusiveDisjunctionMonoid
A monoid that XORs booleans.


conjunctionMonoid

public static final Monoid<Boolean> conjunctionMonoid
A monoid that ANDs booleans.


stringMonoid

public static final Monoid<String> stringMonoid
A monoid that appends strings.


stringBufferMonoid

public static final Monoid<StringBuffer> stringBufferMonoid
A monoid that appends string buffers.


stringBuilderMonoid

public static final Monoid<StringBuilder> stringBuilderMonoid
A monoid that appends string builders.

Method Detail

semigroup

public Semigroup<A> semigroup()
Returns a semigroup projection of this monoid.

Returns:
A semigroup projection of this 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.

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(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.

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.

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.

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(Semigroup<A> s,
                                   A zero)
Constructs a monoid from the given semigroup and zero value, which must follow the monoidal laws.

Parameters:
s - The semigroup 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()
A monoid for options.

Returns:
A monoid for options.

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 monoid for sets.

Parameters:
o - An order for set elements.
Returns:
A monoid for sets whose elements have the given order.

Copyright 2008 - 2009 Tony Morris, Runar Bjarnason, Tom Adams, Brad Clow, Ricky Clarkson, Nick Partridge, Jason Zaugg

This software is released under an open source BSD licence.