Yes, probably something wrong with my stop-function (rank perhaps?). When I run

(f^:(0.8"_ > ?@0:)^:5) y

(replacing the stop function with a random function), everything seems to work 
fine.

It seems like this functionality is something that could easily be added to Z: 
though, since _3 Z: n aborts execution without producing a return value after n 
iterations. But maybe the functionality is, as you say, quirky :). I’m into 
metaheuristic search, where this kind of pattern is not uncommon (try to find 
good solutions for a fixed set of iterations and/or time, but stop early if a 
good enough solution is found).

Regards, Anton Wallgren
On 26 May 2020, 10:02 +0200, Anton Wallgren <[email protected]>, wrote:
> Yes, maybe for loop is the way to go. It just seems very un-J :). I did try 
> f^:v^:n earlier, but it didn’t work, it ran f 2*n times. But when I try it 
> now with my simple example,
>
> (2&*)^:(10&> @ ])^:3 ] 1
>
> returns 8, and
>
> (2&*)^:(10&> @ ])^:5 ] 1
>
> returns 16, as expected. Maybe there is something going on with my “v” in my 
> earlier code.
>
> Thanks, Anton Wallgren
> On 26 May 2020, 09:11 +0200, Raul Miller <[email protected]>, wrote:
> > ^:_ doesn't impose the max limit on iterations, though...
> >
> > I think, for something that quirky, I'd just use a for loop. That
> > said, f^:v^:n would work -- I'm just not sure it's optimized
> > adequately.
> >
> > I hope this helps,
> >
> > --
> > Raul
> >
> > On Tue, May 26, 2020 at 2:39 AM 'Rob Hodgkinson' via Programming
> > <[email protected]> wrote:
> > >
> > > Hi Anton, welcome to J.
> > >
> > > This is a further parameter to the power operator (^:) described here:
> > >
> > > https://code.jsoftware.com/wiki/Vocabulary/Loopless 
> > > <https://code.jsoftware.com/wiki/Vocabulary/Loopless> Section “Types of 
> > > Loops” and the row in the table “Apply a verb repeatedly”, “Until a 
> > > condition is met”.
> > > Use Power ([x] u^:v^:_ y)
> > >
> > > For your example, double while a condition (eg let’s say while the 
> > > sequence is < 100 and stop with the value that breaches that condition …)
> > >
> > > 2&* ^:(100>])^:_ (1) NB. Sequence here is 1,2,4,8,16,32,64,128
> > > 128
> > >
> > > 2&* ^:(100>])^:_ (5) NB. Sequence is 5, 10, 20, 40, 80, 160
> > > 160
> > >
> > > Best, Rob
> > >
> > > > On 26 May 2020, at 4:09 pm, Anton Wallgren <[email protected]> wrote:
> > > >
> > > > Hello!
> > > >
> > > > Fairly recent J enthusiast here. I’m wondering about the idiomatic way 
> > > > to iterate at most n times? I.e. do f^:n y, but with the possibility of 
> > > > an early exit if some condition is met. Is it (u F. ]) y, where u is f 
> > > > but with some Z:’s added? E.g
> > > >
> > > > f=: 2&*
> > > > MAX=: n
> > > >
> > > > u=: monad define
> > > > _2 Z: -.*MAX=: MAX - 1
> > > > _2 Z: some other condition
> > > > f y
> > > > )
> > > >
> > > > But then you need to globally assign and reassign MAX and this doesn’t 
> > > > feel very elegant. Another option of course is to use a for-loop with 
> > > > break.
> > > >
> > > > Thanks, Anton Wallgren
> > > > ----------------------------------------------------------------------
> > > > 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

Reply via email to