RosettaCodeData/Task/Active-object/Zkl/active-object-1.zkl

20 lines
606 B
Plaintext

class Integrator{
// continuously integrate a function `K`, at each `interval` seconds'
fcn init(f,interval=1e-4){
var _interval=interval, K=Ref(f), S=Ref(0.0), run=True;
self.launch(); // start me as a thread
}
fcn liftoff{ // entry point for the thread
start:=Time.Clock.timef; // floating point seconds since Epoch
t0,k0,s:=0,K.value(0),S.value;
while(run){
Atomic.sleep(_interval);
t1,k1:=Time.Clock.timef - start, K.value(t1);
s+=(k1 + k0)*(t1 - t0)/2.0; S.set(s);
t0,k0=t1,k1;
}
}
fcn sample { S.value }
fcn setF(f) { K.set(f) }
}