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).

1  
// assigns probability pStart for first element, pEnd for last element
2  
//   (rest linearly interpolated)
3  
//
4  
// f may return an instance of IProbabilistic (or a normal Runnable)
5  
6  
static <A> void probabilisticForEach(IProbabilisticScheduler scheduler, double pStart, double pEnd, Cl<A> l, IVF1<A> f) {
7  
  if (scheduler == null || f == null) ret;
8  
  probabilisticForEach(scheduler, pStart, pEnd, iterator(l), 0, l(l), 1.0, f);
9  
}
10  
  
11  
static <A> void probabilisticForEach(IProbabilisticScheduler scheduler, double pStart, double pEnd, Iterator<A> it, int i, int n, double myProb, IVF1<A> f) {
12  
  assertTrue(pEnd <= pStart);
13  
  if (scheduler == null || f == null || !it.hasNext()) ret;
14  
  
15  
  // get element
16  
  A a = it.next();
17  
  
18  
  // calculate probability for element
19  
  double p = blend(pStart, pEnd, doubleRatio(i, n-1));
20  
  
21  
  // run client function
22  
  scheduler.at(new Runnable {
23  
    run { f.get(a); }
24  
    toString { ret formatFunctionCall(f, a); }
25  
  });
26  
  
27  
  // schedule next element at lower probability
28  
  scheduler.atRelative(doubleRatio(p, myProb), ->
29  
    probabilisticForEach(scheduler, pStart, pEnd, it, i+1, n, p, f));
30  
}

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: 101 / 166
Version history: 5 change(s)
Referenced in: [show references]