I want to reapply some function to some state until the condition is met.
The f function takes state, changes it, and returns. Apply f again to the returned state, etc.
I think it will work.
(first (filter pred (iterate fx)))
But it's a little ugly. Plus, memory consumption is not ideal, since the iterator will be forced to evaluate and save intermediate states until the state to which pred is true is returned, and at that moment the intermediate states should be garbage collected.
I know that you can write a simple recursive function:
(loop [fxp] (if (px) x (recur f (fx) p))
But I'm looking for a base library function (or some combination of functions) that does the same thing with the same memory efficiency.
yalis
source share