You can use Fold and the related Z: (Terminate Fold)
0 {{ y[1 Z: y > 10 }} F.. + 1 5 6 16
12
0 {{ y[1 Z: y > 10 }} F:. + 1 5 6 16
1 6 12
analogous to +/ and +/\ with the argument shortened to where the sum exceeds 10:
+/1 5 6
12
+/\1 5 6
1 6 12
as a verb:
f0 =: {{ 0 x&{{ y[1 Z: y > x }} F.. + y }}
10 f0 1 5 6 16
12
5 f0 1 5 6 16
6
100 f0 1 5 6 16
28
------- Original Message -------
On Monday, February 28th, 2022 at 12:40 PM, Richard Donovan
<[email protected]> wrote:
> I hope someone can enlighten me…
>
> If I have a list of numbers, how can I write a verb which will can perform a
> cumulative sum +/\ which will stop when a target sum is reached?
>
> For example, is the list contains 1,5,6,16… I wish to find at what position
> the cumulative sum exceeds 10. I feel that when I am dealing with monster
> lists, it would be beneficial if the function could stop as soon as the
> target had been reached. (If the target was not reached by the end of the
> list, it would be good if the index of 1 greater than the last item could be
> returned, as occurs with other j verbs).
>
> Thanks in advance,
>
> Richard Donovan
>
> ----------------------------------------------------------------------
>
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm