package fj; import static fj.Function.curry; import static fj.Function.flip; import fj.data.Array; import fj.data.List; import fj.data.Natural; import fj.data.Option; import fj.data.Set; import fj.data.Stream; import static fj.data.Stream.iterableStream; import java.math.BigInteger; import java.math.BigDecimal; /** * A monoid abstraction to be defined across types of the given type argument. Implementations must * follow the monoidal laws: *
n times (a + a + ... + a)
* @param n multiplier
* @param a the value to multiply
* @return a summed n times. If n <= 0, returns zero()
*/
public A multiply(final int n, final A a) {
A m = zero();
for (int i = 0; i < n; i++) { m = sum(m, a); }
return m;
}
/**
* Sums the given values with right-fold.
*
* @param as The values to sum.
* @return The sum of the given values.
*/
public A sumRight(final List as) {
return as.foldRight(sum, zero);
}
/**
* Sums the given values with right-fold.
*
* @param as The values to sum.
* @return The sum of the given values.
*/
public A sumRight(final Stream as) {
return as.foldRight((a, ap1) -> sum(a, ap1._1()), zero);
}
/**
* Sums the given values with left-fold.
*
* @param as The values to sum.
* @return The sum of the given values.
*/
public A sumLeft(final List as) {
return as.foldLeft(sum, zero);
}
/**
* Sums the given values with left-fold.
*
* @param as The values to sum.
* @return The sum of the given values.
*/
public A sumLeft(final Stream as) {
return as.foldLeft(sum, zero);
}
/**
* Returns a function that sums the given values with left-fold.
*
* @return a function that sums the given values with left-fold.
*/
public F