How to do recursive loops in EasyMorph

Sometimes it may be necessary to recursively repeat certain calculation multiple times until a condition is fulfilled and every iteration should process a result of previous iteration.

image

As of v4.3 EasyMorph doesn’t have a special action for conditional loops, i.e. loops that should stop on condition. The workaround is to do unconditional loops long enough that they will always have more cycles than required for the condition to be fulfilled. E.g. if you know that a loop typically takes 3-5 cycles and never ever more than 10 then make an iteration for 10 cycles. Inside the loop have a condition that won’t do any changes if the condition is already met, to make “empty” cycles that don’t do anything after the condition is met.

Data between iterations should be transferred through an external file (typically EasyMorph dataset, .dset). The file should be created before starting the loop, modified inside the loop, then saved into a file if the condition is still not fulfilled, or not saved if it does. This is not very efficient as it will make the loop read the same dataset again and again in empty cycles, but that’s the way it is now.

Below is a sample project that calculates the square of each value in a column and repeats it recursively until the total sum of all values doesn’t exceed a threshold defined by a parameter.

recursive-loop.morph (6.9 KB)

Module “Main”

Module 1

UPDATE

We’ve added a dedicated action for recursive / recurrent loops. See the “Repeat” action in our tutorial (Advanced topics): https://easymorph.com/learn/iterations.html