Skip to content

Commit 9e77606

Browse files
committed
Converted most of fj.data.fingertrees to use lambdas
1 parent 2335817 commit 9e77606

3 files changed

Lines changed: 54 additions & 105 deletions

File tree

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

Lines changed: 47 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,23 @@ public abstract class Digit<V, A> {
3636
* @param f A function with which to fold this digit.
3737
* @return The right reduction of this digit with the given function.
3838
*/
39-
public final A reduceRight(final F<A, F<A, A>> f) {
40-
return match(new F<One<V, A>, A>() {
41-
public A f(final One<V, A> one) {
42-
return one.value();
43-
}
44-
}, new F<Two<V, A>, A>() {
45-
public A f(final Two<V, A> two) {
46-
final V2<A> v = two.values();
47-
return f.f(v._1()).f(v._2());
48-
}
49-
}, new F<Three<V, A>, A>() {
50-
public A f(final Three<V, A> three) {
51-
final V3<A> v = three.values();
52-
return f.f(v._1()).f(f.f(v._2()).f(v._3()));
53-
}
54-
}, new F<Four<V, A>, A>() {
55-
public A f(final Four<V, A> four) {
56-
final V4<A> v = four.values();
57-
return f.f(v._1()).f(f.f(v._2()).f(f.f(v._3()).f(v._4())));
58-
}
59-
});
60-
}
39+
public final A reduceRight(final F<A, F<A, A>> f) {
40+
return match(
41+
one -> one.value(),
42+
two -> {
43+
final V2<A> v = two.values();
44+
return f.f(v._1()).f(v._2());
45+
},
46+
three -> {
47+
final V3<A> v = three.values();
48+
return f.f(v._1()).f(f.f(v._2()).f(v._3()));
49+
},
50+
four -> {
51+
final V4<A> v = four.values();
52+
return f.f(v._1()).f(f.f(v._2()).f(f.f(v._3()).f(v._4())));
53+
}
54+
);
55+
}
6156

6257
/**
6358
* Folds this digit to the right using the given function.
@@ -66,26 +61,21 @@ public A f(final Four<V, A> four) {
6661
* @return The right reduction of this digit with the given function.
6762
*/
6863
public final A reduceLeft(final F<A, F<A, A>> f) {
69-
return match(new F<One<V, A>, A>() {
70-
public A f(final One<V, A> one) {
71-
return one.value();
72-
}
73-
}, new F<Two<V, A>, A>() {
74-
public A f(final Two<V, A> two) {
75-
final V2<A> v = two.values();
76-
return f.f(v._1()).f(v._2());
77-
}
78-
}, new F<Three<V, A>, A>() {
79-
public A f(final Three<V, A> three) {
80-
final V3<A> v = three.values();
81-
return f.f(f.f(v._1()).f(v._2())).f(v._3());
82-
}
83-
}, new F<Four<V, A>, A>() {
84-
public A f(final Four<V, A> four) {
85-
final V4<A> v = four.values();
86-
return f.f(f.f(f.f(v._1()).f(v._2())).f(v._3())).f(v._4());
87-
}
88-
});
64+
return match(
65+
one -> one.value(),
66+
two -> {
67+
final V2<A> v = two.values();
68+
return f.f(v._1()).f(v._2());
69+
},
70+
three -> {
71+
final V3<A> v = three.values();
72+
return f.f(f.f(v._1()).f(v._2())).f(v._3());
73+
},
74+
four -> {
75+
final V4<A> v = four.values();
76+
return f.f(f.f(f.f(v._1()).f(v._2())).f(v._3())).f(v._4());
77+
}
78+
);
8979
}
9080

9181
/**
@@ -97,23 +87,12 @@ public A f(final Four<V, A> four) {
9787
* with the given function and measured with the given measuring.
9888
*/
9989
public final <B> Digit<V, B> map(final F<A, B> f, final Measured<V, B> m) {
100-
return match(new F<One<V, A>, Digit<V, B>>() {
101-
public Digit<V, B> f(final One<V, A> one) {
102-
return new One<V, B>(m, f.f(one.value()));
103-
}
104-
}, new F<Two<V, A>, Digit<V, B>>() {
105-
public Digit<V, B> f(final Two<V, A> two) {
106-
return new Two<V, B>(m, two.values().map(f));
107-
}
108-
}, new F<Three<V, A>, Digit<V, B>>() {
109-
public Digit<V, B> f(final Three<V, A> three) {
110-
return new Three<V, B>(m, three.values().map(f));
111-
}
112-
}, new F<Four<V, A>, Digit<V, B>>() {
113-
public Digit<V, B> f(final Four<V, A> four) {
114-
return new Four<V, B>(m, four.values().map(f));
115-
}
116-
});
90+
return match(
91+
one -> new One<V, B>(m, f.f(one.value())),
92+
two -> new Two<V, B>(m, two.values().map(f)),
93+
three -> new Three<V, B>(m, three.values().map(f)),
94+
four -> new Four<V, B>(m, four.values().map(f))
95+
);
11796
}
11897

11998
/**
@@ -153,28 +132,15 @@ public V f(final V v, final A a) {
153132
* Returns the tree representation of this digit.
154133
* @return the tree representation of this digit.
155134
*/
156-
public final FingerTree<V, A> toTree() {
157-
final MakeTree<V, A> mk = mkTree(m);
158-
return match(new F<One<V, A>, FingerTree<V, A>>() {
159-
public FingerTree<V, A> f(final One<V, A> one) {
160-
return mk.single(one.value());
161-
}
162-
}, new F<Two<V, A>, FingerTree<V, A>>() {
163-
public FingerTree<V, A> f(final Two<V, A> two) {
164-
return mk.deep(mk.one(two.values()._1()), new Empty<V, Node<V, A>>(m.nodeMeasured()), mk.one(two.values()._2()));
165-
}
166-
}, new F<Three<V, A>, FingerTree<V, A>>() {
167-
public FingerTree<V, A> f(final Three<V, A> three) {
168-
return mk.deep(mk.two(three.values()._1(), three.values()._2()), new Empty<V, Node<V, A>>(m.nodeMeasured()),
169-
mk.one(three.values()._3()));
170-
}
171-
}, new F<Four<V, A>, FingerTree<V, A>>() {
172-
public FingerTree<V, A> f(final Four<V, A> four) {
173-
return mk.deep(mk.two(four.values()._1(), four.values()._2()), new Empty<V, Node<V, A>>(m.nodeMeasured()),
174-
mk.two(four.values()._3(), four.values()._4()));
175-
}
176-
});
177-
}
135+
public final FingerTree<V, A> toTree() {
136+
final MakeTree<V, A> mk = mkTree(m);
137+
return match(
138+
one -> mk.single(one.value()),
139+
two -> mk.deep(mk.one(two.values()._1()), new Empty<V, Node<V, A>>(m.nodeMeasured()), mk.one(two.values()._2())),
140+
three -> mk.deep(mk.two(three.values()._1(), three.values()._2()), new Empty<V, Node<V, A>>(m.nodeMeasured()), mk.one(three.values()._3())),
141+
four -> mk.deep(mk.two(four.values()._1(), four.values()._2()), new Empty<V, Node<V, A>>(m.nodeMeasured()), mk.two(four.values()._3(), four.values()._4()))
142+
);
143+
}
178144

179145
Option<Digit<V, A>> tail() {
180146
return match(

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,7 @@ public V zero() {
7474
* @return A measured instance for nodes.
7575
*/
7676
public Measured<V, Node<V, A>> nodeMeasured() {
77-
return new Measured<V, Node<V, A>>(m, new F<Node<V, A>, V>() {
78-
public V f(final Node<V, A> node) {
79-
return node.measure();
80-
}
81-
});
77+
return new Measured<V, Node<V, A>>(m, node -> node.measure());
8278
}
8379

8480
/**
@@ -87,11 +83,7 @@ public V f(final Node<V, A> node) {
8783
* @return A measured instance for digits.
8884
*/
8985
public Measured<V, Digit<V, A>> digitMeasured() {
90-
return new Measured<V, Digit<V, A>>(m, new F<Digit<V, A>, V>() {
91-
public V f(final Digit<V, A> d) {
92-
return d.measure();
93-
}
94-
});
86+
return new Measured<V, Digit<V, A>>(m, d -> d.measure());
9587
}
9688

9789
}

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,14 @@ public static <V, A, B> F<B, F<Node<V, A>, B>> foldRight_(final F<A, F<B, B>> af
3232
}
3333

3434
public final <B> Node<V, B> map(final F<A, B> f, final Measured<V, B> m) {
35-
return match(new F<Node2<V, A>, Node<V, B>>() {
36-
public Node<V, B> f(final Node2<V, A> node2) {
37-
return new Node2<V, B>(m, node2.toVector().map(f));
38-
}
39-
}, new F<Node3<V, A>, Node<V, B>>() {
40-
public Node<V, B> f(final Node3<V, A> node3) {
41-
return new Node3<V, B>(m, node3.toVector().map(f));
42-
}
43-
});
35+
return match(
36+
node2 -> new Node2<V, B>(m, node2.toVector().map(f)),
37+
node3 -> new Node3<V, B>(m, node3.toVector().map(f))
38+
);
4439
}
4540

4641
public static <V, A, B> F<Node<V, A>, Node<V, B>> liftM(final F<A, B> f, final Measured<V, B> m) {
47-
return new F<Node<V, A>, Node<V, B>>() {
48-
public Node<V, B> f(final Node<V, A> node) {
49-
return node.map(f, m);
50-
}
51-
};
42+
return node -> node.map(f, m);
5243
}
5344

5445
public abstract Digit<V, A> toDigit();

0 commit comments

Comments
 (0)