^:_ 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

Reply via email to