Now uses ar's, and a simple definition language.
first step is to come up with a good replacement for ` (tie). Basically tie
needs to always produce gerunds, appending any non gerunds to an existing
gerund. tieA is like tie but if one side is a string, it attempts to interpret
it as a modifier, or unbound verb name. It makes sense to have a separate
function for special vs noun processing of strings. (multiline definitions have
line break after them)
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. '
'/\' tieA + tieA'/' tieA '\' tie (+@-) tieA 'asdfasdf'
┌─────────┬─┬─┬─┬─────────┬────────┐
│┌─┬─────┐│+│/│\│┌─┬─────┐│asdfasdf│
││4│┌─┬─┐││ │ │ ││@│┌─┬─┐││ │
││ ││/│\│││ │ │ ││ ││+│-│││ │
││ │└─┴─┘││ │ │ ││ │└─┴─┘││ │
│└─┴─────┘│ │ │ │└─┴─────┘│ │
└─────────┴─┴─┴─┴─────────┴────────┘
tie as double adverb also useful
daF =: 1 : ('a =. (''2 : '', (quote m) , '' u'') label_. 1 : (''u 1 :'' ,
quote a)')
tieD =: 'u tie v' daF
tieDs =: 'v tie u' daF
The main advantage of double adverbs is composing them with other adverbs
(conjunctions can't), and there are no unnecessary parentheses for the v side.
-/\ +@- (3 + *) tieD tieD tieA '/\'
┌─────────────┬─────────┬───────────────┬─────────┐
│┌─┬─────────┐│┌─┬─────┐│┌─┬───────────┐│┌─┬─────┐│
││\│┌───────┐│││@│┌─┬─┐│││3│┌─────┬─┬─┐│││4│┌─┬─┐││
││ ││┌─┬───┐││││ ││+│-││││ ││┌─┬─┐│+│*││││ ││/│\│││
││ │││/│┌─┐│││││ │└─┴─┘│││ │││0│3││ │ ││││ │└─┴─┘││
││ │││ ││-│││││└─┴─────┘││ ││└─┴─┘│ │ │││└─┴─────┘│
││ │││ │└─┘││││ ││ │└─────┴─┴─┘││ │
││ ││└─┴───┘│││ │└─┴───────────┘│ │
││ │└───────┘││ │ │ │
│└─┴─────────┘│ │ │ │
└─────────────┴─────────┴───────────────┴─────────┘
Note that the code to create double adverbs looks identical to conjunctions.
To make longer multiadverbs just as easy, the main approach is to turn the
multiadverb into a double adverb where the right param is a code string, and
the left param, the gerund of all other parameters.
A helper function U (capital) is available (silently inserted into adverb) to
extract any gerund. Its based on Ug below. If only one index to U is provided
then the gerund is unpacked (5!:0 turned into verb noun or modifier)
Ug =: 2 : 'a =. (m { n) if. 1 =#a do. a 5!:0 end.'
MA1 =: (1 : 0)
'`ff' =. {: m
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
aatrain is previous utility used to compose double adverbs into infinite
trains. used in defintion of MA (Multiadverb). A feature of aatrain is that
any adverb "anchored" by it can add adverbs and mutliadverbs by string as an
extended adverb (MA3- 3 param multiadverb does this)
+/ % # F NB. parenthesless
+/ % #
+/ (% +:@# F) NB. parens just to show contained adverb.
+/ % +:@#
+: + +/ (% # '+:@:(2 U) 1 U~ 0 U'MA3) F
+: + +:@:# %~ +/
'@' - + % tieD tieD tieAD ('/'tieADs) 'm' MA NB. use ('m' MA) for easy debug
┌─┬─┬─┬─┬─┐
│/│@│-│+│%│
└─┴─┴─┴─┴─┘
'@' - + % tieD tieD tieAD '/'tieADs '2 0 3 2 0 U `:6 ' MA
-/ + -/
modifier params can mess up things if they are not at the left end. tieDs and
tieADs
(('@' (- (+ % tieD) tieD) tieAD) '/'tieADs) 'm' MA NB. operator groupings.
┌─┬─┬─┬─┬─┐
│/│@│-│+│%│
└─┴─┴─┴─┴─┘
can make multiadverbs that work with arbitrary sized parameters, though best
practice is to form the gerund "manually", and a guideline, modifiers are
leftest parameter(s).
'+ -:' 3 - +:@*/ tieD tieD tieAD '(0 (joinstring ;/) }. i.@# m) U `:6' MA
3 (+ -:) - (+ -:) +:@*/
conjoin =: '(0 (joinstring ;/) }. i.@# m) U `:6' MA
'@:' +:/ - +:@*/ tieD tieD tieAD conjoin
+:/@:-@:(+:@*/)
Can also write multiline versions
test =: (cutLF 0 : 0) MA3
a =. 0 1 2 U
a `:6
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm