Recursion is a concept commonly used in programming to divide complex tasks into two parts:
- a solution for a simple problem
- a way to use solutions of simpler problems to solve more complex ones
A real life example on filling out forms for a group of people could be:
- A person receives a stack of forms.
- The person takes a form and fills it out. (basic step)
- The person gives the remaining forms to the next person. (recursive step)
Recursion in ProtoFlux
The recursive step usually needs the ability to call a piece of code within a new Context. In ProtoFlux this can be achieved in multiple ways:
- Using Start Async Task to clone the current context
- Calling a Dynamic Impulse Receiver With Data that has already been called within the current context to create a new context.
Both approaches have the problem that the new context cannot directly interact with the original one. It is planned to solve this when nested nodes are implemented. (github issue 564)
An alternative is managing the context explicitly as part of the Data Model. Duplicating a Slot with Dynamic Variables to represent a context makes this similar to implementing recursion with iteration and a stack.
Slot Hierarchy Traversal
Visiting all slots in a hierarchy is a common situation where recursion can be used. Visiting a single slot makes up the "basic step" while going through all direct children is part of the "recursive step".
The following example does not require managing contexts because it can derive the original context when leaving a recursive step. This is done by using Get Parent Slot.

In a similar fashion the "Depth" value is computed by tracking how many recursive steps have been entered and left.
A boolean value can be used to stop the operation before it finished.
The example should also work with normal For loops in non-async ProtoFlux.