That is a creative application of 6!:4! Thanks for sharing it.

In re: your specific atom-counting tool, can I suggest 

   (($~$)L:0"0~ i.@:#)z
+-----+-----+-+
|0 0 0|1 1 1|2|
|     |1 1 1|2|
|     |     |2|
|     |     |2|
+-----+-----+-+

?

Of course, we could extend this to work with arbitrarily-nested arrays, 
depending on what you're after.

In re: your larger question of variable scope, well there's a lot to say there, 
but I'll just leave it at: there are a lot of tradeoffs to consider, and in 
general J offers ways to achieve one's ends, without having to manage state (as 
seen in the above example).

-Dan

> On Jun 1, 2014, at 1:25 PM, Don Guinn <[email protected]> wrote:
> 
> One problem that has bugged me is the inability  for an explicit definition
> to modify a value accessible to a caller without having to create a global
> name. A tacit definition can reference the caller's local names but copulas
> do not work in a tacit definition. And names in an explicit definition are
> either local within the definition or global.
> 
> 
> It is not a serious problem, but there are a few situations where it cannot
> be avoided. Such as the verb argument to an adverb or conjunction.
> 
> 
> One problem I was dealing with was to identify where atoms were within a
> boxed array.
> 
> 
>   ]z=:'abc';(i.2 3);,.1 2 3 4
> 
> +---+-----+-+
> 
> |abc|0 1 2|1|
> 
> |   |3 4 5|2|
> 
> |   |     |3|
> 
> |   |     |4|
> 
> +---+-----+-+
> 
> 
> I wanted to create a corresponding boxed array containing
> 
> 
> +-----+-----+-+
> 
> |0 0 0|1 1 1|2|
> 
> |     |1 1 1|2|
> 
> |     |     |2|
> 
> |     |     |2|
> 
> +-----+-----+-+
> 
> 
> This gets the job done but n has to be global. It cannot be a local name.
> 
> 
>   inc=: 3 : '($y)$n=:>:n'
> 
>   n=:_1
> 
>   inc L:0 z
> 
> +-----+-----+-+
> 
> |0 0 0|1 1 1|2|
> 
> |     |1 1 1|2|
> 
> |     |     |2|
> 
> |     |     |2|
> 
> +-----+-----+-+
> 
> 
> I needed to find a primitive which has side effects. One to put in an
> explicit definition so each time the definition is executed the primitive
> returns a different result. One such is ? (or ?.) but the result is random
> and there is no guarantee that it won't return duplicate numbers. Another
> is 6!:9 '' (clock counter). It is always ascending but no guarantee that
> fast enough processors might return the same number in successive calls.
> 
> 
> A new foreign was added in J7 - 6!:4'' (parser calls) which does the job
> quite nicely.
> 
> 
>   (3 : '6!:4''''')"0 ]i.3 4
> 
> 6836 6837 6838 6839
> 
> 6840 6841 6842 6843
> 
> 6844 6845 6846 6847
> 
> 
> This gives the desired result. No global name required.
> 
> 
>   inc=. 4 : '($y)$n-~6!:4 '''''
> 
>   n=.2+6!:4 ''
> 
>   (n&inc) L:0 z
> 
> +-----+-----+-+
> 
> |0 0 0|1 1 1|2|
> 
> |     |1 1 1|2|
> 
> |     |     |2|
> 
> |     |     |2|
> 
> +-----+-----+-+
> 
> 
> I'm not sure what the purpose of 6!:4 is, but it is an interesting tool.
> ----------------------------------------------------------------------
> 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