[Jprogramming] Iterate at most n times
'Pascal Jasmin' via Programming
programming at jsoftware.com
Wed May 27 21:30:03 UTC 2020
Sorry if this has been suggested before, but the while variation of ^:_ supports a "while but at most n"
u^:(while true exp)^:(at most n)
if the test expression is quick, then u won't execute once false.
The request appears to have asked to track "reason for exit code", but reapplying the (while true exp) will tell you if it exited as a result of search finished or timeout hit.
On Tuesday, May 26, 2020, 10:07:47 a.m. EDT, Henry Rich <henryhrich at gmail.com> wrote:
I suggest/implore you reconsider using Fold. Fold is a new addition to
J, and is intended to pick up cases where other J idioms would be
inefficient. Like your case. If Fold doesn't work for you, I'd like to
consider what needs to be added to it.
This example suggests that it might be wise to have the loop iteration
number available, perhaps as the result of monad Z:. But I would like to
see what Fold would look like for your case.
Do note that the 9.02 docs (which is what NuVoc is at now) reflect the
fact that we changed the definition of Fold to apply v first, then y.
So your Fold implementation would be
test-for-exit F. do-one-iteration y
in 9.02.
Henry Rich
On 5/26/2020 8:58 AM, Anton Wallgren wrote:
> Cool! Seems preferable to the for loop (at least for one example) as well (modified a bit since I’m using a boxed array as y):
>
> repuntil=: 2 : '((<:&.>@{. , u@}.) ` ] @. v)^:(0 < >@{.)^:_'
> repuntil2=: 2 : 'for. i.x do. if. v y do. break. else. y=. u y end. end.'
> cond=: 6 >: >@{:
>
> depth=: 100000
>
> timespacex '(search repuntil cond)"1 (< depth) ,"1 m'
> 38.0857 182080
> timespacex 'depth (search repuntil2 cond)"1 m'
> 38.1578 1.2073e6
>
> Regards, Anton Wallgren
> On 26 May 2020, 13:45 +0200, Jan-Pieter Jacobs <janpieter.jacobs at gmail.com>, wrote:
>> Hi, I would do something like this:
>>
>> f=: +:
>> cond=: 100<{:
>> repuntil =: 2 : '((<:@{. , u@}.)`] @. v )^:(0<{.)^:_'
>> f repuntil cond 20 1
>> 0 32
>>
>> f repuntil cond 20 1
>> 13 128
>>
>> the first item tells you whether:
>>
>> - if 0: iterations ended due to hitting the limit (first item of argument);
>> or
>>
>> - if > 0, the condition became true.
>>
>> The agenda (@.) construct is just a way of making the left verb argument to
>> the left-most ^: return the same twice, which triggers the end of the
>> iteration by ^: _ .
>>
>> Good luck exploring J, it'll keep being interesting for long ;).
>>
>> Best regards,
>>
>> Jan-Pieter
>>
>> On Tue, May 26, 2020, 12:04 'Rob Hodgkinson' via Programming <
>> programming at jsoftware.com> wrote:
>>
>>> I don’t agree, it does not proceed further.
>>>
>>> ([x] u^:v^:_ y) will actually stop when v returns 0 (does not skip or
>>> continue executing)
>>>
>>> Here is a simple way to confirm this using a function f to do 2&* but
>>> which also uses ’smoutput’ to output to the session log (print intermediate
>>> results).
>>>
>>> f=: verb define
>>> smoutput r=:2*y
>>> r
>>> )
>>>
>>> f 5
>>> 10 NB. Intermediate print using smoutput
>>> 10. NB. Result of f
>>>
>>> Test it…
>>> a=:f 5
>>> 5. NB. It still prints within f via smoutput
>>>
>>> So now …
>>> f^:(10>])^:3 ] 1
>>> 2 }
>>> 4 } Intermediates from smoutpout (f called 3 times)
>>> 8 }
>>> 8 NB. Result returned (ran to completion)
>>> f^:(10>])^:4 ] 1
>>> 2 }
>>> 4 } Intermediates from smoutpoutn (f called 4 times)
>>> 8 }
>>> 16 }
>>> 16 NB. Result returned (ran to completion)
>>> f^:(10>])^:8 ] 1
>>> 2 }
>>> 4 } Intermediates from smoutput (f called 4 times)
>>> 8 }
>>> 16 }
>>> 16 NB. Result returned (aborted, so it stopped short
>>> and did not proceed further)
>>>
>>> HTH, Rob
>>>
>>>
>>>> On 26 May 2020, at 6:40 pm, Anton Wallgren <anton.w at hotmail.com> wrote:
>>>>
>>>> On closer inspection, f^:v^:n doesn’t seem to work (as I want it to).
>>> What it does is to skip evaluation each time v y returns 0. While this
>>> would work in my situation, as the stopping condition would remain true, it
>>> would of course be unnecessary (and inefficient) to keep checking this for
>>> the remainder of the iterations.
>>>> 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
--
This email has been checked for viruses by AVG.
https://www.avg.com
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
More information about the Programming
mailing list