Here is a bit of a breakthrough on this type of code: (uses definitions quoted below including those from Jose) consider
]`+vvtie"1 0]2 3 ]`+vvtie"1 0]2 3 ┌─┬─┬─────────────────┐ │]│+│┌─┬─────────────┐│ │ │ ││"│┌─────┬─────┐││ │ │ ││ ││┌─┬─┐│┌─┬─┐│││ │ │ ││ │││0│2│││0│_││││ │ │ ││ ││└─┴─┘│└─┴─┘│││ │ │ ││ │└─────┴─────┘││ │ │ │└─┴─────────────┘│ ├─┼─┼─────────────────┤ │]│+│┌─┬─────────────┐│ │ │ ││"│┌─────┬─────┐││ │ │ ││ ││┌─┬─┐│┌─┬─┐│││ │ │ ││ │││0│3│││0│_││││ │ │ ││ ││└─┴─┘│└─┴─┘│││ │ │ ││ │└─────┴─────┘││ │ │ │└─┴─────────────┘│ └─┴─┴─────────────────┘ or to avoid copying definitions: 2:`+`] ,: 3:`+`] ┌──┬─┬─┐ │2:│+│]│ ├──┼─┼─┤ │3:│+│]│ └──┴─┴─┘ that created 2 gerund trains that are equivalent to functions storing 2 functions f1 and f2. Although this is a good essay, it doesn't provide the same functionality described below http://www.jsoftware.com/pipermail/programming/2013-November/034177.html train6 =: 4 : ' x `:6 y' train0 =: 4 : ' x `:0 y' With these verbs we can apply lists of functions to lists of arguments. One of the more useful ones has mysterious magic behind it: ..f1 f2 y (acutally wrong... see below. It is (f1 f2) y. A hook.) (<"1 2:`+`] ,: 3:`+`]) train6 4 6 9 11 ..each f1 and f2 to y (<"1 2:`+`] ,: 3:`+`]) train6"0 1] 4 6 6 8 7 9 ..f1 y1 , f2 y2 NB. one of the most useful forms (<"1 2:`+`] ,: 3:`+`]) train6"0 ] 4 6 6 9 or (<"1 2:`+`] ,: 3:`+`]) train6 &> 4 6 6 9 combinable as: ....f1 each y , f2 each y (<"1 2:`+`] ,: 3:`+`]) train6 each"0 1 ] 1 4 6;2 3;1 1 ┌─────┬───┬───┐ │3 6 8│4 5│3 3│ ├─────┼───┼───┤ │4 7 9│5 6│4 4│ └─────┴───┴───┘ train0 is a bit less useful, but still (<"1 2:`+`] ,: 3:`+`]) train0 each"0 1 ] 1 2 ┌─────┬─────┐ │2 1 1│2 2 2│ ├─────┼─────┤ │3 1 1│3 2 2│ └─────┴─────┘ 2:`+`] `:0 ] 1 2 1 1 can do train of trains: ( ( [ , +`'' ,])/ <"1 2:`+`] ,: 3:`+`]) train6 1 3 5 7 11 15 where: ( ( [ , +`'' ,])/ <"1 2:`+`] ,: 3:`+`]) ┌────────┬─┬────────┐ │┌──┬─┬─┐│+│┌──┬─┬─┐│ ││2:│+│]││ │ │3:│+│]││ │└──┴─┴─┘│ │└──┴─┴─┘│ └────────┴─┴────────┘ ( [ , +`'' ,])/ <"1 ]`+vvtie"1 0]2 3 4 not shown due to formatting prob on this comp, but joins 3 outer verb trains with + as pivot (( [ , +`'' ,])/ <"1 ]`+vvtie"1 0]2 3 4) train6 1 3 5 12 18 24 notice that there is a generic function adverb there: jointrains=: 1 : '( [ , (u`'''') ,])/' +jointrains <"1 ]`+vvtie"1 0]2 3 4 this still doesn't quite get to a compose trains function, but its close. one way to get compose is to use the [: form in trains: composetrains =: [:`'' , {:,~[: jointrains@:}: (composetrains <"1 ]`+vvtie"1 0]2 3) train6 1 2 4 6 7 9 (composetrains <"1 ]`+vvtie"1 0]2 3 4) train6 1 2 4 10 11 13 (both compose and join require 2+ verbs) From: Pascal Jasmin <godspiral2...@yahoo.ca> To: "programm...@jsoftware.com" <programm...@jsoftware.com> Cc: Sent: Friday, February 21, 2014 2:18:07 PM Subject: Re: [Jprogramming] Generating code from templates The usefulness of Cloak (and verbifying) is that you can then apply adverbs and conjunctions (" ~ ...)to what are essentially adverbs and conjunctions. Consider the now badly named conjunction vtie which is an enhancement to tie designed to always produce verbs (promoting nouns with "_), and so is useful for the most common gerund cases. see: http://www.jsoftware.com/pipermail/programming/2014-February/034913.html vtie =: 2 : 'try. ((u"_)`(v"_)"_)^:(0=L.) (u`v) catch. try. u`(v"_) catch. (u"_)`(v"_) end. end.' 'tie vvtie'=. (Cloak @: < each) @: ;: '` vtie' This produces the 2 hooks (+ 2:) and (+ 3:), and returns them as a single storable result (equivalent to +&3 and +&2 (+`'' vvtie"1 0 ] 2 3) ┌─┬─────────────────┐ │+│┌─┬─────────────┐│ │ ││"│┌─────┬─────┐││ │ ││ ││┌─┬─┐│┌─┬─┐│││ │ ││ │││0│2│││0│_││││ │ ││ ││└─┴─┘│└─┴─┘│││ │ ││ │└─────┴─────┘││ │ │└─┴─────────────┘│ ├─┼─────────────────┤ │+│┌─┬─────────────┐│ │ ││"│┌─────┬─────┐││ │ ││ ││┌─┬─┐│┌─┬─┐│││ │ ││ │││0│3│││0│_││││ │ ││ ││└─┴─┘│└─┴─┘│││ │ ││ │└─────┴─────┘││ │ │└─┴─────────────┘│ └─┴─────────────────┘ ((+`'' vvtie"1 0 ] 2 3){~0) `:6 ] 5 NB. selects the first function and applies it to 5 7 (]`+ vvtie"1 0 ] 2) `:6 NB. another way of (]+2:) ie. +&2 ] + 2"_ (]`+ vvtie"1 0 ] 2) `:6 ] 3 5 when searching for a way to apply rank to `:6 to get a list of results, I found this: (<"1 ]`+ vvtie"1 0 ] 2 3) `:6 &> 4 9 That will apply f1 f2 4 , where f1 is (]+2), f2 is (]+3). I havent found how to get the list (append) of each function can parameterize both the start and end of an implicit definition this way. [`+ vvtie"1 0 ] #`] ┌─┬─┬─┐ │[│+│#│ ├─┼─┼─┤ │[│+│]│ └─┴─┴─┘ [`+ vvtie~"1 0 ] #`] ┌─┬─┬─┐ │#│[│+│ ├─┼─┼─┤ │]│[│+│ └─┴─┴─┘ %`# vvtie~"1 0 ] +/`(-/) ┌───────┬─┬─┐ │┌─┬───┐│%│#│ ││/│┌─┐││ │ │ ││ ││+│││ │ │ ││ │└─┘││ │ │ │└─┴───┘│ │ │ ├───────┼─┼─┤ │┌─┬───┐│%│#│ ││/│┌─┐││ │ │ ││ ││-│││ │ │ ││ │└─┘││ │ │ │└─┴───┘│ │ │ └───────┴─┴─┘ ----- Original Message ----- From: Pascal Jasmin <godspiral2...@yahoo.ca> To: "programm...@jsoftware.com" <programm...@jsoftware.com> Cc: Sent: Friday, February 21, 2014 1:11:32 PM Subject: Re: [Jprogramming] Generating code from templates neat. tie ,^:(0:``) tie&'' f. ,^:(0:``)&'' which is using the gerund (v1`v2) version of ^: . This appears to be undocumented in that v2 here is a conjunction(`). I assume that using an adverb as v2 is impossible? The other peculiarity is that v1 is 0: which means that , never executes. Is that right? ----- Original Message ----- From: Jose Mario Quintana <jose.mario.quint...@gmail.com> To: Programming forum <programm...@jsoftware.com> Cc: Sent: Friday, February 21, 2014 11:31:56 AM Subject: Re: [Jprogramming] Generating code from templates The verbalized conjunctions tie and rank can be produced as follows: 9!:14 '' j701/2011-01-10/11:25 Cloak=. (0:`)(,^:) NB. Conjunction to cloak adverbs and conjunctions as verbs Cloak=. (5!:1@<'Cloak')Cloak NB. Cloaking Cloak 'tie rank'=. (Cloak @: < each) @: ;: '` "' NB. Cloaking tie (`) and rank (") cf=. {.@(tie&'')@(rank&_)"0 f. 100"_`(200"_)`(300"_) -: cf 100 200 300 1 Cloak can also be produced without using ^: (although with a caveat). Members interested in following the beginnings of the story can look at http://www.jsoftware.com/pipermail/programming/2013-March/031883.html . On Thu, Feb 20, 2014 at 8:52 PM, Raul Miller <rauldmil...@gmail.com> wrote: > This would be more fun if you provided definitions for tie and rank (and > others you use) in an addon. > > Without that, it feels like deus ex machina (and does not execute for me > like it does for you). > > Thanks, > > -- > Raul > > > On Thu, Feb 20, 2014 at 6:06 PM, Jose Mario Quintana < > jose.mario.quint...@gmail.com> wrote: > > > " > > Anyway, thought I'd share, and see if anyone else has useful tools for > > manipulating J code (metaprogramming). > > " > > > > Dan, I will try to show some meta-programming exercises soon. I am > jumping > > into the darkness hoping that I will land well; so far, so good. In the > > meantime, the following is a F^4 underground verb corresponding to Raul's > > explicit verb (using verbalized primitive conjunctions): > > > > cf=. {.@(tie&'')@(rank&_)"0 f. > > > > 100"_`(200"_)`(300"_) -: cf 100 200 300 > > 1 > > > > > > > > On Thu, Feb 20, 2014 at 2:39 PM, Dan Bron <j...@bron.us> wrote: > > > > > 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 > > > > > ---------------------------------------------------------------------- > > For information about J forums see http://www.jsoftware.com/forums.htm > > > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm