2 · Neighbours and fields
With nbr(v) each device sends v to its neighbours and gets theirs back.
One value per neighbour
nbr(v) returns a neighbouring field: one value per neighbour, plus the device's own in .self. It is not a number, so you reduce it to a local value:
.sum(),.min(),.max(): the usual reductions;.reduce((acc, v) => ..., initial): likeArray.reduce, and it takesMath.minorMath.maxdirectly.
Reductions skip the device itself: only neighbours count. Here is each node's number of neighbours; drag nodes around or change range.
Hover a node: it sends {"x0":[1]}. x0 is the name of that nbr in the program, and devices only compare values with the same name. Chapter 5 is about these names.
Transforming and combining fields
.map((v, id) => ...)transforms each value;.zip(other, (a, b) => ...)combines two fields value by value;nbrSense('range')is a field the platform provides: the distance to each neighbour.
Try it
1. The largest id among the neighbours (simulator ids are strings: use +id()).
2. "Am I a local leader?": true if my id is larger than every neighbour's.
3. The average x position of the neighbours (sensor sense('x')).
Solutions
var program = () => nbr(+id()).max();
var program = () => nbr(+id()).max() < +id();
var program = () => nbr(sense('x')).sum() / nbr(1).sum();
The local leader works for isolated nodes too: .max() of an empty field is -Infinity.