[Jprogramming] A templating framework for J expressions
R.E. Boss
r.e.boss at outlook.com
Fri Sep 11 13:39:18 UTC 2020
Much appreciated.
I have got something to chew on, thanks.
R.E. Boss
-----Original Message-----
From: Programming <programming-bounces at forums.jsoftware.com> On Behalf Of 'Pascal Jasmin' via Programming
Sent: vrijdag 11 september 2020 15:09
To: programming at jsoftware.com
Subject: Re: [Jprogramming] A templating framework for J expressions
Its an easier way to write functions that include multiple parameters, modifiers as parameters, lets you bind/curry any combination of those parameters, and provides easy autoquoting of a parameter in chosen circumstances. It behaves like extra u v x y parameters with multiple substitutions of each token, though for that exact behaviour parentheses around either the data-parameter or the token-variable need to be included. WIthout parentheses, the templating engine can insert any string into the template, and so fragment combinations of linear expressions is possible.
A walkthrough of some of the functions
bb =: 1 : 'dltb each (''`''&cut) m'
used to cut linear representations on ` . This is a helper function that simplifies parameter building. Similar to traditional language function call that cuts parameters on ",". ` is used because there are better gerund builder functions for J, and so ` is non essential. Templates discard the 2: to 9: and _2: to _9: verbs from use, as they too are non essential, and happen to not appear in the addon library.
' `% `# ' bb
┌┬─┬─┐
││%│#│
└┴─┴─┘
builds 3 parameters. A null parameter passed to template leaves its corresponding token unbound. It is also possible to pass a verb or noun directly to template that will just bind the first (lowest magnitude) parameter.
NoteV_z_ =: 1 : '(0 (0 :)) label_. 0 (m :)'
Allows embedding documentation in an explicit function. Adverb where m is the left parameter to : . Can be enhanced to provide side effect database/dictionary/queryable function documentation.
jt is the templating engine conjunction. Its documentation string:
template n includes tokens in form of number colon (except for 0: 1:). if n string it is tokenized.
those tokens will be replaced with blocked string expressions in m
a repeated token gets replaced multiple times.
a negative token gets replaced by the quoted m block.
m blocks will replace right to left in order of sorted token absolute values (ie. if tokens are 2: _3: 5:, leftmost of 3 m blocks will replace 5: Rightmost will replace 2:)
a null('') m block skips the token binding. Replaces token with +100 magnitude. Therefore limit of 100 unique replacement tokens.
too short of an m list, just partially binds the lowest magnitude token values.
an unboxed u (verb) parameter is replaced with a single boxed lr. unboxed m is boxed. Less efficient to pass one parameter at a time, but supported.
if not fully bound, returns (jt n) adverb with n partially bound tokens.
if fully bound, returns result string
multiline n can be coerced into ` delimited single line to fit this function.
The Jxp function turns a string into an explicit or tacit expression based on its content. It also is in embedded documentation format.
One of the practical applications of this templating engine is creating/enabling "modifier trains" that mix conjunctions and adverbs. Examples of how those modifier trains should bind parameters is provided, but it is easy to create alternatives. A next step is a function that takes a series of adverbs and conjunctions, that autobuilds a train of CC CA AC combinators based on the parameter types.
On Friday, September 11, 2020, 05:45:43 a.m. EDT, R.E. Boss <r.e.boss at outlook.com> wrote:
Am I the only one who don't understand a word of this high-browed J?
Even not why I should make an effort to digest it.
R.E. Boss
-----Original Message-----
From: Programming <programming-bounces at forums.jsoftware.com> On Behalf Of 'Pascal Jasmin' via Programming
Sent: vrijdag 11 september 2020 05:42
To: Programming forum <programming at jsoftware.com>
Subject: [Jprogramming] A templating framework for J expressions
cocurrent 'z'
NB. utilities pasted from jpp, hopefully did not forget any:
NoteV_z_ =: 1 : '(0 (0 :)) label_. 0 (m :)'
bb =: 1 : 'dltb each (''`''&cut) m'
lrA =: 1 : '5!:5 < ''u'''
eval =: 1 : 'if. 2 ~: 3!:0 m do. m else. a: 1 : m end.'
numcolonnot01 =: {.@((0 1 _1 -. at e.~ ".@}:) *. ':' = {:)`0:@.('''' = {:)
a2blr =: 1 : 'if. 3 = nc <''u'' do. < u lrA else. boxopen m end.'
jt =: 2 NoteV
template n includes tokens in form of number colon (except for 0: 1:). if n string it is tokenized.
those tokens will be replaced with blocked string expressions in m
a repeated token gets replaced multiple times.
a negative token gets replaced by the quoted m block.
m blocks will replace right to left in order of sorted token absolute values (ie. if tokens are 2: _3: 5:, leftmost of 3 m blocks will replace 5: Rightmost will replace 2:)
a null('') m block skips the token binding. Replaces token with +100 magnitude. Limit of 100 unique replacement tokens.
too short of an m list, just partially binds the lowest magnitude token values.
an unboxed u (verb) parameter is replaced with a single boxed lr. unboxed m is boxed. Less efficient to pass one parameter at a time, but supported.
if not fully bound, returns (jt n) adverb with n partially bound tokens.
if fully bound, returns result string
multiline n can be coerced into ` delimited single line to fit this function.
)
n =. ;:^:(0 = L.) n
m =. |. u a2blr
Ilist =. I. {. at numcolonnot01 &> n
nlist =. ".@}: each Ilist { n NB. numeric version of each n: param.
'too many parameters given' assert (#m) <: # ~. | each nlist
snlist =. /:@:( (| , ]) each) nlist
r =. m #~ (#m) {. #/.~ snlist { | each nlist
a =.|: jthelper r ,. (nlist (,<)"0 Ilist) {~ snlist {.~ #r
n =. ({. a) (; {: a)} n
if. 1 e. numcolonnot01 &> n do. (jt n) else. ;:inv L:1 <^:(1 = L.) n end.
)
jthelper =: 3 : 0("1)
'a b c' =. y
if. '' -: a do. a =. ':' ,~ ": (* * 100 + |) b
elseif. 0 > b do. a=. quote a end.
a;c
)
Jxp =: cutLF v2a 1 NoteV
creates tacit or explicit expression based on m.
m is either string (auto boxed to 1) or "multiline equivalent" boxed format.
if includes v/n, explicit conjunction
if includes u/m, explicit adverb
if includes x but not single line :, explicit dyad
if includes y, explicit monad
else tacit or noun expression, eval'd if not multiline.
)
if. 1 = #m do. m =. (>m) bb end. NB. ` is coded multiline
m =. ;: each m
if. +./ , (;: 'v n') e.S:1 m do. 2 : (;: inv each m)
elseif. +./ , (;: 'u m') e.S:1 m do. 1 : (;: inv each m)
elseif. (0 = (< ;: ':') e. m) *. +./ (;: 'x') e.S:1 m do. 4 : (;: inv each m)
elseif. +./ (;: 'y') e.S:1 m do. 3 : (;: inv each m)
elseif. 1 = #m do. (> ;: inv each m) eval else. ;: inv each m end.
)
some examples this permits are an easy way to create conjunction trains:
CC =: 2 : '( u ; v ) jt ''u (4: 3:) (2: v)'' Jxp'
CA =: 2 : '( u ; v ) jt ''(u 3: v) 2:'' Jxp'
AC =: 2 : '( u ; v ) jt ''(u 3:) 2: v'' Jxp'
CC is a conjunction that returns an adverb that returns a conjunction (3/5 parameters total)
+: +'@' CC '@:'- NB. binding order same as if it were called in multi adverb form.
+ at +:@:-
The same function can be done with more flexible bindings as a 5 parameter adverb:
' `+:` `@` ' bb jt '5: (4: 3:) (2: 6:)' Jxp
(jt(;:'+:(104:@)(102:106:)'))Jxp
'+`-` @:' bb ' `+:` `@` ' bb jt '5: (4: 3:) (2: 6:)' Jxp
- at +:@:+
Both the CC and 5-adverb version are much easier to write and read than the nested quote monstrocity that is the alternative.
Jxp is a suggestion I believe Henry made on wiki. At first, I didn't like it because the intent of a function is both usefully described, and an early intentional decision. But it is convenient when that initial intent can change:
'+`-` @:' bb ' `+:` `@` ' bb jt '5: (4: 3:) (2: 6:) y' Jxp
'@`@:' bb jt '+ _3: CC _2:' Jxp NB. negative token markers get quoted before replacement.
2 : 'u ( + @ ) ( @: v )'
----------------------------------------------------------------------
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
More information about the Programming
mailing list