Skip to content

Commit b947a4f

Browse files
committed
Added int min, int max and ord monoid. Added FingerTree empty and int max for pqueue's
1 parent 97b767e commit b947a4f

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,10 @@ public static <A> Monoid<IO<A>> ioMonoid(final Monoid <A> ma) {
401401
return monoid(Semigroup.ioSemigroup(ma.semigroup()), IOFunctions.unit(ma.zero()));
402402
}
403403

404+
public static final Monoid<Integer> intMaxMonoid = monoid(Semigroup.intMaximumSemigroup, Integer.MIN_VALUE);
405+
406+
public static final Monoid<Integer> intMinMonoid = monoid(Semigroup.intMinimumSemigroup, Integer.MAX_VALUE);
407+
404408
/**
405409
* A monoid for the Unit value.
406410
*/
@@ -416,4 +420,8 @@ public static <A> Monoid<Set<A>> setMonoid(final Ord<A> o) {
416420
return monoid(Semigroup.setSemigroup(), Set.empty(o));
417421
}
418422

423+
public static <A> Monoid<A> ordMonoid(Ord<A> o, A zero) {
424+
return monoid(o.max, zero);
425+
}
426+
419427
}

core/src/main/java/fj/data/fingertrees/FingerTree.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import fj.data.Seq;
66

77
import static fj.Monoid.intAdditionMonoid;
8+
import static fj.Monoid.intMaxMonoid;
89

910
/**
1011
* Provides 2-3 finger trees, a functional representation of persistent sequences supporting access to the ends in
@@ -96,8 +97,8 @@ public final <B> FingerTree<V, A> filter(final F<A, Boolean> f) {
9697
public final boolean isEmpty() {
9798
return this instanceof Empty;
9899
}
99-
100-
final Measured<V, A> measured() {
100+
101+
public final Measured<V, A> measured() {
101102
return m;
102103
}
103104

@@ -225,7 +226,15 @@ public final P3<FingerTree<V, A>, A, FingerTree<V, A>> split1(final F<V, Boolean
225226
public abstract int length();
226227

227228
public static <A> FingerTree<Integer, A> emptyIntAddition() {
228-
return mkTree(FingerTree.<Integer, A>measured(intAdditionMonoid, Function.constant(1))).empty();
229+
return empty(intAdditionMonoid, Function.constant(1));
229230
}
230231

232+
public static <V, A> FingerTree<V, A> empty(Monoid<V> m, F<A, V> f) {
233+
return FingerTree.mkTree(measured(m, f)).empty();
234+
}
235+
236+
public static <A> FingerTree<Integer, P2<Integer, A>> emptyIntMax() {
237+
return empty(intMaxMonoid, (P2<Integer, A> p) -> p._1());
238+
}
239+
231240
}

0 commit comments

Comments
 (0)