6 · xc: one message per neighbour
nbr and share send the same value to everyone. The eXchange Calculus has a single primitive, xc, which can send each neighbour a different value.
How it works
xc(initial, (n) => ...):
nis the field of what each neighbour sent to me;n.selfis what I sent myself last round (at the start,initial);- if the function returns a field, each neighbour gets its own value; a new neighbour, not in the field yet, gets the default;
- if it returns a plain value, everyone gets it;
retsend(result, toSend)separates the returned value from the one sent.
The operators so far are special cases: share is xc returning a plain value, and nbr(v) is almost xc(v, (n) => retsend(n, v)); the only difference is that in nbr, .self is already this round's v.
Link age
I send each neighbour back what it sent me, plus one. Every pair of nodes keeps a private counter that grows as long as the link lasts. Each node shows the age of its newest link: drag a node elsewhere and its new links restart from 1.
Hover a node: the message has the form [default, {id: value}], one value per neighbour. Over Bluetooth, each extra neighbour costs about 3 bytes.
Collect: going up the gradient
The opposite of broadcast: gathering information towards the source. Each node picks as parent the neighbour closest to the source and sends its partial sum to it only; the others get 0. The source ends up with the number of nodes that reach it.
With nbr or share you would send everyone "my parent is X" and let each one filter. With xc the message is already addressed. The standard library's collectSum is exactly this function.
Try it
1. Each node counts its children: how many neighbours chose it as parent.
2. Write your own myShare(init, f) with xc only, and use it in the gradient.
Solutions
var program = () => {
var dist = distanceTo(sense('source'));
var parent = minId(nbr(dist), dist);
return xc(0, (n) => retsend(n.sum(), n.map((x, id) => id === parent ? 1 : 0)));
};
var myShare = (init, f) => xc(init, (n) => f(n));
The second is identical to share: when the function returns a plain value, xc uses it both as result and as message.