Day #12 with Cloud Workflows: loops and iterations
In previous episodes of this Cloud Workflows series, we’ve learned about variable assignment, data structures like arrays, jumps and switch conditions to move between steps, and expressions to do some computations, including potentially some built-in functions.
With all these previous learnings, we are now equipped with all the tools to let us create loops and iterations, like for example, iterating over the element of an array, perhaps to call an API several times but with different arguments. So let’s see how to create such an iteration!
First of all, let’s prepare some variable assignments:
The array variable will hold the values we’ll be iterating over.
The result variable contains a string to which we’ll append each values from the array.
And the i variable is an index, to know our position in the array.
Next, like in a for loop of programming languages, we need to prepare a condition for the loop to finish. We’ll do that in a dedicated step:
We define a switch, with a condition expression that compares the current index position with the length of the array, using the built-in len() function. If the condition is true, we’ll go to an iterate step. If it’s false, we’ll go to the ending step (called returnResult here).
Let’s tackle the iteration body itself. Here, it’s quite simple, as we’re just assigning new values to the variables: we append the i-th element of the array into the result variable, and we increment the index by one. Then we go back to the checkCondition step.
Note that if we were doing something more convoluted, for example calling an HTTP endpoint with the element of the array as argument, we would need two steps: one for the actual HTTP endpoint call, and one for incrementing the index value. However in the example above, we’re only assigning variables, so we did the whole body of the iteration in this simple assignment step.
When going through the checkCondition step, if the condition is not met (ie. we’ve reached the end of the array), then we’re redirected to the returnResult step:
This final step simply returns the value of the result variable.