> Here's a spacious version of the same idea .
Earlier in my J career, I also wanted to visualize how trains were parsed, but
found the boxed display intrusive. I came up with a solution similar to
Oleg's, here. I called the utility unfold [1]. Here's an example of its
use and output:
load 'strings printf'
unfold 'deb'
0 #~( )@( )
1 +.( ) ' '&~:
2 1:|.( )
3 ></\
unfold 'formatS_jprintf_'
0 ;:^:_1@:( )^:( ):(
)
1 ":&.> 32&=@( ) ( )~;:^:_1@:( )^:(
)
2 3!:0 ( )~2&{ ":&.>
32&=@( )
3 ( ){.]
3!:0
4 <.#
Here's the definition:
depth =. (({. -~ +/\@:(-/))@:(=/))
parenDepth =. (;:'()')&depth
groupByDepth =. (<:@:# }."1 >) @: (}: #^:_1&.>~ i.@:<:@:#
<"1@:(=/) >@:{:) @: (</.~&:>/ , {:) @: ((,&< ,&.>~ (,&<~
#&a:@:#)@:i.@:(>./)@:]) parenDepth)
formattedCount =. (i. ":"0~ j.&0@:>:@:#@:":)@:#
reformat =. ,.&:>/@:(<@:>"1)@:|:@:(,.~ <@,&'
'"1@:formattedCount)
parse =. ;:@:(5!:5)@:({.@:;:^:(L.=0:)) :. reformat
unfold =: groupByDepth&.parse f.
Applying unfold to itself is an interesting exercise.
-Dan
[1] From one of my old utility scripts, at
http://www.jsoftware.com/svn/DanBron/trunk/uncategorized/danutils5.ijs (line
1356). It needed some adaptation to work around a bug in J6
(http://www.jsoftware.com/jwiki/System/Interpreter/Bugs#fix_ambivalent).
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm