Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions core/src/main/java/fj/Monoid.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import fj.data.Option;
import fj.data.Set;
import fj.data.Stream;

import static fj.Semigroup.multiply1p;
import static fj.data.Stream.iterableStream;

import java.math.BigInteger;
Expand Down Expand Up @@ -90,15 +92,20 @@ public A zero() {
}

/**
* Returns a value summed <code>n</code> times (<code>a + a + ... + a</code>)
* Returns a value summed <code>n</code> times (<code>a + a + ... + a</code>).
* The default definition uses peasant multiplication, exploiting
* associativity to only require `O(log n)` uses of
* {@link #sum(Object, Object)}.
*
* @param n multiplier
* @param a the value to multiply
* @return <code>a</code> summed <code>n</code> times. If <code>n <= 0</code>, returns <code>zero()</code>
* @param a the value to be reapeatly summed
* @return {@code a} summed {@code n} times. If {@code n <= 0}, returns
* {@code 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;
return (n <= 0)
? zero
: multiply1p(sum, n - 1, a);
}

/**
Expand Down
44 changes: 40 additions & 4 deletions core/src/main/java/fj/Semigroup.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package fj;

import static fj.Function.curry;

import fj.data.Array;
import fj.data.List;
import fj.data.Natural;
Expand All @@ -10,8 +8,10 @@
import fj.data.Set;
import fj.data.Stream;

import java.math.BigInteger;
import java.math.BigDecimal;
import java.math.BigInteger;

import static fj.Function.curry;

/**
* Implementations must satisfy the law of associativity:
Expand Down Expand Up @@ -58,6 +58,42 @@ public F<A, F<A, A>> sum() {
return sum;
}

/**
* Returns a value summed <code>n + 1</code> times (
* <code>a + a + ... + a</code>) The default definition uses peasant
* multiplication, exploiting associativity to only require `O(log n)` uses of
* {@link #sum(Object, Object)}.
*
* @param n multiplier
* @param a the value to be reapeatly summed n + 1 times
* @return {@code a} summed {@code n} times. If {@code n <= 0}, returns
* {@code zero()}
*/
public A multiply1p(int n, A a) {
return multiply1p(sum, n, a);
}

// shared implementation between Semigroup and Monoid
static <A> A multiply1p(F<A, F<A, A>> sum, int n, A a) {
if (n <= 0) {
return a;
}

A xTmp = a;
int yTmp = n;
A zTmp = a;
while (true) {
if ((yTmp & 1) == 1) {
zTmp = sum.f(xTmp).f(zTmp);
if (yTmp == 1) {
return zTmp;
}
}
xTmp = sum.f(xTmp).f(xTmp);
yTmp = (yTmp) >>> 1;
}
}

/**
* Constructs a semigroup from the given function.
*
Expand Down Expand Up @@ -316,7 +352,7 @@ public static <A> Semigroup<P1<A>> p1Semigroup(final Semigroup<A> sa) {
* @return A semigroup for binary products.
*/
public static <A, B> Semigroup<P2<A, B>> p2Semigroup(final Semigroup<A> sa, final Semigroup<B> sb) {
return semigroup((a1, a2) -> P.lazy(() -> sa.sum(a1._1(), a2._1()), () -> sb.sum(a1._2(), a2._2())));
return semigroup((a1, a2) -> P.lazy(() -> sa.sum(a1._1(), a2._1()), () -> sb.sum(a1._2(), a2._2())));
}

/**
Expand Down