Skip to content

Commit e602445

Browse files
committed
Added filter and uncurried foldLeft and foldRight to FingerTree
1 parent 6a49182 commit e602445

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package fj.data.fingertrees;
22

3-
import fj.F;
4-
import fj.P2;
3+
import fj.*;
54
import fj.data.Seq;
6-
import fj.Monoid;
75

86
/**
97
* Provides 2-3 finger trees, a functional representation of persistent sequences supporting access to the ends in
@@ -32,6 +30,10 @@ public abstract class FingerTree<V, A> {
3230
*/
3331
public abstract <B> B foldRight(final F<A, F<B, B>> f, final B z);
3432

33+
public <B> B foldRight(final F2<A, B, B> f, final B z) {
34+
return foldRight(F2Functions.curry(f), z);
35+
}
36+
3537
/**
3638
* Folds the tree to the right with the given function.
3739
*
@@ -49,6 +51,10 @@ public abstract class FingerTree<V, A> {
4951
*/
5052
public abstract <B> B foldLeft(final F<B, F<A, B>> f, final B z);
5153

54+
public <B> B foldLeft(final F2<B, A, B> f, final B z) {
55+
return foldLeft(F2Functions.curry(f), z);
56+
}
57+
5258
/**
5359
* Folds the tree to the left with the given function.
5460
*
@@ -67,6 +73,11 @@ public abstract class FingerTree<V, A> {
6773
*/
6874
public abstract <B> FingerTree<V, B> map(final F<A, B> f, final Measured<V, B> m);
6975

76+
public <B> FingerTree<V, A> filter(final F<A, Boolean> f) {
77+
FingerTree<V, A> tree = new Empty<V, A>(m);
78+
return foldLeft((acc, a) -> f.f(a) ? acc.snoc(a) : acc, tree);
79+
}
80+
7081
/**
7182
* Returns the sum of this tree's annotations.
7283
*

0 commit comments

Comments
 (0)