Skip to main content

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​

  1. Create a List node holding the items to loop over, for example [0, 1, 2, 3, 4, 5].
  2. Wire the list into the values port of a For Loop node. Its value output is the current item and changes on every pass.
  3. Build the loop body: the nodes you wire from the For Loop's value output.
  4. Close the loop by wiring the body's last node into an End For Loop node.
  5. 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:

  1. Create a second, empty List node ([]) as the starting list.
  2. Wire the empty list and the For Loop's value into an Append List node.
  3. 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.