Subgraphs and functions
Some nodes take a function: a small graph of its own that they call, once per element or per step.
- Map calls it on each element of an array, Filter uses it to keep elements, and Reduce to combine them.
- Repeat calls it again and again, until it says to stop or reaches its limit.
A function is a subgraph: a graph with a Graph Input node for its
parameters and a Graph Output node for its results. It gets values only
through its parameters: it can't use values from the graph around it
directly. Pass them in through the higher-order node's arguments input
instead: each connected value becomes one more parameter, after the ones
the node always passes (such as the element and its index for Map).
Nodes and function nodes
Each subgraph gives two nodes, both named after it:
- its node ("Petal") runs the subgraph once: its inputs are the subgraph's params and its outputs its results, like any other node. Use it to reuse a piece of graph in several places;
- its function node ("Petal (function)") outputs the subgraph itself,
as a function value, to connect to a
functioninput.
A subgraph can use other subgraphs, and itself: see Recursion below.
Types are positional
A function's type lists the types of its parameters and results in order,
for example Function(Point, number) -> (Circle): names are only labels.
A function fits a function input when its parameters and results match
in number and type, position by position. Extra parameters at the end are
the ones arguments fill.
Calls are separate
Each call evaluates the body anew, with the values of that call. Random nodes inside it draw new numbers on every call; see Determinism and random.
Recursion
A subgraph may call itself, through its node or its function node inside its own body, or through other subgraphs that call it back. Each call works on its own values, so a shape can be split into smaller copies of itself, which are split again, and so on: the Sierpinski triangle example does this.
A recursion has to stop somewhere: the base case. That's up to the graph:
- a Switch chooses between the base case and
the recursive calls, for instance with a
depthparam that goes down by one on every call. Switch evaluates only the branch it picks, so the calls in the other branch aren't made; - or the calls come from Map or Filter over an array that gets shorter on every call: an empty array calls nothing.
If it never stops, limits keep the browser safe. A call from the main graph may nest at most 64 calls deep and make at most 10 000 calls in all, counting the calls of Map, Filter, Reduce and Repeat inside it. Beyond either, its node fails ("Recursion deeper than 64 calls: check the base case" or "More than 10 000 calls") and nothing is drawn from it; the rest of the drawing still is. Each node of the main graph has its own count.
In the Playground
The Subgraphs guide shows how to create subgraphs, edit their bodies and signatures, and make a fitting function for Map, Filter, Reduce or Repeat in one step with New function…. The triangles on a grid example puts it together.
See also
- Subgraphs, the guide.
- Composite types: function types.
- Generic pins, for
arguments. - Sierpinski triangle: a recursive subgraph.