There was a question on StackOverflow today on generating constant functions from the items of an array. As in, 100 200 300 <==> 100"_`(200"_)`(300"_) .
As a more general question, how can we specify code templates and then use them to dynamically produce gerunds for later execution? * Here's one idiom I've found useful in such situations: substit =: [^:(__-:])L:_ 0 template =: __"_ NB. The template; note the __ TEMPLATE =: {. template f.`'' NB. Atomic rep of template GERUND =: TEMPLATE substit"_1 _~ 100 * 1 + i.3 GERUND`:6 (100"_) (200"_) 300"_ GERUND@.0 100"_ GERUND@.2 'hello' 300 The basic idea is to "fill in the blanks" in the template with the inputs. Here, the blank is __ (negative infinity), so we look for that in the gerund template, and when we find it, we replace it with the input. I kind of like how the verb does this: first, we only bother looking at level 0 (because __ is unboxed, so it will never appear above level 0), and second, the verb is passive until __ is detected (because ^:(]-:__) is ^:0, i.e. "do nothing and just return the RHA" until then). One possible issue is what we use to denote "blank". If we're going to allow users to pass in any gerund, then he could potentially want to include __ as part of the code to be produced, rather than replaced. There are a couple possible ways to address that limitation. First, we could use a value which a user is very unlikely to want to produce (as opposed to replace), i.e. _. : substit =: [^:(1-:128!:5@])L:_ 0 SPARTA =: ({. _."_ `'') substit"_1 _~ 300 SPARTA`:6 300"_ Or, we could define substit as an adverb (adjective), which takes the "blank" as an argument: Substit =: (-:&) (@]) ([^:) (L:_ 0) substit =: 'blank' Substit substit [^:(-:&'blank'@])L:_ 0 ; , ((< {. 'blank'&;`'') substit~&.> ;:'fill in the blanks' )`:0 '!' fill!in!the!blanks! But if we do that, we have to start wondering about the 0 in L:_ 0 , because the user's "blank" might be boxed, and therefore have L.>0 . Of course, we could fix this up by making the L: dynamic: Substit =: adverb : '[^:(m-:])L:(_,L.m)' NOTNULL =: ({. ~:&a:` '') a: Substit~ <'#N/A#' NOTNULL`:6 ~:&(<'#N/A#') But it might be more fun to go meta-meta: (({. substit f.`'') substit~ _999)`:6 [^:(_999 -: ])L:_ 0 Here, define the "standard" blank to be __, and then let the user change it by using substit :) Anyway, thought I'd share, and see if anyone else has useful tools for manipulating J code (metaprogramming). -Dan * For a recent example of where such metaprogramming might be useful, or convenient, see the postscript of: http://www.jsoftware.com/pipermail/programming/2013-December/034373.html Other examples can be found with a forum archive search for the string L:0 _ ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm