]A=:'Hello ';'world! ' -------T-------┐ │Hello │world! │ L------+-------- ]B=:'abc';'de' ----T--┐ │abc│de│ L---+--- ,>|:2 2$A,B Hello abc world! de
Linda -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Henry Rich Sent: Thursday, January 7, 2016 3:38 PM To: [email protected] Subject: Re: [Jprogramming] verb to template a string Pepe's version is classy. I avoided :: because I wasn't sure what would happen if there was an empty character-string on the left. I included 8 because sometimes you get a float where you wouldn't expect one: 3!:0 (2^3) 8 Works fine for { (2^3) { i. 9 8 I now think it would be better to test for character-string; and handling the empty-list case and rewriting to use ^: and allow non-strings on the right gives applyTemplate =: ;@:(":&.>)@({~^:(2 ~: 3!:0@>@])"_ 0~ ,) The original / at the end was unnecessary. Henry Rich On 1/7/2016 9:49 AM, Joe Bogner wrote: > 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 <[email protected]> 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 > ---------------------------------------------------------------------- > 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
