sclass PStack is Steppable {
new ProbabilisticScheduler scheduler;
class SingleStack extends VStack is Runnable {
double probability = 1.0;
*(PStackComputable computable) {
super(computable);
}
run {
if (step())
// re-add myself to scheduler to continue
scheduler.add(probability, this);
}
}
srecord neoq FollowUp(PStackComputable computable, IVF1 onCompletion) extends PStackComputableWithStep {
void step(PStack stack) {
if (step == 0) {
stack.push(computable);
++step;
} else {
onCompletion.get(stack.subResult());
stack.return();
}
}
}
void add(PStackComputable computable) {
scheduler.at(1.0, new SingleStack(computable));
}
void add(PStackComputable computable, IVF1 onCompletion) {
if (onCompletion != null)
add(new FollowUp<>(computable, onCompletion));
else
add(computable);
}
public bool step() { ret scheduler.step(); }
}