88import fj .data .NonEmptyList ;
99import fj .data .Option ;
1010import fj .data .Stream ;
11+ import fj .data .Set ;
12+ import fj .data .TreeMap ;
1113import fj .data .Tree ;
1214import fj .data .Validation ;
1315import fj .data .hlist .HList ;
@@ -282,7 +284,7 @@ public static <A, B> Show<Validation<A, B>> validationShow(final Show<A> sa, fin
282284 * @return A show instance for the {@link Stream} type.
283285 */
284286 public static <A > Show <List <A >> listShow (final Show <A > sa ) {
285- return show (as -> streamShow (sa ).show (as .toStream ()));
287+ return show (as -> streamShow (sa , "List(" , "," , ")" ).show (as .toStream ()));
286288 }
287289
288290 /**
@@ -303,21 +305,91 @@ public static <A> Show<NonEmptyList<A>> nonEmptyListShow(final Show<A> sa) {
303305 */
304306 public static <A > Show <Tree <A >> treeShow (final Show <A > sa ) {
305307 return show (a -> {
306- final Stream <Character > b = sa .f .f (a .root ())
307- .append (p1Show (streamShow (Show .<A >treeShow (sa ))).f .f (a .subForest ()))
308- .snoc (')' );
309- return cons ('(' , p (b ));
308+ Stream <Character > result = sa .f .f (a .root ());
309+ if (!a .subForest ()._1 ().isEmpty ()) {
310+ result = result .append (fromString ("," )).append (streamShow (treeShow (sa ), "" , "," , "" ).f .f (a .subForest ()._1 ()));
311+ }
312+ return fromString ("Tree(" ).append (p (result )).append (fromString (")" ));
310313 });
311314 }
312315
316+ /**
317+ * A show instance for the {@link Set} type.
318+ *
319+ * @param sa Show for the elements of the set.
320+ * @return A show instance for the {@link Set} type.
321+ */
322+ public static <A > Show <Set <A >> setShow (final Show <A > sa ) {
323+ return show (s -> streamShow (sa , "Set(" , "," , ")" ).show (s .toStream ()));
324+ }
325+
326+ /**
327+ * A show instance for the {@link TreeMap} type.
328+ *
329+ * @param sk Show for the keys of the TreeMap.
330+ * @param sv Show for the values of the TreeMap.
331+ * @return A show instance for the {@link TreeMap} type.
332+ */
333+ public static <K , V > Show <TreeMap <K , V >> treeMapShow (final Show <K > sk , final Show <V > sv ) {
334+ return show (tm -> {
335+ Stream <P2 <K , V >> stream = Stream .iteratorStream (tm .iterator ());
336+ return streamShow (Show .p2MapShow (sk , sv ), "TreeMap(" , "," , ")" ).show (stream );
337+ });
338+ }
339+
340+ /**
341+ * A show instance for the {@link P2 tuple-2} type in the style of a mapping from A to B.
342+ *
343+ * @param sa Show for the first element of the tuple.
344+ * @param sb Show for the second element of the tuple.
345+ * @return A show instance for the {@link P2 tuple-2} type.
346+ */
347+ public static <A , B > Show <P2 <A , B >> p2MapShow (final Show <A > sa , final Show <B > sb ) {
348+ return p2Show (sa , sb , "(" , ":" , ")" );
349+ }
350+
351+ /**
352+ * A show instance for the {@link P2 tuple-2} type.
353+ *
354+ * @param sa Show for the first element of the tuple.
355+ * @param sb Show for the second element of the tuple.
356+ * @param start Prefix string for the show.
357+ * @param sep Separator string between elements of the tuple.
358+ * @param end Suffix string for the show.
359+ * @return A show instance for the {@link P2 tuple-2} type.
360+ */
361+ public static <A , B > Show <P2 <A , B >> p2Show (final Show <A > sa , final Show <B > sb , String start , String sep , String end ) {
362+ return show (p -> fromString (start ).append (p (sa .show (p ._1 ()))).append (fromString (sep )).append (sb .show (p ._2 ())).append (fromString (end )));
363+ }
364+
313365 /**
314366 * A show instance for the {@link Stream} type.
315367 *
316368 * @param sa Show for the elements of the stream.
317369 * @return A show instance for the {@link Stream} type.
318370 */
319371 public static <A > Show <Stream <A >> streamShow (final Show <A > sa ) {
320- return show (as -> join (as .map (sa .show_ ()).intersperse (fromString ("," )).cons (fromString ("<" )).snoc (p (fromString (">" )))));
372+ return streamShow (sa , "Stream(" , "," , ")" );
373+ }
374+
375+ /**
376+ * A show instance for the {@link Stream} type.
377+ *
378+ * @param sa Show for the first element of the tuple.
379+ * @param start Prefix string for the show.
380+ * @param sep Separator string between elements of the stream.
381+ * @param end Suffix string for the show.
382+ * @return A show instance for the {@link Stream} type.
383+ */
384+ public static <A > Show <Stream <A >> streamShow (final Show <A > sa , String start , String sep , String end ) {
385+ return show (streamShow_ (sa , start , sep , end ));
386+ }
387+
388+ /**
389+ * Returns the transformation equivalent for the stream show.
390+ */
391+ public static <A > F <Stream <A >, Stream <Character >> streamShow_ (final Show <A > sa , String start , String sep , String end ) {
392+ return as -> join (as .map (sa .show_ ()).intersperse (fromString (sep )).cons (fromString (start )).snoc (p (fromString (end ))));
321393 }
322394
323395 /**
@@ -334,12 +406,12 @@ public static <A> Show<Array<A>> arrayShow(final Show<A> sa) {
334406 b = b .append (sa .f .f (as .get (i )));
335407
336408 if (i != as .length () - 1 )
337- b = b .snoc ( ',' );
409+ b = b .append ( fromString ( "," ) );
338410 }
339411
340- b = b .snoc ( '}' );
412+ b = b .append ( fromString ( "Array(" ) );
341413
342- return cons ( '{' , p (b ));
414+ return fromString ( ")" ). append ( p (b ));
343415 });
344416 }
345417
@@ -370,7 +442,7 @@ public static <A> Show<P1<A>> p1Show(final Show<A> sa) {
370442 * @return A show instance for the {@link P2 tuple-2} type.
371443 */
372444 public static <A , B > Show <P2 <A , B >> p2Show (final Show <A > sa , final Show <B > sb ) {
373- return show ( p -> cons ( '(' , p ( sa . show ( p . _1 ()))). snoc ( ',' ). append ( sb . show ( p . _2 ())). snoc ( ')' ) );
445+ return p2Show ( sa , sb , "(" , "," , ")" );
374446 }
375447
376448 /**
@@ -487,7 +559,7 @@ public static <A, B, C, D, E> Show<P5<A, B, C, D, E>> p5Show(final Show<A> sa, f
487559 * @return A show instance for a vector-2.
488560 */
489561 public static <A > Show <V2 <A >> v2Show (final Show <A > ea ) {
490- return streamShow (ea ).comap (V2 .<A >toStream_ ());
562+ return streamShow (ea , "V2(" , "," , ")" ).comap (V2 .<A >toStream_ ());
491563 }
492564
493565 /**
@@ -497,7 +569,7 @@ public static <A> Show<V2<A>> v2Show(final Show<A> ea) {
497569 * @return A show instance for a vector-3.
498570 */
499571 public static <A > Show <V3 <A >> v3Show (final Show <A > ea ) {
500- return streamShow (ea ).comap (V3 .<A >toStream_ ());
572+ return streamShow (ea , "V3(" , "," , ")" ).comap (V3 .<A >toStream_ ());
501573 }
502574
503575 /**
@@ -507,7 +579,7 @@ public static <A> Show<V3<A>> v3Show(final Show<A> ea) {
507579 * @return A show instance for a vector-4.
508580 */
509581 public static <A > Show <V4 <A >> v4Show (final Show <A > ea ) {
510- return streamShow (ea ).comap (V4 .<A >toStream_ ());
582+ return streamShow (ea , "V4(" , "," , ")" ).comap (V4 .<A >toStream_ ());
511583 }
512584
513585 /**
@@ -517,7 +589,7 @@ public static <A> Show<V4<A>> v4Show(final Show<A> ea) {
517589 * @return A show instance for a vector-5.
518590 */
519591 public static <A > Show <V5 <A >> v5Show (final Show <A > ea ) {
520- return streamShow (ea ).comap (V5 .<A >toStream_ ());
592+ return streamShow (ea , "V5(" , "," , ")" ).comap (V5 .<A >toStream_ ());
521593 }
522594
523595 /**
@@ -527,7 +599,7 @@ public static <A> Show<V5<A>> v5Show(final Show<A> ea) {
527599 * @return A show instance for a vector-6.
528600 */
529601 public static <A > Show <V6 <A >> v6Show (final Show <A > ea ) {
530- return streamShow (ea ).comap (V6 .<A >toStream_ ());
602+ return streamShow (ea , "V6(" , "," , ")" ).comap (V6 .<A >toStream_ ());
531603 }
532604
533605 /**
@@ -537,7 +609,7 @@ public static <A> Show<V6<A>> v6Show(final Show<A> ea) {
537609 * @return A show instance for a vector-7.
538610 */
539611 public static <A > Show <V7 <A >> v7Show (final Show <A > ea ) {
540- return streamShow (ea ).comap (V7 .<A >toStream_ ());
612+ return streamShow (ea , "V7(" , "," , ")" ).comap (V7 .<A >toStream_ ());
541613 }
542614
543615 /**
@@ -547,7 +619,7 @@ public static <A> Show<V7<A>> v7Show(final Show<A> ea) {
547619 * @return A show instance for a vector-8.
548620 */
549621 public static <A > Show <V8 <A >> v8Show (final Show <A > ea ) {
550- return streamShow (ea ).comap (V8 .<A >toStream_ ());
622+ return streamShow (ea , "V8(" , "," , ")" ).comap (V8 .<A >toStream_ ());
551623 }
552624
553625 /**
@@ -583,6 +655,6 @@ public static <A> Show<Stream<A>> unlineShow(final Show<A> sa) {
583655 * @return a show instance for heterogeneous Streams.
584656 */
585657 public static <E , L extends HList <L >> Show <HList .HCons <E , L >> HListShow (final Show <E > e , final Show <L > l ) {
586- return show (c -> e .show (c .head ()). cons ( '[' ).append (l .show (c .tail ())).snoc ( ']' ));
658+ return show (c -> fromString ( "HList(" ). append ( e .show (c .head ())).append (l .show (c .tail ())).append ( fromString ( ")" ) ));
587659 }
588660}
0 commit comments