Looks nice. Here is a continuation of my gerund manipulation approach:
conjoin=: (,@:[ <@:; <@:])"1
('@' conjoin -.`*) `:6
-.@*
'@' conjoin (-.`'') ," 1 ,. *`-`+
┌──────────┬──────────┬──────────┐
│┌─┬──────┐│┌─┬──────┐│┌─┬──────┐│
││@│┌──┬─┐│││@│┌──┬─┐│││@│┌──┬─┐││
││ ││-.│*││││ ││-.│-││││ ││-.│+│││
││ │└──┴─┘│││ │└──┴─┘│││ │└──┴─┘││
│└─┴──────┘│└─┴──────┘│└─┴──────┘│
└──────────┴──────────┴──────────┘
stitchtrains =: ,.&(G2train@:(<"1))
(-.`'') stitchtrains ,. *`-`+
┌──┬─┐
│-.│*│
├──┼─┤
│-.│-│
├──┼─┤
│-.│+│
└──┴─┘
('@' conjoin (-.`'') stitchtrains *`'') `:6
-.@*
G2train turns out to be a pretty good idea for working with informal lists of
verbs and combining them into trains/tacit definitions. But working with
tables of gerunds is a bad idea bc `:6 fails when a:/fills are present.
To complete my last example:
shortcircuits =: ((,'@:'); [<@:;])/ @:(_2&}. , (,'@:') <@:; _2&{ <@:, {:)
(< shortcircuits '^:' conjoin (]`>vvtie"1 0]9 12 2) stitchtrains~ ]`+vvtie"1
0]5+i.3) `:6
(] + 5"_)^:(] > 9"_)@:((] + 6"_)^:(] > 12"_)@:((] + 7"_)^:(] > 2"_)))
AndDoTrains =: ([: <@:shortcircuits '^:' conjoin [ stitchtrains ])
( (]`>vvtie"1 0]9 12 2) AndDoTrains~ ]`+vvtie"1 0]5+i.3)`:6
(] + 5"_)^:(] > 9"_)@:((] + 6"_)^:(] > 12"_)@:((] + 7"_)^:(] > 2"_)))
though it turns out this was the wrong spec, as it has the same result as the
[: version (fails > 12, but still execs later >9)
( (]`>vvtie"1 0]9 12 2) AndDoTrains~ ]`+vvtie"1 0]5+i.3)`:6"0 ] 1 2 3 4 5 6
1 2 15 16 17 24
So, I'm no longer sure there is a way to tacitly break execution on the first
fail test. Or actually, I need to paren the whole left side of any ^: ...
which was probably a needed manipulation function anyway.
_____________________
( shortcircuit=.train o ((power&.:train ~)`(at&.:train ~)/) o |.)
v0`t0`v1`t1`v2`t2
v0^:t0@:v1^:t1@:v2^:t2
ShortCircuit=. shortcircuit sv
[: v0 t0 v1 t1 v2 t2 v3 t3 v4 t4 ShortCircuit
v0^:t0@:v1^:t1@:v2^:t2@:v3^:t3@:v4^:t4
----- Original Message -----
code at bottom of message,
G2train is needed to make "formal trains" even though `:6 can sometimes
evaluate a list of boxed verbs as a train, if we add conjunctions to the train,
it needs to be formalized as a group of verbs.
( '^:'(4<]) addconj"1 0 G2train <"1 ]`+vvtie"1 0]2 4) train6"0 0] 5 4
7 4
( '^:'(4<]) addconj"1 0 G2train <"1 ]`+vvtie"1 0]2 4) train6"1 0] 5 4
11 4
(+jointrains '^:'(4<]) addconj"1 0 G2train <"1 ]`+vvtie"1 0]2 4) train6"1
0] 5 4 2 1
16 8 4 2
(+jointrains '^:'(4<]) addconj"1 0 G2train <"1 ]`+vvtie"1 0]2 4)`:6
(] + 2"_)^:(4 < ]) + (] + 4"_)^:(4 < ])
addconjar can add tests to each train.
(+jointrains (4<])`(2<]) '^:' addconjar"0 G2train <"1 ]`+vvtie"1 0]2 4)
train6"1 0] 5 4 3 1
16 12 10 2
seems a useful way to build and chain a series of power conjunctions
(composetrains (7<])`(2<]) '^:' addconjar"0 G2train <"1 ]`+vvtie"1 0]2 4)
train6"1 0] 5 4 3 1
11 10 7 1
but doesn't quite work the same as a real short circuit:
(composetrains (9<])`(12<])`(2<]) '^:' addconjar"0 G2train <"1 ]`+vvtie"1 0]
5+i.3) train6"1 0] 10 4 3 1
28 16 15 1
here the 3 input fails the >12 test, but still evaluates and passes the (3+7)>
9 test.
(composetrains (9<])`(12<])`(2<]) '^:' addconjar"0 G2train <"1 ]`+vvtie"1
0] 5+i.3) `:6
[: (] + 5"_)^:(9 < ]) [: (] + 6"_)^:(12 < ]) (] + 7"_)^:(2 < ])
note that the tests can also be buit by parameters (and doesn't need the
G2train processing to pass the `:6 parser.:
(composetrains ( <"1 ]`> vvtie"1 0]9 12 2) '^:' addconjar"0 G2train <"1
]`+vvtie"1 0] 5+i.3) `:6
[:^:((] + 5"_) (] > 9"_)) [:^:((] + 6"_) (] > 12"_))^:((] + 7"_) (] > 2"_))
the above is not a useless pattern, but it would still be nice to have a real
short circuit.
the pattern: doN^:testN...@:do2^:test2@:do1^:test1
might be implementable using clever `:3, and ^:`@: but its not straightforawd
to tie two conjunctions (^:`@: is error). I'm not sure that would work, but
there is a simpler approach anyway. Rewrite addconj as a verb where y is a
pair of gerunds corresponding to left and right conjunction arguments, then
join pairs of do, test with ^:, and resulting pairs with @:. maybe next post.
the code:
Cloak1=: (0:`)(,^:) NB. Conjunction to cloak adverbs and conjunctions as verbs
Cloak=: (5!:1@<'Cloak1')Cloak1 NB. Cloaking Cloak
vtie =: 2 : 'try. ((u"_)`(v"_)"_)^:(0=L.) (u`v) catch. try. u`(v"_) catch.
(u"_)`(v"_) end. end.'
vvtie =: Cloak <'vtie'
train6 =: 4 : ' x `:6 y'
jointrains=: 1 : '( [ , (u`'''') ,])/'
composetrains =: [:`'' , {:,~[: jointrains@:}:
ar =: 1 : '5!:1 <''u'''
G2train =: 3 : '; y `:6(`'''')' each
addconj =: 1 : (':';' <(,x);<y ; u ar')("1 0)
addconj2 =: 1 : (':';' <(,u);<y ; x ar')
addconjar =: 1 : (':';' <(,u);<y ; x')
----- Original Message -----
From: Pascal Jasmin <[email protected]>
To: "[email protected]" <[email protected]>
Cc:
Sent: Monday, March 3, 2014 7:57:42 AM
Subject: Re: [Jprogramming] Generating code from templates
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 <[email protected]>
To: "[email protected]" <[email protected]>
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 <[email protected]>
To: "[email protected]" <[email protected]>
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 <[email protected]>
To: Programming forum <[email protected]>
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 <[email protected]> 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 <
> [email protected]> 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 <[email protected]> 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"_
> > >
> > > [email protected]
> > > 100"_
> > >
> > > [email protected] '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
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm