There are many ways to evoke code in J. Usually we do this when our code needs
to be calculated based on some input, then executed. Most often we want to
calculate verbs and apply them to nouns, and for this case either verb_string
128!:2 noun or ".noun_resultant_sentence will suffice.
However, I have sometimes needed to calculate and execute code which does not
produce a noun. There is no neat way to do this in J. If we are content to
use gerunds, then we can simply use 5!:0 or `:6 to evoke an arbitrary
gerund or train of gerunds. But gerunds not always convenient to manipulate,
nor does their construction come naturally to a J programmer. We would really
like to build code using strings.
So we're left with a couple of options. We can use ~ , but that requires
our code be named (*), as in:
".'name=.',sentence
'name'~
which is ungainly. Our final option is + (1 : sentence) . As a named
utility, it might look like this:
ae =: 1 : '+ 1 : u'
There are drawbacks to this approach (e.g. the sentence to execute better not
reply on u as a name; OTOH it can use control words). But the largest one
for me is that the utility is coded as an explicit adverb. I would prefer a
tacit version (just 'cause). And now I have it:
ae =: ("_) (`(<1;~,'0')) (((<,':') <@:, (<1;~,'0') <@:, (,'0')
<@:(;<) ,&>@:,@:(<^:(0=L.)))`) (`:6) ((<1;~,'0')`) (`:6)
Watch the wrap; the definition is all one line. A less obfuscated version of
this utility is available from
http://www.jsoftware.com/svn/DanBron/trunk/environment/anonymous_evoke.ijs .
For example:
+ '/' ae 1 2 3
6
'+/ % #' ae i. 10
4.5
1: '@:' ae 2:
1:@:2:
Of course, given the constraints, no solution to this problem can be "truly
tacit". By definition : produces explicit code. But the named utility
itself is tacit. And, more amusing to me, it makes no use of names. Not even
the special name u like its explicit counterpart.
Just thought I'd share this with all you tacit fans.
-Dan
* It would be nice if m~ lifted the requirement for the the noun m to be a
name; there's no syntactic reason it couldn't be any valid J sentence. Or
maybe we could have a new n in n : m which directly produced the result
of the script m .
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm