I've posted this verb before:

   amendV 
4 : '(> ({.^:((,1) -: $)) }: x) (>{: x) } y' 

It unpacks the arguments to } from boxes, and works but here is another neat 
approach.

eval =: 1 : ' a: 1 :  m'
advswap =: 2 : (':';'u x v eval y')

I've shown eval before.  It turns a string into any tacit evaluation or noun 
result.

advswap is designed to switch the x and m arguments to a dyadic adverb but 
because of the way it is written can do other things:

   2 (11)advswap '}' i.5 
0 1 11 3 4 

   2 +: advswap '+' 3 
10 
   2 +: advswap '&+' 3 
10 

The last 2 examples show passing a verb as the u argument.  Whether v argument 
evaluates to  a verb or adverb... x v y happens.  If u is a noun then v must be 
an adverb string.

advswap used with the primary adverb swapping function is most useful in 
forming forks where the indexes to replace are computed while the replacement 
value is fixed.

it can also be applied to dyadic '@.v' '`:n'.  Maybe 'u^:' and probably a few 
foreigns.

   +`- 2 advswap '`:0' 3 
5 _1 

   +`- 2 advswap '`:0' "0 ] 3 1 
5 1 

a more advanced/useful example:

draw a random box inside a square grid (but make sure it is 1 space away from 
right and bottom edges)

   rndbox 
[: <@:(+ ([: i. 2 >. ]))/"1 ] (] ,.~ [: ? 2 >. <:@:<:@:<:@:-) [: ? <: 

for a 10x8 random matrix (of 0 to 2) replace a random sized box with 3s.
   ( <@:rndbox 3 advswap '}' [: ? ] $ 3:) 10 8 
2 1 2 1 0 2 0 0 
1 3 3 3 3 0 2 1 
1 3 3 3 3 2 1 2 
2 3 3 3 3 1 0 1 
0 3 3 3 3 2 1 1 
1 3 3 3 3 0 2 1 
0 3 3 3 3 0 0 1 
2 3 3 3 3 1 0 1 
1 3 3 3 3 2 0 0 
2 0 2 2 2 2 1 0 

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

Reply via email to