exchange
abstract fun <Initial> exchange(initial: Initial, body: (Field<ID, Initial>) -> Field<ID, Initial>): Field<ID, Initial>
The exchange function manages the computation of values between neighbors in a specific context. It computes a body function starting from the initial value and the messages received from other neighbors, then sends the results from the evaluation to specific neighbors or to everyone, it is contingent upon the origin of the calculated value, whether it was received from a neighbor or if it constituted the initial value.
Example
exchange(0) { f ->
f.mapField { _, v -> if (v % 2 == 0) v + 1 else v * 2 }
}Content copied to clipboard
The result of the exchange function is a field with as messages a map with key the id of devices across the network and the result of the computation passed as relative local values.