This looks similar to boxing a recursively nested parenthesized expression
(or XML, or whatever).  So, we can use recursion:
 
           foo =: ] <`(<@$:);.1~ 1 , 2 ~:/\ ]={.
           foo 1 1 2 2 3 3 4 2 3 4 4 1 2 2 2 1
        +---+---------------------------+-+-------+-+
        |1 1|+---+---------+-+---------+|1|+-----+|1|
        |   ||2 2|+---+---+|2|+-+-----+|| ||2 2 2|| |
        |   ||   ||3 3|+-+|| ||3|+---+||| |+-----+| |
        |   ||   ||   ||4||| || ||4 4|||| |       | |
        |   ||   ||   |+-+|| || |+---+||| |       | |
        |   ||   |+---+---+| |+-+-----+|| |       | |
        |   |+---+---------+-+---------+| |       | |
        +---+---------------------------+-+-------+-+
           
We start by cutting the list into "top level" items, and "nested" items.
Having done this, we only need to recurse into the nested items and process
them similarly. 
 
Of course, since series of top-level items are grouped together until
interrupted a nested item, the list will end up alternating between groups
(boxes) of top-level items and groups of nested items, like A B A B A B. So
we only need to recurse into the odd boxes, and leave the even boxes alone.

 
The verb's relative simplicity arises from this observation, and the cute
gerundial form of ;. which allows us to say f`g;.1 where f and g will be
applied cyclically to the partitions of the argument (but if we didn't have
that feature, we could emulate it with ^: ).
 
-Dan
 
PS: 
 
This assumes you trust  $:  not to abort with a stack error, which it does
sometimes.  Though I'd only expect that with long and deeply-nested
arguments. 
 
But if you wanted to be certain ab initio (or you just have time to kill and
find J amusing), you could try rewriting the verb using a bottom-up approach
based on ^: .  You even could start with the code you (REB) provided to me
in a similar thread back in 2009 [1].
 
[1] Thread "Recursive nesting, avoiding recursion"
        
http://www.jsoftware.com/pipermail/programming/2009-August/016181.html
 
PPS:  Happy Thanksgiving!

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of R.E. Boss
Sent: Thursday, November 24, 2011 9:27 AM
To: Programming forum
Subject: [Jprogramming] multiple boxing

Given an array of positive integers A starting with 1 and such that  2 > >./
2 -~/\ A  

 (a next atom in A is at most 1 larger than the preceding one).

 

Now I want the items to be boxed according to this example

 

    foo  1 1 2 2 3 3 4 2 3 4 4 1 2 2 2 1

+---+---------------------------+-+-------+-+

|1 1|+---+---------+-+---------+|1|+-----+|1|

|   ||2 2|+---+---+|2|+-+-----+|| ||2 2 2|| |

|   ||   ||3 3|+-+|| ||3|+---+||| |+-----+| |

|   ||   ||   ||4||| || ||4 4|||| |       | |

|   ||   ||   |+-+|| || |+---+||| |       | |

|   ||   |+---+---+| |+-+-----+|| |       | |

|   |+---+---------+-+---------+| |       | |

+---+---------------------------+-+-------+-+

 

 

Any suggestions for foo?

 

 

R.E. Boss

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to