Another cool function that expands on the usefulness of tie and tieA. (Thanks
to Pepe for the hint towards isgerund) is one that can take the ar of a
conjunction (or multiadverb) and bind it with another ar (of verb or noun/
conjunction parameter) to make a single "super" ar (bound conjunction type 4)
lr2ar =: 3 : 'y aar' NB.assumes adverb preferred.
ar2lr =: 3 : 'o =. i.0 for_i. y do. a =. (i 5!:0) label_. o =. o , < 5!:5
<''a'' end. o' :. lr2ar
Ugt =: 4 : 0 NB. y: vector of ars (gerund), x: 1 or matching len ar of
modifiers usually conjunctions or multiadverbs.
if. -.@isgerund x do. x =. x aar end.
if. -.@isgerund y do. y =. y aar end.
x (;: inv&.ar2lr)"1@:,. y
)
Ugt (U of gerund and tieA) stitches a list of gerunds together, takes the lr of
each row, and then uses aar (lr is param) to make the ar of the combination.
Some fun functions that involve stitching ars together are short circuit
functions.
AND1 =: '(u aar , , ''^:'' aar ,. v)`:6' daF
OR =: '((_2 { ]`u) , , (''`'' tieA (_1 { ]`u)) ,"1 ''@.'' aar ,. v)`:6' daF NB.
shortcircuit agenda u may be gerund if not ]`u is final if condition. v is
gerund of tests
ANDe =: '((1 { u) , , ( (0 { u) Ugt ''`'') ,"1 ''@.'' aar ,. v)`:6' daF
AND =: 'if. 2 = # ]`u do. u v AND1 else. u v ANDe end.' daF
The easiest of these is AND1. This is a double adverb that takes a gerund
(v)and a verb (u). the v gerund is a list of verb ars. These are stitched
with ^:, raveled, and then u's ar prepended. `:6 transforms into verb
+: _5&> (0 < ]) (0 =*) tieD tieD AND1
+:^:(_5&>)^:(0 < ])^:(0 = *)
+: _5&>`(0 < ])`(0 =*) AND1
+:^:(_5&>)^:(0 < ])^:(0 = *)
function doubles a number if all 3 conditions are met.
OR is a shortcircuited agenda. v is similar gerund of test clauses. u is
optionally gerund of 2 verbs (else and true action). if not passed as gerund of
2, the "else" clause of ] is used. A fairly important consideration is to not
assign any names to any gerund components. Doing so would force you to fix
results which may mess up localed parameters. in OR, @. is stitched with each
v clause. The true verb is stitched with ` and this result stitched with
previous "table", which is then raveled, and the else clause prepended.
-:`+: _5&> (0 < ]) (0 =*) tieD tieD OR
-:`+:@.(_5&>)`+:@.(0 < ])`+:@.(0 = *)
(+: * -:) _5&> (0 < ]) (0 =*) tieD tieD OR
]`(+: * -:)@.(_5&>)`(+: * -:)@.(0 < ])`(+: * -:)@.(0 = *)
in looking at the output pattern, its easy to notice the repeated 4 clauses:
`,then,@.,test. As long as the intended output is a verb, `:6 will group ar
conjunctions the same as lr (commandline) execution
- tieA '@' tieA + tieA'@' `:6
-@+@
@+@-
|syntax error
@+(@-)
(@+)(@-)
(@+)@-
|syntax error
+(-@)
-@+
This preamble is useful to understand ANDe. It creates an and shortcircuit
with @. and an else clause. The else`true clause is passed as gerund u. The
tests are v clause (gerund). like OR @. and tests are stitched. Ugt is used to
stitch/bind (else `) the 2 are stitched together, and somewhat amazingly `:6
figures out the parentheses (parens entire "u" clause when it "unrolls" the ar
and reverses them).
]`+: 0&< 5&> (1 4 8 16 64 128 e.~ 3!:0) tieD tieD ANDe
]`(]`(]`+:@.(0&<))@.(5&>))@.(1 4 8 16 64 128 e.~ 3!:0)
'not numeric or between 0 and 5'"_`+: 0&< 5&> (1 4 8 16 64 128 e.~ 3!:0) tieD
tieD ANDe 'sdf'
not numeric or between 0 and 5
AND guards between the 2 adverbs, depending on whether u is a gerund (contains
else clause)
]`+: 0&< 5&> tieD AND
]`(]`+:@.(0&<))@.(5&>)
+: 0&< 5&> tieD AND
+:^:(0&<)^:(5&>)
2 + 0&<@] 5&>@] tieD AND 4
6
2 -`+ 0&<@] 5&>@] tieD AND 8
_6
A final cool thing to do is revisiting conjoin to handle behaviour like gerund/
(alternating insert)
lena =: (&>)/(@:(,&< $~ &.> >.&#)) NB. prevent lenght error by extending
shorter side with alternate copies ($ rule)
lenf =: (&>)/(@:(,&< {.~ &.> >.&#)) NB. prevent lenght error by extending
shorter side with fills ({. rule)
lenfD =: 1 : '(&>)/(@:(,&< {.(!.m)~ &.> >.&#))' NB. specify fill !.m
1 2 ,: lena i.5
1 2 1 2 1
0 1 2 3 4
1 2 ,: lenf i.5
1 2 0 0 0
0 1 2 3 4
1 2 ,: 11 lenfD i.5
1 2 11 11 11
0 1 2 3 4
lena (length alternate) will form the basis for insN (insert Noun). Instead of
equalizing lengths, x is made at most as long as y, then stitched,raveled,
curtailed. (works like / but for nouns)
a couple of triple adverbs similar to OR and AND. u1 is action clause (left
arg to ^: or @.) u0 is gerund or verb consisting of dyadic logical combiners.
v is gerund of test clauses.
insN =: }:@:,@:([`([ $~ <.&#)@.(>&#) ,.~ lena ])
iflogic =: 'if. -. u isgerundA do. u =. u`'''' end. (u insN n )`:6 ''u^:v''
daF' daF
iflogicE =: 'if. -. u isgerundA do. u =. u`'''' end. (u insN n )`:6 ''[email protected]''
daF' daF
NB. gerund u0: *.`+.
+: *.`+. 0&< (2 | ]) 5&> tieD tieD iflogic
+:^:(0&< *. (2 | ]) +. 5&>)NB. u0 just *.
+: *. 0&< (2 | ]) 5&> tieD tieD iflogic
+:^:(0&< *. (2 | ]) *. 5&>)
-:`+: +. 0&< (2 | ]) 5&> tieD tieD iflogicE
-:`+:@.(0&< +. (2 | ]) +. 5&>)
corrected copy of code mentioned so far, (with above defs copied at bottom)
lr =: 3 : '5!:5 < ''y'''
daFx =: (0 : ) 1 : ('a =. (''2 : ('', (lr m) , '') u'') label_. 1 : (''u
1 :'' , quote a)')
tieAD =: 'u tieA v' daF
tieADs =: 'v tieA u' daF
eval_z_ =: 1 : ' a: 1 : m'
isNoun_z_ =: (0 = 4!:0 ( :: 0:))@:<
ar =: 1 : '5!:1 <''u'''
aar =: 1 : 'if. isNoun ''u'' do. q =. m eval else. q =. u end. 5!:1 < ''q'' '
isgerund =: 0:`(0 -.@e. 3 : ('y (5!:0)';'1')"0)@.(0 < L.) :: 0:
isgerundA =: 1 : ' if. isNoun ''u'' do. isgerund m else. 0 end.'
tie =: 2 : 'if. u isgerundA do. if. v isgerundA do. m ar , v ar else. m , v ar
end. else. if. v isgerundA do. u ar , n else. u ar , v ar end. end. '
tieA =: 2 : 'if. u isgerundA do. if. v isgerundA do. m ar , v ar else. m , v
aar end. else. if. v isgerundA do. u aar , n else. u aar , v aar end. end. '
daF =: 1 : ('a =. (''2 : '', (quote m) , '' u'') label_. 1 : (''u 1 :'' ,
quote a)')
tieD =: 'u tie v' daF
tieDs =: 'v tie u' daF
Ug =: 2 : 'a =. (m { n) if. 1 =#a do. a 5!:0 end.'
MA1 =: (1 : 0)
ff =. ({: m) 5!:0
f =. 1 : ('U =. Ug m'; ff )
(}: m) f
)
ncS =: 3 : 'z=.y 1 :y label_. 4!:0 <''z'' ' ::(_2:)
lrA =: 1 : '5!:5 < ''u'''
eval =: 1 : ' a: 1 : m'
ismodstring =: 1 : 'if. 0 = 4!:0 <''u'' do. try. q =. m eval catch. 0 return.
end. 1 2 e.~ 4!:0 <''q''else. 0 end. '
ncA =: 1 : 'if. 3 ~: 4!:0 < ''u'' do. if. m ismodstring do. m ; ncS m else. 0
;~ ''('', m lrA ,'')'' end. else. 3;~ ''('', u lrA ,'')'' end.'
aatrain =: 0 daFx
if. 0 -.@-: 1{:: a =. v ncA do. n =. ,: a end.
if. 1 = 1 {:: (;: inv {."1 a =.(u ncA , n)) ncA do. a aatrain else.
(;: inv {."1 a) eval end.
)
MA =: 'tieD MA1' aatrain
MA3 =: 'tieD tieD' MA
NB. fork definitions
F =: '0 U 1 U 2 U' MA3
F =: '(i.3) U `:6' MA3
F =: 'm `:6' MA3
conjoin =: '(0 (joinstring ;/) }. i.@# m) U `:6' MA
lr2ar =: 3 : 'y aar' NB.assumes adverb preferred.
ar2lr =: 3 : 'o =. i.0 for_i. y do. a =. (i 5!:0) label_. o =. o , < 5!:5
<''a'' end. o' :. lr2ar
Ugt =: 4 : 0 NB. y: vector of ars (gerund), x: 1 or matching len ar of
modifiers usually conjunctions or multiadverbs.
if. -.@isgerund x do. x =. x aar end.
if. -.@isgerund y do. y =. y aar end.
x (;: inv&.ar2lr)"1@:,. y
)
AND1 =: '(u aar , , ''^:'' aar ,. v)`:6' daF
OR =: '((_2 { ]`u) , , (''`'' tieA (_1 { ]`u)) ,"1 ''@.'' aar ,. v)`:6' daF NB.
shortcircuit agenda u may be gerund if not ]`u is final if condition. v is
gerund of tests
ANDe =: '((1 { u) , , ( (0 { u) Ugt ''`'') ,"1 ''@.'' aar ,. v)`:6' daF
AND =: 'if. 2 = # ]`u do. u v AND1 else. u v ANDe end.' daF
lena =: (&>)/(@:(,&< $~ &.> >.&#)) NB. prevent lenght error by extending
shorter side with alternate copies ($ rule)
lenf =: (&>)/(@:(,&< {.~ &.> >.&#)) NB. prevent lenght error by extending
shorter side with fills ({. rule)
lenfD =: 1 : '(&>)/(@:(,&< {.(!.m)~ &.> >.&#))' NB. specify fill !.m
insN =: }:@:,@:([`([ $~ <.&#)@.(>&#) ,.~ lena ])
iflogic =: 'if. -. u isgerundA do. u =. u`'''' end. (u insN n )`:6 ''u^:v''
daF' daF
iflogicE =: 'if. -. u isgerundA do. u =. u`'''' end. (u insN n )`:6 ''[email protected]''
daF' daF
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm