Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

30
LINES

< > BotCompany Repo | #1033144 // probabilisticForEach

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5340L/30K).

// 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 <A> void probabilisticForEach(IProbabilisticScheduler scheduler, double pStart, double pEnd, Cl<A> l, IVF1<A> f) {
  if (scheduler == null || f == null) ret;
  probabilisticForEach(scheduler, pStart, pEnd, iterator(l), 0, l(l), 1.0, f);
}
  
static <A> void probabilisticForEach(IProbabilisticScheduler scheduler, double pStart, double pEnd, Iterator<A> it, int i, int n, double myProb, IVF1<A> 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));
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033144
Snippet name: probabilisticForEach
Eternal ID of this version: #1033144/6
Text MD5: 646c76a6cde3375b2ec9cc6f9538deb3
Transpilation MD5: a755f3728690ce09ed63633528e4245a
Author: stefan
Category: javax / probabilistic scheduling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-25 03:20:38
Source code size: 1147 bytes / 30 lines
Pitched / IR pitched: No / No
Views / Downloads: 170 / 249
Version history: 5 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)