Advertisement
happy-barney

pretty-print data structure

Oct 12th, 2019
2,997
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 1.46 KB | None | 0 0
  1. my $indent = 0;
  2.  
  3. sub increment_indent () { $indent + 2; }
  4.  
  5. multi sub p (Hash $value) {
  6.     my @keys = $value.keys.sort;
  7.  
  8.     return print '{}'
  9.         if @keys.elems == 0;
  10.  
  11.     return print '{ ', @keys[0], ' => :', p($value{@keys[0]}), ' }'
  12.         if @keys.elems == 1
  13.             && !$value{@keys[0]}.isa( Iterable )
  14.     ;
  15.  
  16.     my $max = max @keys.map({.chars});
  17.  
  18.     {
  19.         temp $indent = increment_indent;
  20.         print "\{\n";
  21.         for @keys -> $key {
  22.             printf '%*s%-*s => ', $indent, '', $max, $key;
  23.             p $value{$key};
  24.             print ",\n";
  25.         }
  26.     }
  27.     print ' ' x $indent, '}';
  28.     print "\n" unless $indent;
  29. }
  30.  
  31. multi sub p (Array $value) {
  32.     return print '[]'
  33.         if $value.elems == 0;
  34.  
  35.     return print '[ ', p($value[0]), ' ]'
  36.         if $value.elems == 1
  37.             && !$value[0].isa( Iterable )
  38.     ;
  39.  
  40.     {
  41.         temp $indent = increment_indent;
  42.         print "[\n";
  43.         for $value.kv -> $index, $key {
  44.             printf '%*s[%d] = ', $indent, '', $index;
  45.             p $value[$index];
  46.             print ",\n";
  47.         }
  48.     }
  49.     print ' ' x $indent, ']';
  50.     print "\n" unless $indent;
  51. }
  52.  
  53. multi sub p (Any $value) {
  54.     print $value.perl;
  55. }
  56.  
  57. p {
  58.     statements => $[{:create-table(${:create("CREATE"), :table("TABLE"), :table-body(${:relational-table(${:relational-properties($[{:column-definition(${:column-name("sCompId"), :column-type(${:datatype("VARCHAR2"), :datatype-size("16")}), :not-null("not-null")})}, {:column-definition(${:column-name("sRoleCompId"), :column-type(${:datatype("VARCHAR2"), :datatype-size("16")})})}])})}), :table-name("AllRoleInactive")})},]
  59. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement