Skip to content

Commit 6e5c822

Browse files
committed
Added show instances and toString implementations for finger trees
1 parent b947a4f commit 6e5c822

File tree

12 files changed

+96
-0
lines changed

12 files changed

+96
-0
lines changed

core/src/main/java/fj/Show.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package fj;
22

33
import fj.data.*;
4+
import fj.data.fingertrees.FingerTree;
5+
import fj.data.fingertrees.Node;
46
import fj.data.hlist.HList;
57
import fj.data.vector.V2;
68
import fj.data.vector.V3;
@@ -301,6 +303,44 @@ public static <A> Show<Tree<A>> treeShow(final Show<A> sa) {
301303
});
302304
}
303305

306+
public static <V, A> Show<fj.data.fingertrees.Digit<V, A>> digitShow(final Show<V> sv, final Show<A> sa) {
307+
return show(d -> {
308+
String s = d.match(
309+
o -> "One(" + o.measure() + " -> " + o.value() + ")",
310+
two -> "Two(" + two.measure() + " -> " + v2Show(sa).showS(two.values()) + ")",
311+
three -> "Three(" + three.measure() + " -> " + v3Show(sa).showS(three.values()) + ")",
312+
four -> "Four(" + four.measure() + " -> " + v4Show(sa).showS(four.values()) + ")"
313+
);
314+
return Stream.fromString(s);
315+
});
316+
}
317+
318+
public static <V, A> Show<Node<V, A>> nodeShow(final Show<V> sv, final Show<A> sa) {
319+
return show(n -> {
320+
final String s = n.match(
321+
n2 -> "Node2(" + n2.measure() + " -> " + v2Show(sa).showS(n2.toVector()) + ")",
322+
n3 -> "Node3(" + n3.measure() + " -> " + v3Show(sa).showS(n3.toVector()) + ")");
323+
return Stream.fromString(s);
324+
});
325+
}
326+
327+
public static <V, A> Show<FingerTree<V, A>> fingerTreeShow(final Show<V> sv, final Show<A> sa) {
328+
329+
return show(ft -> {
330+
String sep = ", ";
331+
String str = ft.match(e -> "Empty()",
332+
s -> "Single(" + sv.showS(ft.measure()) + " -> " + sa.showS(s.value()) + ")",
333+
d -> "Deep(" + d.measure() + " -> " +
334+
digitShow(sv, sa).showS(d.prefix()) + sep +
335+
fingerTreeShow(sv, nodeShow(sv, sa)).showS(d.middle()) + sep +
336+
digitShow(sv, sa).showS(d.suffix()) +
337+
")"
338+
);
339+
return Stream.fromString(str);
340+
});
341+
}
342+
343+
304344
public static <A> Show<Seq<A>> seqShow(final Show<A> sa) {
305345
return show(s -> streamShow(sa, "Seq(", ",", ")").show(s.toStream()));
306346
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,4 +520,9 @@ private static <V, A> FingerTree<V, Node<V, Node<V, A>>> addDigits4(final Measur
520520
});
521521
});
522522
}
523+
524+
public String toString() {
525+
return Show.fingerTreeShow(Show.<V>anyShow(), Show.<A>anyShow()).showS(this);
526+
}
527+
523528
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,7 @@ final Option<Digit<V, A>> init() {
162162

163163
public abstract int length();
164164

165+
public String toString() {
166+
return Show.digitShow(Show.<V>anyShow(), Show.<A>anyShow()).showS(this);
167+
}
165168
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import fj.F;
44
import fj.P2;
55
import fj.P3;
6+
import fj.Show;
67

78
import static fj.Bottom.error;
89

@@ -81,4 +82,9 @@ public V measure() {
8182
@Override P3<FingerTree<V, A>, A, FingerTree<V, A>> split1(final F<V, Boolean> predicate, final V acc) {
8283
throw error("Splitting an empty tree");
8384
}
85+
86+
public String toString() {
87+
return Show.fingerTreeShow(Show.<V>anyShow(), Show.<A>anyShow()).showS(this);
88+
}
89+
8490
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import fj.P;
44
import fj.P2;
55
import fj.P3;
6+
import fj.Show;
67
import fj.data.Option;
78
import fj.data.vector.V4;
89
import fj.F;
@@ -88,4 +89,9 @@ public V4<A> values() {
8889
public int length() {
8990
return 4;
9091
}
92+
93+
public String toString() {
94+
return Show.digitShow(Show.<V>anyShow(), Show.<A>anyShow()).showS(this);
95+
}
96+
9197
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import fj.P;
44
import fj.P3;
5+
import fj.Show;
56
import fj.data.Option;
67
import fj.data.vector.V2;
78
import fj.F;
@@ -66,4 +67,9 @@ public int length() {
6667
public V2<A> toVector() {
6768
return as;
6869
}
70+
71+
public String toString() {
72+
return Show.nodeShow(Show.<V>anyShow(), Show.<A>anyShow()).showS(this);
73+
}
74+
6975
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import fj.P;
44
import fj.P3;
5+
import fj.Show;
56
import fj.data.Option;
67
import fj.data.vector.V3;
78
import fj.F;
@@ -74,4 +75,9 @@ P3<Option<Digit<V, A>>, A, Option<Digit<V, A>>> split1(final F<V, Boolean> predi
7475
public V3<A> toVector() {
7576
return as;
7677
}
78+
79+
public String toString() {
80+
return Show.nodeShow(Show.<V>anyShow(), Show.<A>anyShow()).showS(this);
81+
}
82+
7783
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import fj.P;
55
import fj.P2;
66
import fj.P3;
7+
import fj.Show;
78
import fj.data.Option;
89

910
import static fj.data.Option.none;
@@ -54,4 +55,8 @@ public A value() {
5455
public int length() {
5556
return 1;
5657
}
58+
59+
public String toString() {
60+
return Show.digitShow(Show.<V>anyShow(), Show.<A>anyShow()).showS(this);
61+
}
5762
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import fj.P;
55
import fj.P2;
66
import fj.P3;
7+
import fj.Show;
78

89
import static fj.P.p;
10+
import static fj.Show.anyShow;
911

1012
/**
1113
* A tree with a single element.
@@ -99,4 +101,9 @@ public int length() {
99101
public A value() {
100102
return a;
101103
}
104+
105+
public String toString() {
106+
return Show.fingerTreeShow(Show.<V>anyShow(), Show.<A>anyShow()).showS(this);
107+
}
108+
102109
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import fj.P;
44
import fj.P2;
55
import fj.P3;
6+
import fj.Show;
67
import fj.data.Option;
78
import fj.data.vector.V3;
89
import fj.F;
@@ -78,4 +79,9 @@ public V3<A> values() {
7879
public int length() {
7980
return 3;
8081
}
82+
83+
public String toString() {
84+
return Show.digitShow(Show.<V>anyShow(), Show.<A>anyShow()).showS(this);
85+
}
86+
8187
}

0 commit comments

Comments
 (0)