Hey Anton, Welcome to the J-verse!
FWIW, the question as stated makes me suspect that you're (unconsciously) trying to fit J into an imperative programming paradigm. I would venture that you'd get better answers by more directly sharing what you're trying to accomplish and the problems thus encountered. One simple formula is to simply share the input you have and output you want. From lurking here on the forums for a bit, those kinds of questions often get a lot of good attention. It's often the case that one of the old guard peeps in with a short and sweet solution that is both beautiful and humbling. Cheers, Anton Wallgren <[email protected]> wrote: > Hi, thanks! But as Raul pointed out, this does not allow me to specify max > iterations. > > Regards, Anton Wallgren > On 26 May 2020, 08:39 +0200, '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
