// assigns probability pStart for first element, pEnd for last element
// (rest linearly interpolated)
//
// f may return an instance of IProbabilistic (or a normal Runnable)
static void probabilisticForEach(IProbabilisticScheduler scheduler, double pStart, double pEnd, Cl l, IVF1 f) {
if (scheduler == null || f == null) ret;
probabilisticForEach(scheduler, pStart, pEnd, iterator(l), 0, l(l), 1.0, f);
}
static void probabilisticForEach(IProbabilisticScheduler scheduler, double pStart, double pEnd, Iterator it, int i, int n, double myProb, IVF1 f) {
assertTrue(pEnd <= pStart);
if (scheduler == null || f == null || !it.hasNext()) ret;
// get element
A a = it.next();
// calculate probability for element
double p = blend(pStart, pEnd, doubleRatio(i, n-1));
// run client function
scheduler.at(new Runnable {
run { f.get(a); }
toString { ret formatFunctionCall(f, a); }
});
// schedule next element at lower probability
scheduler.atRelative(doubleRatio(p, myProb), ->
probabilisticForEach(scheduler, pStart, pEnd, it, i+1, n, p, f));
}