Thanks for the suggestions, but I should have clarified two points earlier:
1. The actual calculation is path-dependent and non-trivial, so the solution should work for any arbitrary function of the current state and an element of the iterated vector.
2. I want to avoid updating globals because ultimately I want to be able to run this calculation many times in parallel with peach. And AFAIK, it's not possible to use thread local globals directly from q.