Reputation: 7689
I have the problem of trying to 'process' (as in, 'run a function on') elements in a collection, like you would do with map
or foreach
. The problem is that the collection can change during processing - the processing function can add new elements that need to be processed. In imperative form, I'd keep a stack of these elements and push-to/pop-from the stack until it was empty. I am currently doing this with a mutable list but the format of the code that results is poor. Is there a standard immutable functional idiom for this case?
Upvotes: 2
Views: 93
Reputation: 708
It seems like OP has a function that can either give an answer or require additional work to be done. The easiest way I can think of to solve this is to have the function generating the answer recurse so it never returns something requiring additional work.
Upvotes: 2