[Jprogramming] verb to template a string
'Pascal Jasmin' via Programming
programming at jsoftware.com
Thu Jan 7 18:37:00 UTC 2016
silly example version and flexible to unboxed numeric and string Y
maybenum =: 0&".^:(] -:&(, $~ 1 -.~ $) ":@:(0&".^:(2 = 3!:0)))
ApplyTemplate=. ('`' maybenum each@:(cut^:(0 = L.@])) [) ;@:({:: ::[L:0 _) ":each@:(<^:(2=3!:0))@]
Y =: (&({::))(@:])
'0` bottles of beer on the wall. `0` bottles of beer. You take `1` down, pass the `1` around, `2` bottles of beer on the wall.' ApplyTemplate("1) 1 1 0 ,~ ((2 Y - 1 Y ),~ 1 Y ,~ 0 Y - 1 Y )^:(0 < 1 Y)^:(<49) (] , -/)99 2
----- Original Message -----
From: 'Pascal Jasmin' via Programming <programming at jsoftware.com>
To: "programming at jsoftware.com" <programming at jsoftware.com>
Sent: Thursday, January 7, 2016 12:06 PM
Subject: Re: [Jprogramming] verb to template a string
less typy version :)
maybenum =: 0&".^:(] -:&(, $~ 1 -.~ $) ":@:(0&".^:(2 = 3!:0)))
ApplyTemplate=. ('`' maybenum each@:(cut^:(0 = L.@])) [) ;@:({:: ::[L:0 _) ]
'Hello `0` world! `1' ApplyTemplate (;:'abc de')
Hello abc world! de
still works with typy version:
('Hello ';0;' world! ';1) ApplyTemplate (;:'abc de')
Hello abc world! de
----- Original Message -----
From: Jose Mario Quintana <jose.mario.quintana at gmail.com>
To: Programming forum <programming at jsoftware.com>
Sent: Thursday, January 7, 2016 11:46 AM
Subject: Re: [Jprogramming] verb to template a string
Joe,
This (or something similar) might be sufficient depending on what you are
really trying to accomplish:
ApplyTemplate=. [ ;@:({:: ::[L:0 _) ,@:]
('Hello ';0;' world! ';0) ApplyTemplate (<'abc')
Hello abc world! abc
('Hello ';0;' world! ';1) ApplyTemplate (;:'abc de')
Hello abc world! de
On Thu, Jan 7, 2016 at 11:16 AM, Raul Miller <rauldmiller at gmail.com> wrote:
> On Thu, Jan 7, 2016 at 9:49 AM, Joe Bogner <joebogner at gmail.com> wrote:
> > I think the concept of applyTemplate is useful because joining strings
> > can be tedious when J evaluates them as a train (] , ' foo') -- no
> > noun allowed in the right tine! and that can get trippy with longer
> > strings:
> >
> > ('Dear', ] , ' - Hello!') 'abc' -- nope!
> >
> > ('Dear ', ] @ ,&' - Hello!') 'abc' -- typically a trial and error and
> > not as easily red
> >
> > ('Dear ';0;' - Hello!') applyTemplate ('abc';'')
> > Dear abc - Hello!
>
> That particular issue can be resolved using an appropriate bit of
> nothing to anchor the train.
>
> mt1=:''"_
> ('Dear ', ] , ' - Hello!',mt1) 'abc'
> Dear abc - Hello!
>
> The 1 in the name refers to the rank of the empty value it supplies.
> In some cases you would want a rank 2 value.
>
> And of course, the consequence is that you can use J's trains as
> "templates" containing verbs. Whether or not that's appropriate will
> depend on what you are doing - there still might be contexts where
> boxed index/string combos are the right answer. But in a lot of the
> cases something simpler will do. (This seems to be the case so very
> often, though there can also be good reasons to use other
> approaches...).
>
> FYI,
>
> --
> Raul
> ----------------------------------------------------------------------
> 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