Skip to content

Commit 35b55ff

Browse files
author
Runar Bjarnason
committed
Added monoid composition
1 parent 206afa6 commit 35b55ff

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ build
1313
.DS_Store
1414
MANIFEST.MF
1515
*/bin/**
16-
17-
16+
*~

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fj;
22

33
import static fj.Function.curry;
4-
import static fj.Function.compose;
54
import static fj.Function.flip;
65
import fj.data.Array;
76
import fj.data.List;
@@ -20,7 +19,7 @@
2019
* <ul>
2120
* <li><em>Left Identity</em>; forall x. sum(zero(), x) == x</li>
2221
* <li><em>Right Identity</em>; forall x. sum(x, zero()) == x</li>
23-
* <li><em>Associativity</em>; forall x. forall y. forall z. sum(sum(x, y), z) == sum(x, sum(y, z))</li>
22+
* <li><em>Associativity</em>; forall x y z. sum(sum(x, y), z) == sum(x, sum(y, z))</li>
2423
* </ul>
2524
*
2625
* @version %build.number%
@@ -34,6 +33,14 @@ private Monoid(final F<A, F<A, A>> sum, final A zero) {
3433
this.zero = zero;
3534
}
3635

36+
/**
37+
* Composes this monoid with another.
38+
*/
39+
public <B> Monoid<P2<A,B>>compose(Monoid<B> m) {
40+
return monoid((P2<A,B> x) -> (P2<A,B> y) ->
41+
P.p(sum(x._1(), y._1()), m.sum(x._2(), y._2())), P.p(zero, m.zero));
42+
}
43+
3744
/**
3845
* Returns a semigroup projection of this monoid.
3946
*
@@ -176,7 +183,7 @@ public A join(final Iterable<A> as, final A a) {
176183
final Stream<A> s = iterableStream(as);
177184
return s.isEmpty() ?
178185
zero :
179-
s.foldLeft1(compose(sum, flip(sum).f(a)));
186+
s.foldLeft1(Function.compose(sum, flip(sum).f(a)));
180187
}
181188

182189
/**

0 commit comments

Comments
 (0)