Loops
A loop runs part of your graph once for each item in a list. EdgeWeave has two nodes for this, For Loop and End For Loop, found in the Control Flow category of the Library (or search for them with Ctrl+K).
How a loop is built
- Create a List node holding the items to loop over, for example
[0, 1, 2, 3, 4, 5]. - Wire the list into the
valuesport of a For Loop node. Itsvalueoutput is the current item and changes on every pass. - Build the loop body: the nodes you wire from the For Loop's
valueoutput. - Close the loop by wiring the body's last node into an End For Loop node.
- Whatever you wire out of End For Loop runs after the loop has finished, and receives the value from the final pass.
Everything between For Loop and End For Loop runs once per item. Nodes outside the loop run once.
Collecting results: the Append List pattern
To keep a result from every pass, use an Append List node in the loop body:
- Create a second, empty List node (
[]) as the starting list. - Wire the empty list and the For Loop's
valueinto an Append List node. - Wire Append List into End For Loop, and Print / Debug after it.
Append List keeps its running list between passes and starts fresh each time the
loop starts, so running the graph twice gives the same answer. The finished list,
[0, 1, 2, 3, 4, 5] in the example, is what comes out of End For Loop.
The demo control_flow/for_loop_demo.weave builds exactly this. Open it and press
Run.
What loops don't do (yet)
- There is no conditional (if / else) node. Control flow today is the loop pair.
- Only lists can be collected across passes, with Append List. A value computed in one pass can't be fed into the next pass as a single running number, such as a total. Collect the values in a list, then reduce the list after the loop.
- Keep the loop's list a real list in a List node, because that is how the loop knows how many times to run.
Python export
Loops export. The generated script contains an ordinary for loop around the
body, so the exported code does what the graph did. See
Export a workflow to Python.