Skip to content

Commit 30da36c

Browse files
committed
Add "dual" methods on Semigroup and Monoid
1 parent 3716e73 commit 30da36c

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

core/src/main/java/fj/Monoid.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import fj.data.Set;
1212
import fj.data.Stream;
1313

14+
import static fj.Function.flip;
1415
import static fj.Semigroup.multiply1p;
1516
import static fj.data.Stream.iterableStream;
1617

@@ -191,6 +192,13 @@ public A join(final Iterable<A> as, final A a) {
191192
s.foldLeft1(Function.compose(sum, flip(sum).f(a)));
192193
}
193194

195+
/**
196+
* Swaps the arguments when summing.
197+
*/
198+
public Monoid<A> dual() {
199+
return monoid(flip(sum), zero);
200+
}
201+
194202
/**
195203
* Constructs a monoid from the given sum function and zero value, which must follow the monoidal
196204
* laws.

core/src/main/java/fj/Semigroup.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.math.BigInteger;
1515

1616
import static fj.Function.curry;
17+
import static fj.Function.flip;
1718

1819
/**
1920
* Implementations must satisfy the law of associativity:
@@ -97,6 +98,13 @@ static <A> A multiply1p(F<A, F<A, A>> sum, int n, A a) {
9798
}
9899

99100
/**
101+
/**
102+
* Swaps the arguments when summing.
103+
*/
104+
public Semigroup<A> dual() {
105+
return semigroup(flip(sum));
106+
}
107+
100108
* Constructs a semigroup from the given function.
101109
*
102110
* @param sum The function to construct this semigroup with.

0 commit comments

Comments
 (0)