Jan-Pieter, Interesting. I’m actually a bit surprised to learn that the “keyword” solution performs so well—keywords have apparently received more optimization love than I’d have expected.
Only if you’re in the mood for measurement—I’m curious about the performance of Raul’s F: approach vis-a-vis Pascal’s approach. I don’t have the fold add-on yet so I can’t run the test myself… Thanks. Ed Sent from my iPhone > On Oct 22, 2022, at 9:33 AM, Jan-Pieter Jacobs <janpieter.jac...@gmail.com> > wrote: > > Don't get me wrong, I'm also a huge tacit fan. However, I learned on the > way that it is not always the fastest way, especially when having > "irregular" problems. > > Out of curiosity, I benchmarked the results presented so far: > > coords=: 1e6 3 ?@$0 > pj=: }.^:(0=f@{.)^:_ > jp=: {{for_val. y do. if. f val do. val_index{y return. end. end.}} > rm=: 0&(]F..(_2 Z: f@[)) > +/2.95<+/"1 coords > 23 > f=: 2.95 < +/ > 100×pacex every 'rm coords';'pj coords';'jp coords' > 0.0261933 3136 > 0.003927 2240 > 0.00336666 1344 > > > It turns out Pascal's solution is doing surprisingly well, likely due to > J's fantastic virtual block system; otherwise it'd be duplicating the > remainder of the coords at every step. > > That said, it is difficult to compare numbers without knowing what you'd > want to have returned. Raul's version returns no result, Pascale's returns > the remainder of the list, and mine the first match... > > Best regards, > > Jan-Pieter > > > >> On Fri, 21 Oct 2022, 21:53 Ed Gottsman, <edward.j.gotts...@gmail.com> wrote: >> >> Jan-Pieter, >> >> Thank you for the solution. I’m a huge tacit fan (or maybe the word is >> “dilettante”) when writing J…less so when reading it a week later :-). In >> this particular case the tacit solution wins on elegance and (at least at >> the moment) seems perfectly clear. >> >> Thanks again. >> >> Ed >> >> Sent from my iPad >> >>> On Oct 21, 2022, at 1:55 PM, Jan-Pieter Jacobs < >> janpieter.jac...@gmail.com> wrote: >>> >>> I don't know whether you'r only looking for a tacit function, but this >>> would also likely work if fold is unavailable for you (untested): >>> >>> {{ for_val. y do. if. f val do. val_index{y return. end. end.}} coords >>> >>> Jan-Pieter >>> >>>> On Fri, 21 Oct 2022, 19:51 Ed Gottsman, <edward.j.gotts...@gmail.com> >> wrote: >>>> >>>> Pascal, >>>> >>>> Your original solution is perfect for me—I don’t happen to need the >>>> index. And there may in fact be no items with f = 1. >>>> >>>> Very clever: it gets a place of honor in the flashcard deck :-). >>>> >>>> Many thanks. >>>> >>>> Ed >>>> >>>> Sent from my iPad >>>> >>>>> On Oct 21, 2022, at 12:46 PM, 'Pascal Jasmin' via Programming < >>>> programm...@jsoftware.com> wrote: >>>>> >>>>> if you want the index rather than the coordinate values, then >>>>> >>>>> (1 i.~ f) y >>>>> >>>>> will "short circuit" to the first 1 >>>>> >>>>> and >>>>> >>>>> ({~ 1 i.~ f) >>>>> >>>>> may be faster to get first coordinates than my original solution, >> though >>>> errors if there are no items with 1 = f >>>>> >>>>> >>>>> >>>>> >>>>> On Friday, October 21, 2022 at 12:55:41 p.m. EDT, 'Pascal Jasmin' via >>>> Programming <programm...@jsoftware.com> wrote: >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> }.^:(0 = f@{.)^:_ >>>>> >>>>> will return the list with the head being the first f not equal 0. >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> On Friday, October 21, 2022 at 12:19:41 p.m. EDT, Ed Gottsman < >>>> edward.j.gotts...@gmail.com> wrote: >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> Hi. I’m blanking on something basic and research isn’t helping. When >>>> the blindingly obvious solution is revealed I will blame a combination >> of >>>> sleep deprivation and senescence. >>>>> >>>>> I’ve got a long list of coordinates ordered by desirability. I want to >>>> invoke f (a custom script) with each coordinate in turn. f may return 0 >>>> (failure), in which case I want to invoke it again with the next >>>> coordinate. When f (eventually) returns 1, I want to terminate—the >>>> remainder of the coordinates should not be processed. (Note that the >> very >>>> first invocation of f may return 1.) >>>>> >>>>> This *almost* feels like ^: in its Do While form. Almost. It also >>>> feels like SCs around list operations (though they seem to be limited to >>>> primitives). >>>>> >>>>> Help. >>>>> >>>>> Many thanks. >>>>> >>>>> Ed >>>>> >>>>> Sent from my iPad >>>>> ---------------------------------------------------------------------- >>>>> 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 >> ---------------------------------------------------------------------- >> 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