abstract Weighted<A>(Iterable<Pair<A, Float>>)
package interlude.ds
from Iterable<Pair<A, Float>>, to Iterable<Pair<A, Float>>
Represents a weighted probability distribution. Supports a tree-structure for
weights with Weighted<Weighted<A>>
enum Coin { H; T; }
// Heads and Tails equally likely
var fairCoin = [H, T].uniform();
var fairFlips = [
for(i in 0...10)
fairCoin.draw(Math.random)
]; // ex: [T,H,H,T,T,H,T,T,H,T]
// Tails much more likely than Heads
var unfairCoin = [H.with(0.25), T.with(0.75)];
var unfairFlips = [
for(i in 0...10)
unfairCoin.draw(Math.random)
]; // ex: [T,T,T,T,H,T,H,T,T,H]Static methods
staticdraw<A>(dist:Weighted<A>, randomGen:() ‑> Float):A
Draws a single result from a weighted set. randomGen should be a
function that generates a random value between 0 and 1, e.g. Math.random
Clamps the output of randomGen between 0.0 and 1.0
staticdrawIndex<A>(dist:Weighted<A>, randomGen:() ‑> Float):Int
Draws a result from a weighted set and returns its index. randomGen
should be a function that generates a random value between 0 and 1,
e.g. Math.random
Clamps the output of randomGen between 0.0 and 1.0
staticflatMap<A, B>(dist:Weighted<A>, fn:A ‑> Weighted<B>):Weighted<B>
Projects each element of a weighted distribution of A to a distribution
of type B, and flattens the results
Monadic bind/flatMap
staticflatten<A>(dists:Weighted<Weighted<A>>):Weighted<A>
Flattens one layer of a probability tree by multiplying weights together
staticmap<A, B>(dist:Weighted<A>, fn:A ‑> B):Weighted<B>
Maps the A elements of a weighted distribution to a type B using fn
[123.with(1)].map(x -> '$x') == ['123'.with(1)];