1 · Rounds and state
Every device runs the same program, over and over. Each run is a round.
A device's loop
In every round a device does the same three things:
- reads its sensors and the latest messages from its neighbours;
- runs the program, which produces a value (the device's output) and a message;
- broadcasts the message to whoever is listening.
The simulator runs rounds in lockstep for simplicity. Real devices each keep their own pace, and aggregate programs work all the same.
The program is step 2: a function with no arguments, called program. This one shows each device's identifier:
Remembering: rep
The program starts from scratch every round: local variables do not survive. For state use rep(initial, f): in the first round f gets the initial value, then always the result of the previous round.
Reset restarts the network from scratch. Hover a node: its message is {}, because rep is local memory and sends nothing to the neighbours.
Math, arrays and objects are fine; destructuring, spread and default parameters are not.Try it
1. A blinker: every node alternates true and false each round.
2. A countdown from 10 that stops at 0.
3. Each node starts from its own id (as a number: +id()) and counts up from there.
Solutions
var program = () => rep(false, (b) => !b);
var program = () => rep(10, (c) => Math.max(c - 1, 0));
var program = () => rep(+id(), (c) => c + 1);