[Jprogramming] understanding scan

Pascal Jasmin godspiral2000 at yahoo.ca
Tue Oct 17 05:26:06 HKT 2006


   a =: 44 _78 _3 32 7 2 _9 33 9 183 _350 _34 44 75 17 22 19 _190 120 19 7 3 29 8 65 42 2 88 _119 1


gambler=: 3 : 0
out =. i.0
max=. 0
for_i. i. # y do.
out=. out , ] max =. (0 >. max+ (i_index{y))
end.
)

   gambler a
44 0 0 32 39 41 32 65 74 257 0 0 44 119 136 158 177 0 120 139 146 149 178 186 251 293 295 383 264 265

The above function gives the running total so far if that total is above 0, otherwise it returns 0, and restarts the running total.

I somehow guessed that a solution of the form f /\ a would exist, but couldn't think of f.

alternatively there is probably a solution in the form (+ >. 0:) customadverb a

If there are general guidelines for comming up with J-like versions of functions that peek ahead at the next index to 'correct' a summarizing calculation, that would help me.





More information about the Programming mailing list