[Jprogramming] verb to template a string
Joe Bogner
joebogner at gmail.com
Thu Jan 7 14:49:08 UTC 2016
Thanks Henry - that's covers it nicely and would translate into a
description of the spec (had I wrote it)
Reduce X, a list of boxes, to a single string by looking at the
contents of each box. If the contents (or type) of the value in the
box is not a number, append the contents to the string being
constructed. If the value is a number, use the number to index into Y,
a list of boxes, and append that content to the string.
Stating it this way, it becomes apparent it's a general form of
reduction with a parameter that needs to be available at each step of
the reduction.
I'm curious why you included 8 (JFL) in the list of types. It would
meet the criteria of a number in the spec, but might of course doesn't
work for { ... Either way it's fine, just interested in your thinking
There's a small issue if Y only has one value
('Dear ';0;' - Hello!') applyTemplate (<'abc')
|length error: applyTemplate
But that can be fixed by enlisting it:
('Dear ';0;' - Hello!') applyTemplate (,<'abc')
Dear abc - Hello!
applyTemplate =: ;@:([`{@.(1 4 8 e.~ 3!:0@>@[)"0 _/)
_/ is particularly interesting and threw me off. I then realized it's
just controlling the rank of the verb, u, for / . It specifies the
rank as 0 _ so it's saying apply the verb to each cell of X and use
the entirety of Y... Makes sense. To confirm, I plugged 1 in for _ and
it was fine. Dissect helped me figure this out
('Dear ';0;' - Hello!') ;@:([`{@.(1 4 8 e.~ 3!:0@>@[)"0 1 /) (,<'abc')
Dear abc - Hello!
I also like the clarity of the statement more so than using adverse ::
Pascal - Thanks for the reply as well. Yours acts more like a printf
with the token in the string instead of outside in a box. I opted for
the boxed view so I wouldn't have to worry about parsing and picking a
good token delimiter. The binary code of :::: is an interesting
approach though.
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!
On Thu, Jan 7, 2016 at 7:43 AM, Henry Rich <henryhrich at gmail.com> wrote:
> I finally understand the spec.
>
> applyTemplate =: ;@:([`{@.(1 4 8 e.~ 3!:0@>@[)"0 _/)
>
> Henry Rich
>
>
> On 1/6/2016 9:37 PM, Joe Bogner wrote:
>>
>> I often find myself joining strings with x/y
>>
>> The most recent example was:
>> ('http://real-chart.finance.yahoo.com/table.csv?s=' ,
>> ]@,&'&a=10&b=2&c=1993&d=00&e=6&f=2016&g=d&ignore=.csv') 'TICKER'
>>
>> I think it would be handy to have a simple verb that can take a box of
>> strings / indices and merge it with a box of inputs
>>
>> Something like:
>>
>> ('hello ';0;0;0) applyTemplate ('a';'b')
>>
>> Would result in
>>
>> hello aaa
>>
>> ('hello ';0;' hello';1) applyTemplate ('a';'b')
>>
>> would be:
>>
>> hello a hello b
>>
>> I struggled with this for about 25 minutes to make it tacit and gave
>> up. Any takers?
>>
>> (I thought about using printf but figured it was too heavy)
>>
>>
>> applyTemplate =: 4 : 0
>> str=.''
>> for_p. x do.
>> str=.str,(( y {::~ ]) :: ] ) (>p)
>> end.
>> )
>>
>> ('hello ';0;' hello ';1) applyTemplate ('a';'b')
>> ----------------------------------------------------------------------
>> 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