A little less typy,
('Dear ', ] , ' - Hello!'"_) 'abc'
Y =: (&{::)(@:])
('Dear ', 0 Y , '. ' , 1 Y , ' - Hello!'"_) ;:'Mr abc'
Dear Mr. abc - Hello!
----- Original Message -----
From: Raul Miller <[email protected]>
To: Programming forum <[email protected]>
Sent: Thursday, January 7, 2016 11:16 AM
Subject: Re: [Jprogramming] verb to template a string
On Thu, Jan 7, 2016 at 9:49 AM, Joe Bogner <[email protected]> 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