// assuming that math_sqrt can be used
function squarer(a, b) {
function process_new_value() {
if (has_value(b)) {
if (get_value(b) < 0) {
error(get_value(b), "square less than 0 -- squarer");
} else {
set_value(a, math_sqrt(get_value(b)), me);
}
} else{
if(has_value(a)) {
set_value(b, get_value(a) * get_value(a), me);
}
}
}
function process_forget_value() {
forget_value(a, me);
forget_value(b, me);
process_new_value();
}
function me(request) {
if (request === "I have a value.") {
process_new_value();
} else if (request === "I lost my value.") {
process_forget_value();
} else {
error(request, "unknown request -- squarer");
}
}
connect(a, me);
connect(b, me);
return me;
}
Test cases:
const a = make_connector();
const b = make_connector();
squarer(a,b);
probe("a",a);
probe("b", b);
set_value(a, 3 , "user");
// set_value(b, 16, "user");
Output:
"Probe: a = 3"
"Probe: b = 9"
"done"
Output when the first set_value is commented and the second one is not:
"Probe: b = 16"
"Probe: a = 4"
"done"
Test cases:
Output:
Output when the first set_value is commented and the second one is not: