I'm playing with John Earnest's K implementation in the context of Project Euler problems.
Many of the problems involve taking the first n terms, or all terms <= n, from an infinite series (particularly primes). It could also involve taking items from a pre-existing list one at a time until a condition is satisfied.
In Python, one approach is to rely on the iterator protocol: you can take from an iterator until it's finished, or break out early when some condition is satisfied (say you've taken n items, or the last item you took satisfies a certain condition).
What are typical patterns in K (or other APLs) for achieving something similar - that is, taking from a list or generator until a condition is satisfied, without evaluating or processing the whole list? Do I have to rely on the techniques below, perhaps using some kind of internal state within f
? Is this kind of approach discouraged and if so, why?
f/x / fixed point
n f/x / apply f n times
p f/x / do or while loop, with p a predicate function (stops when 0)
EDIT 2018-10-14: Some interesting notes on lazy iteration in APLs here.