comparison src/main/java/fj/Show.java @ 0:fe80c1edf1be

add getLoop
author tatsuki
date Fri, 20 Mar 2015 21:04:03 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:fe80c1edf1be
1 package fj;
2
3 import fj.data.Array;
4 import fj.data.Either;
5 import fj.data.LazyString;
6 import fj.data.List;
7 import fj.data.Natural;
8 import fj.data.NonEmptyList;
9 import fj.data.Option;
10 import fj.data.Stream;
11 import fj.data.Tree;
12 import fj.data.Validation;
13 import fj.data.hlist.HList;
14 import fj.data.vector.V2;
15 import fj.data.vector.V3;
16 import fj.data.vector.V4;
17 import fj.data.vector.V5;
18 import fj.data.vector.V6;
19 import fj.data.vector.V7;
20 import fj.data.vector.V8;
21
22 import java.math.BigDecimal;
23 import java.math.BigInteger;
24
25 import static fj.Function.compose;
26 import static fj.P.p;
27 import static fj.Unit.unit;
28 import static fj.data.Stream.cons;
29 import static fj.data.Stream.fromString;
30 import static fj.data.Stream.join;
31 import static fj.data.Stream.nil;
32 import static fj.data.Stream.single;
33
34 /**
35 * Renders an object for display.
36 *
37 * @version %build.number%
38 */
39 public final class Show<A> {
40 private final F<A, Stream<Character>> f;
41
42 private Show(final F<A, Stream<Character>> f) {
43 this.f = f;
44 }
45
46 /**
47 * Maps the given function across this show as a contra-variant functor.
48 *
49 * @param f The function to map.
50 * @return A new show.
51 */
52 public <B> Show<B> comap(final F<B, A> f) {
53 return show(compose(this.f, f));
54 }
55
56 /**
57 * Returns the display rendering of the given argument.
58 *
59 * @param a The argument to display.
60 * @return The display rendering of the given argument.
61 */
62 public Stream<Character> show(final A a) {
63 return f.f(a);
64 }
65
66 /**
67 * Returns the display rendering of the given argument.
68 *
69 * @param a The argument to display.
70 * @return The display rendering of the given argument.
71 */
72 public List<Character> showl(final A a) {
73 return show(a).toList();
74 }
75
76 /**
77 * Returns the display rendering of the given argument as a <code>String</code>.
78 *
79 * @param a The argument to display.
80 * @return The display rendering of the given argument as a <code>String</code>.
81 */
82 public String showS(final A a) {
83 return Stream.asString(show(a));
84 }
85
86 /**
87 * Returns the transformation equivalent to this show.
88 *
89 * @return the transformation equivalent to this show.
90 */
91 public F<A, String> showS_() {
92 return new F<A, String>() {
93 public String f(final A a) {
94 return showS(a);
95 }
96 };
97 }
98
99 /**
100 * Returns the transformation equivalent to this show.
101 *
102 * @return the transformation equivalent to this show.
103 */
104 public F<A, Stream<Character>> show_() {
105 return f;
106 }
107
108 /**
109 * Prints the given argument to the standard output stream with a new line.
110 *
111 * @param a The argument to print.
112 * @return The unit value.
113 */
114 public Unit println(final A a) {
115 print(a);
116 System.out.println();
117 return unit();
118 }
119
120 /**
121 * Prints the given argument to the standard output stream.
122 *
123 * @param a The argument to print.
124 * @return The unit value.
125 */
126 public Unit print(final A a) {
127 final char[] buffer = new char[8192];
128 int c = 0;
129 for (Stream<Character> cs = show(a); cs.isNotEmpty(); cs = cs.tail()._1()) {
130 buffer[c] = cs.head();
131 c++;
132 if (c == 8192) {
133 System.out.print(buffer);
134 c = 0;
135 }
136 }
137 System.out.print(Array.copyOfRange(buffer, 0, c));
138 return unit();
139 }
140
141 /**
142 * Prints the given argument to the standard error stream with a new line.
143 *
144 * @param a The argument to print.
145 */
146 public void printlnE(final A a) {
147 System.err.println(showS(a));
148 }
149
150 /**
151 * Returns a show instance using the given function.
152 *
153 * @param f The function to use for the returned show instance.
154 * @return A show instance.
155 */
156 public static <A> Show<A> show(final F<A, Stream<Character>> f) {
157 return new Show<A>(f);
158 }
159
160 /**
161 * Returns a show instance using the given function.
162 *
163 * @param f The function to use for the returned show instance.
164 * @return A show instance.
165 */
166 public static <A> Show<A> showS(final F<A, String> f) {
167 return new Show<A>(new F<A, Stream<Character>>() {
168 public Stream<Character> f(final A a) {
169 return fromString(f.f(a));
170 }
171 });
172 }
173
174 /**
175 * Returns a show instance that uses {@link Object#toString()} to perform the display rendering.
176 *
177 * @return A show instance that uses {@link Object#toString()} to perform the display rendering.
178 */
179 public static <A> Show<A> anyShow() {
180 return new Show<A>(new F<A, Stream<Character>>() {
181 public Stream<Character> f(final A a) {
182 return Stream.fromString((a == null) ? "null" : a.toString());
183 }
184 });
185 }
186
187 /**
188 * A show instance for the <code>boolean</code> type.
189 */
190 public static final Show<Boolean> booleanShow = anyShow();
191
192 /**
193 * A show instance for the <code>byte</code> type.
194 */
195 public static final Show<Byte> byteShow = anyShow();
196
197 /**
198 * A show instance for the <code>char</code> type.
199 */
200 public static final Show<Character> charShow = anyShow();
201
202 /**
203 * A show instance for the <code>double</code> type.
204 */
205 public static final Show<Double> doubleShow = anyShow();
206
207 /**
208 * A show instance for the <code>float</code> type.
209 */
210 public static final Show<Float> floatShow = anyShow();
211
212 /**
213 * A show instance for the <code>int</code> type.
214 */
215 public static final Show<Integer> intShow = anyShow();
216
217 /**
218 * A show instance for the <code>BigInteger</code> type.
219 */
220 public static final Show<BigInteger> bigintShow = anyShow();
221
222 /**
223 * A show instance for the <code>BigDecimal</code> type.
224 */
225 public static final Show<BigDecimal> bigdecimalShow = anyShow();
226
227 /**
228 * A show instance for the <code>long</code> type.
229 */
230 public static final Show<Long> longShow = anyShow();
231
232 /**
233 * A show instance for the <code>short</code> type.
234 */
235 public static final Show<Short> shortShow = anyShow();
236
237 /**
238 * A show instance for the {@link String} type.
239 */
240 public static final Show<String> stringShow = anyShow();
241
242 /**
243 * A show instance for the {@link StringBuffer} type.
244 */
245 public static final Show<StringBuffer> stringBufferShow = anyShow();
246
247 /**
248 * A show instance for the {@link StringBuilder} type.
249 */
250 public static final Show<StringBuilder> stringBuilderShow = anyShow();
251
252 /**
253 * A show instance for the {@link Option} type.
254 *
255 * @param sa Show for the element of the option.
256 * @return A show instance for the {@link Option} type.
257 */
258 public static <A> Show<Option<A>> optionShow(final Show<A> sa) {
259 return new Show<Option<A>>(new F<Option<A>, Stream<Character>>() {
260 public Stream<Character> f(final Option<A> o) {
261 return o.isNone() ?
262 fromString("None") :
263 fromString("Some(").append(sa.f.f(o.some())).append(single(')'));
264 }
265 });
266 }
267
268 /**
269 * A show instance for the {@link Either} type.
270 *
271 * @param sa Show for the left side of the {@link Either}.
272 * @param sb Show for the right side of the {@link Either}.
273 * @return A show instance for the {@link Either} type.
274 */
275 public static <A, B> Show<Either<A, B>> eitherShow(final Show<A> sa, final Show<B> sb) {
276 return new Show<Either<A, B>>(new F<Either<A, B>, Stream<Character>>() {
277 public Stream<Character> f(final Either<A, B> e) {
278 return e.isLeft() ?
279 fromString("Left(").append(sa.f.f(e.left().value())).append(single(')')) :
280 fromString("Right(").append(sb.f.f(e.right().value())).append(single(')'));
281 }
282 });
283 }
284
285 /**
286 * A show instance for the {@link Validation} type.
287 *
288 * @param sa Show for the fail side of the {@link Validation}.
289 * @param sb Show for the success side of the {@link Validation}.
290 * @return A show instance for the {@link Validation} type.
291 */
292 public static <A, B> Show<Validation<A, B>> validationShow(final Show<A> sa, final Show<B> sb) {
293 return new Show<Validation<A, B>>(new F<Validation<A, B>, Stream<Character>>() {
294 public Stream<Character> f(final Validation<A, B> v) {
295 return v.isFail() ?
296 fromString("Fail(").append(sa.f.f(v.fail())).append(single(')')) :
297 fromString("Success(").append(sb.f.f(v.success())).append(single(')'));
298 }
299 });
300 }
301
302 /**
303 * A show instance for the {@link Stream} type.
304 *
305 * @param sa Show for the elements of the Stream.
306 * @return A show instance for the {@link Stream} type.
307 */
308 public static <A> Show<List<A>> listShow(final Show<A> sa) {
309 return new Show<List<A>>(new F<List<A>, Stream<Character>>() {
310 public Stream<Character> f(final List<A> as) {
311 return streamShow(sa).show(as.toStream());
312 }
313 });
314 }
315
316 /**
317 * A show instance for the {@link NonEmptyList} type.
318 *
319 * @param sa Show for the elements of the non-empty Stream.
320 * @return A show instance for the {@link NonEmptyList} type.
321 */
322 public static <A> Show<NonEmptyList<A>> nonEmptyListShow(final Show<A> sa) {
323 return listShow(sa).comap(NonEmptyList.<A>toList_());
324 }
325
326 /**
327 * A show instance for the {@link Tree} type.
328 *
329 * @param sa Show for the elements of the tree.
330 * @return A show instance for the {@link Tree} type.
331 */
332 public static <A> Show<Tree<A>> treeShow(final Show<A> sa) {
333 return new Show<Tree<A>>(new F<Tree<A>, Stream<Character>>() {
334 public Stream<Character> f(final Tree<A> a) {
335 final Stream<Character> b = sa.f.f(a.root())
336 .append(p1Show(streamShow(treeShow(sa))).f.f(a.subForest()))
337 .snoc(')');
338 return cons('(', p(b));
339 }
340 });
341 }
342
343 /**
344 * A show instance for the {@link Stream} type.
345 *
346 * @param sa Show for the elements of the stream.
347 * @return A show instance for the {@link Stream} type.
348 */
349 public static <A> Show<Stream<A>> streamShow(final Show<A> sa) {
350 return new Show<Stream<A>>(new F<Stream<A>, Stream<Character>>() {
351 public Stream<Character> f(final Stream<A> as) {
352 return join(as.map(sa.show_()).intersperse(fromString(",")).cons(fromString("<")).snoc(p(fromString(">"))));
353 }
354 });
355 }
356
357 /**
358 * A show instance for the {@link Array} type.
359 *
360 * @param sa Show for the elements of the array.
361 * @return A show instance for the {@link Array} type.
362 */
363 public static <A> Show<Array<A>> arrayShow(final Show<A> sa) {
364 return new Show<Array<A>>(new F<Array<A>, Stream<Character>>() {
365 public Stream<Character> f(final Array<A> as) {
366 Stream<Character> b = nil();
367
368 for (int i = 0; i < as.length(); i++) {
369 b = b.append(sa.f.f(as.get(i)));
370
371 if (i != as.length() - 1)
372 b = b.snoc(',');
373 }
374
375 b = b.snoc('}');
376
377 return cons('{', p(b));
378 }
379 });
380 }
381
382 /**
383 * A show instance for the {@link Class} type.
384 *
385 * @return A show instance for the {@link Class} type.
386 */
387 public static <A> Show<Class<A>> classShow() {
388 return new Show<Class<A>>(new F<fj.Class<A>, Stream<Character>>() {
389 public Stream<Character> f(final Class<A> c) {
390 return anyShow().show(c.clas());
391 }
392 });
393 }
394
395 /**
396 * A show instance for the {@link P1 tuple-1} type.
397 *
398 * @param sa Show for the first element of the tuple.
399 * @return A show instance for the {@link P1 tuple-1} type.
400 */
401 public static <A> Show<P1<A>> p1Show(final Show<A> sa) {
402 return new Show<P1<A>>(new F<P1<A>, Stream<Character>>() {
403 public Stream<Character> f(final P1<A> p) {
404 return cons('(', p(sa.show(p._1()))).snoc(')');
405 }
406 });
407 }
408
409 /**
410 * A show instance for the {@link P2 tuple-2} type.
411 *
412 * @param sa Show for the first element of the tuple.
413 * @param sb Show for the second element of the tuple.
414 * @return A show instance for the {@link P2 tuple-2} type.
415 */
416 public static <A, B> Show<P2<A, B>> p2Show(final Show<A> sa, final Show<B> sb) {
417 return new Show<P2<A, B>>(new F<P2<A, B>, Stream<Character>>() {
418 public Stream<Character> f(final P2<A, B> p) {
419 return cons('(', p(sa.show(p._1()))).snoc(',').append(sb.show(p._2())).snoc(')');
420 }
421 });
422 }
423
424 /**
425 * A show instance for the {@link P3 tuple-3} type.
426 *
427 * @param sa Show for the first element of the tuple.
428 * @param sb Show for the second element of the tuple.
429 * @param sc Show for the third element of the tuple.
430 * @return A show instance for the {@link P3 tuple-3} type.
431 */
432 public static <A, B, C> Show<P3<A, B, C>> p3Show(final Show<A> sa, final Show<B> sb, final Show<C> sc) {
433 return new Show<P3<A, B, C>>(new F<P3<A, B, C>, Stream<Character>>() {
434 public Stream<Character> f(final P3<A, B, C> p) {
435 return cons('(', p(sa.show(p._1()))).snoc(',').append(sb.show(p._2())).snoc(',')
436 .append(sc.show(p._3())).snoc(')');
437 }
438 });
439 }
440
441 /**
442 * A show instance for the {@link P4 tuple-4} type.
443 *
444 * @param sa Show for the first element of the tuple.
445 * @param sb Show for the second element of the tuple.
446 * @param sc Show for the third element of the tuple.
447 * @param sd Show for the fourth element of the tuple.
448 * @return A show instance for the {@link P4 tuple-4} type.
449 */
450 public static <A, B, C, D> Show<P4<A, B, C, D>> p4Show(final Show<A> sa, final Show<B> sb,
451 final Show<C> sc, final Show<D> sd) {
452 return new Show<P4<A, B, C, D>>(new F<P4<A, B, C, D>, Stream<Character>>() {
453 public Stream<Character> f(final P4<A, B, C, D> p) {
454 return cons('(', p(sa.show(p._1()))).snoc(',').append(sb.show(p._2())).snoc(',')
455 .append(sc.show(p._3())).snoc(',').append(sd.show(p._4())).snoc(')');
456 }
457 });
458 }
459
460 /**
461 * A show instance for the {@link P5 tuple-5} type.
462 *
463 * @param sa Show for the first element of the tuple.
464 * @param sb Show for the second element of the tuple.
465 * @param sc Show for the third element of the tuple.
466 * @param sd Show for the fourth element of the tuple.
467 * @param se Show for the fifth element of the tuple.
468 * @return A show instance for the {@link P5 tuple-5} type.
469 */
470 public static <A, B, C, D, E> Show<P5<A, B, C, D, E>> p5Show(final Show<A> sa, final Show<B> sb,
471 final Show<C> sc, final Show<D> sd, final Show<E> se) {
472 return new Show<P5<A, B, C, D, E>>(new F<P5<A, B, C, D, E>, Stream<Character>>() {
473 public Stream<Character> f(final P5<A, B, C, D, E> p) {
474 return cons('(', p(sa.show(p._1()))).snoc(',').append(sb.show(p._2())).snoc(',')
475 .append(sc.show(p._3())).snoc(',').append(sd.show(p._4())).snoc(',').append(se.show(p._5())).snoc(')');
476 }
477 });
478 }
479
480 /**
481 * A show instance for the {@link P6 tuple-6} type.
482 *
483 * @param sa Show for the first element of the tuple.
484 * @param sb Show for the second element of the tuple.
485 * @param sc Show for the third element of the tuple.
486 * @param sd Show for the fourth element of the tuple.
487 * @param se Show for the fifth element of the tuple.
488 * @param sf Show for the sixth element of the tuple.
489 * @return A show instance for the {@link P6 tuple-6} type.
490 */
491 public static <A, B, C, D, E, F$> Show<P6<A, B, C, D, E, F$>> p6Show(final Show<A> sa, final Show<B> sb,
492 final Show<C> sc, final Show<D> sd,
493 final Show<E> se, final Show<F$> sf) {
494 return new Show<P6<A, B, C, D, E, F$>>(new F<P6<A, B, C, D, E, F$>, Stream<Character>>() {
495 public Stream<Character> f(final P6<A, B, C, D, E, F$> p) {
496 return cons('(', p(sa.show(p._1()))).snoc(',').append(sb.show(p._2())).snoc(',')
497 .append(sc.show(p._3())).snoc(',').append(sd.show(p._4())).snoc(',')
498 .append(se.show(p._5())).snoc(',').append(sf.show(p._6())).snoc(')');
499 }
500 });
501 }
502
503 /**
504 * A show instance for the {@link P7 tuple-7} type.
505 *
506 * @param sa Show for the first element of the tuple.
507 * @param sb Show for the second element of the tuple.
508 * @param sc Show for the third element of the tuple.
509 * @param sd Show for the fourth element of the tuple.
510 * @param se Show for the fifth element of the tuple.
511 * @param sf Show for the sixth element of the tuple.
512 * @param sg Show for the seventh element of the tuple.
513 * @return A show instance for the {@link P7 tuple-7} type.
514 */
515 public static <A, B, C, D, E, F$, G> Show<P7<A, B, C, D, E, F$, G>> p7Show(final Show<A> sa, final Show<B> sb,
516 final Show<C> sc, final Show<D> sd,
517 final Show<E> se, final Show<F$> sf,
518 final Show<G> sg) {
519 return new Show<P7<A, B, C, D, E, F$, G>>(new F<P7<A, B, C, D, E, F$, G>, Stream<Character>>() {
520 public Stream<Character> f(final P7<A, B, C, D, E, F$, G> p) {
521 return cons('(', p(sa.show(p._1()))).snoc(',').append(sb.show(p._2())).snoc(',')
522 .append(sc.show(p._3())).snoc(',').append(sd.show(p._4())).snoc(',')
523 .append(se.show(p._5())).snoc(',').append(sf.show(p._6())).snoc(',').append(sg.show(p._7())).snoc(')');
524 }
525 });
526 }
527
528 /**
529 * A show instance for the {@link P8 tuple-8} type.
530 *
531 * @param sa Show for the first element of the tuple.
532 * @param sb Show for the second element of the tuple.
533 * @param sc Show for the third element of the tuple.
534 * @param sd Show for the fourth element of the tuple.
535 * @param se Show for the fifth element of the tuple.
536 * @param sf Show for the sixth element of the tuple.
537 * @param sg Show for the seventh element of the tuple.
538 * @param sh Show for the eighth element of the tuple.
539 * @return A show instance for the {@link P8 tuple-8} type.
540 */
541 public static <A, B, C, D, E, F$, G, H> Show<P8<A, B, C, D, E, F$, G, H>> p8Show(final Show<A> sa, final Show<B> sb,
542 final Show<C> sc, final Show<D> sd,
543 final Show<E> se, final Show<F$> sf,
544 final Show<G> sg, final Show<H> sh) {
545 return new Show<P8<A, B, C, D, E, F$, G, H>>(new F<P8<A, B, C, D, E, F$, G, H>, Stream<Character>>() {
546 public Stream<Character> f(final P8<A, B, C, D, E, F$, G, H> p) {
547 return cons('(', p(sa.show(p._1()))).snoc(',').append(sb.show(p._2())).snoc(',')
548 .append(sc.show(p._3())).snoc(',').append(sd.show(p._4())).snoc(',')
549 .append(se.show(p._5())).snoc(',').append(sf.show(p._6())).snoc(',')
550 .append(sg.show(p._7())).snoc(',').append(sh.show(p._8())).snoc(')');
551 }
552 });
553 }
554
555 /**
556 * A show instance for a vector-2.
557 *
558 * @param ea A show for the elements of the vector.
559 * @return A show instance for a vector-2.
560 */
561 public static <A> Show<V2<A>> v2Show(final Show<A> ea) {
562 return streamShow(ea).comap(V2.<A>toStream_());
563 }
564
565 /**
566 * A show instance for a vector-3.
567 *
568 * @param ea A show for the elements of the vector.
569 * @return A show instance for a vector-3.
570 */
571 public static <A> Show<V3<A>> v3Show(final Show<A> ea) {
572 return streamShow(ea).comap(V3.<A>toStream_());
573 }
574
575 /**
576 * A show instance for a vector-4.
577 *
578 * @param ea A show for the elements of the vector.
579 * @return A show instance for a vector-4.
580 */
581 public static <A> Show<V4<A>> v4Show(final Show<A> ea) {
582 return streamShow(ea).comap(V4.<A>toStream_());
583 }
584
585 /**
586 * A show instance for a vector-5.
587 *
588 * @param ea A show for the elements of the vector.
589 * @return A show instance for a vector-5.
590 */
591 public static <A> Show<V5<A>> v5Show(final Show<A> ea) {
592 return streamShow(ea).comap(V5.<A>toStream_());
593 }
594
595 /**
596 * A show instance for a vector-6.
597 *
598 * @param ea A show for the elements of the vector.
599 * @return A show instance for a vector-6.
600 */
601 public static <A> Show<V6<A>> v6Show(final Show<A> ea) {
602 return streamShow(ea).comap(V6.<A>toStream_());
603 }
604
605 /**
606 * A show instance for a vector-7.
607 *
608 * @param ea A show for the elements of the vector.
609 * @return A show instance for a vector-7.
610 */
611 public static <A> Show<V7<A>> v7Show(final Show<A> ea) {
612 return streamShow(ea).comap(V7.<A>toStream_());
613 }
614
615 /**
616 * A show instance for a vector-8.
617 *
618 * @param ea A show for the elements of the vector.
619 * @return A show instance for a vector-8.
620 */
621 public static <A> Show<V8<A>> v8Show(final Show<A> ea) {
622 return streamShow(ea).comap(V8.<A>toStream_());
623 }
624
625 /**
626 * A show instance for natural numbers.
627 */
628 public static final Show<Natural> naturalShow = bigintShow.comap(new F<Natural, BigInteger>() {
629 public BigInteger f(final Natural natural) {
630 return natural.bigIntegerValue();
631 }
632 });
633
634 /**
635 * A show instance for streams that splits into lines.
636 *
637 * @param sa A show instance for the elements of a stream.
638 * @return A show instance for streams that splits into lines.
639 */
640 public static <A> Show<Stream<A>> unlineShow(final Show<A> sa) {
641 return new Show<Stream<A>>(new F<Stream<A>, Stream<Character>>() {
642 public Stream<Character> f(final Stream<A> as) {
643 return join(as.map(sa.show_()).intersperse(fromString("\n")));
644 }
645 });
646 }
647
648 /**
649 * A show instance for lazy strings.
650 */
651 public static final Show<LazyString> lazyStringShow = show(new F<LazyString, Stream<Character>>() {
652 public Stream<Character> f(final LazyString string) {
653 return string.toStream();
654 }
655 });
656
657 /**
658 * A show instance for the empty heterogeneous Stream.
659 */
660 public static final Show<HList.HNil> HListShow = showS(Function.<HList.HNil, String>constant("Nil"));
661
662 /**
663 * A show instance for heterogeneous Streams.
664 *
665 * @param e A show instance for the first element of the Stream.
666 * @param l A show instance for the rest of the Stream.
667 * @return a show instance for heterogeneous Streams.
668 */
669 public static <E, L extends HList<L>> Show<HList.HCons<E, L>> HListShow(final Show<E> e, final Show<L> l) {
670 return show(new F<HList.HCons<E, L>, Stream<Character>>() {
671 public Stream<Character> f(final HList.HCons<E, L> c) {
672 return e.show(c.head()).cons('[').append(l.show(c.tail())).snoc(']');
673 }
674 });
675 }
676 }