[Jprogramming] Iterate at most n times

Anton Wallgren anton.w at hotmail.com
Tue May 26 11:07:39 UTC 2020


Hi, thank you, missed this mail! You are of course right, more details is better :)

I think that posting complete code for this question might obscure the idea, as its more about a pattern I want to use occasionally. An example would be for a Tabu search algorithm I might know a lower bound for the objective function, so if I during search find a solution whose objective value equals the lower bound, I do not want to continue as I know that I can’t find a better solution. If the lower bound is not tight however, or I do not manage to find such a solution, I wish to abort the search after some set number of iterations, as the search could go on forever.

Regards, Anton Wallgren
On 26 May 2020, 10:39 +0200, ethiejiesa via Programming <programming at jsoftware.com>, wrote:
> 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 <anton.w at hotmail.com> 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 <programming at jsoftware.com>, 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 <anton.w at hotmail.com> 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


More information about the Programming mailing list