2 people like it.
Like the snippet!
Draw from sequences
Peeks from a few seq randomly according to specified frequencies. not tested
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
|
let selectPeek (seqs: seq<_> seq) (probs: float seq)=
let r = System.Random()
let enums = seqs |> Seq.map (fun x-> x.GetEnumerator()) |> Seq.toArray
let probs = probs |> Seq.toArray
assert(enums.Length = probs.Length)
//curlist always contains non empty sequences
let curlist = enums |> Array.mapi (fun i x-> i,x)
|> Array.fold(fun s (i,x) -> if x.MoveNext() <> false then i::s else s ) List.empty
let rec select curlist zleft = seq { //get next element and return list of future ids to peek from
let rec next (curlist:int list) zleft = //walks prob tree and return next element
match curlist with
| [] -> None
| i::[] -> Some(i, enums.[i].Current)
| i::xs -> if r.NextDouble() < probs.[i]/zleft then
Some(i, enums.[i].Current)
else
next xs (zleft-probs.[i])
match next curlist zleft with
| Some (i,e) -> yield e
let curlist', zleft' = if enums.[i].MoveNext() <> false then curlist, zleft
else let curlist' = curlist |> List.filter(fun e -> e <> i)
curlist', curlist' |> List.fold(fun s e -> s + probs.[e]) 0.
yield! select curlist' zleft'
| _ -> ()
}
select curlist (curlist |> List.fold(fun s e -> s + probs.[e]) 0.)
let rec ones() = seq{ yield 1; yield! ones() }
let rec twos() = seq{ yield 2;yield! twos() }
let onetwos = selectPeek [ones(); twos()] [|2.;1.|] |> Seq.truncate 100 |> Seq.toArray
let none,ntwo = onetwos |> Seq.filter(fun x -> x = 1) |> Seq.length , onetwos |> Seq.filter(fun x -> x = 2) |> Seq.length
|
val selectPeek : seqs:seq<seq<'a>> -> probs:seq<float> -> seq<'a>
Full name: Script.selectPeek
val seqs : seq<seq<'a>>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Core.Operators.seq
--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>
Full name: Microsoft.FSharp.Collections.seq<_>
val probs : seq<float>
Multiple items
val float : value:'T -> float (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.float
--------------------
type float = System.Double
Full name: Microsoft.FSharp.Core.float
--------------------
type float<'Measure> = float
Full name: Microsoft.FSharp.Core.float<_>
val r : System.Random
namespace System
Multiple items
type Random =
new : unit -> Random + 1 overload
member Next : unit -> int + 2 overloads
member NextBytes : buffer:byte[] -> unit
member NextDouble : unit -> float
Full name: System.Random
--------------------
System.Random() : unit
System.Random(Seed: int) : unit
val enums : System.Collections.Generic.IEnumerator<'a> []
module Seq
from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>
Full name: Microsoft.FSharp.Collections.Seq.map
val x : seq<'a>
System.Collections.Generic.IEnumerable.GetEnumerator() : System.Collections.Generic.IEnumerator<'a>
val toArray : source:seq<'T> -> 'T []
Full name: Microsoft.FSharp.Collections.Seq.toArray
val probs : float []
property System.Array.Length: int
val curlist : int list
module Array
from Microsoft.FSharp.Collections
val mapi : mapping:(int -> 'T -> 'U) -> array:'T [] -> 'U []
Full name: Microsoft.FSharp.Collections.Array.mapi
val i : int
val x : System.Collections.Generic.IEnumerator<'a>
val fold : folder:('State -> 'T -> 'State) -> state:'State -> array:'T [] -> 'State
Full name: Microsoft.FSharp.Collections.Array.fold
val s : int list
System.Collections.IEnumerator.MoveNext() : bool
Multiple items
module List
from Microsoft.FSharp.Collections
--------------------
type List<'T> =
| ( [] )
| ( :: ) of Head: 'T * Tail: 'T list
interface IEnumerable
interface IEnumerable<'T>
member Head : 'T
member IsEmpty : bool
member Item : index:int -> 'T with get
member Length : int
member Tail : 'T list
static member Cons : head:'T * tail:'T list -> 'T list
static member Empty : 'T list
Full name: Microsoft.FSharp.Collections.List<_>
val empty<'T> : 'T list
Full name: Microsoft.FSharp.Collections.List.empty
val select : (int list -> float -> seq<'a>)
val zleft : float
val next : (int list -> float -> (int * 'a) option)
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
type 'T list = List<'T>
Full name: Microsoft.FSharp.Collections.list<_>
union case Option.None: Option<'T>
union case Option.Some: Value: 'T -> Option<'T>
val xs : int list
System.Random.NextDouble() : float
val e : 'a
val curlist' : int list
val zleft' : float
val filter : predicate:('T -> bool) -> list:'T list -> 'T list
Full name: Microsoft.FSharp.Collections.List.filter
val e : int
val fold : folder:('State -> 'T -> 'State) -> state:'State -> list:'T list -> 'State
Full name: Microsoft.FSharp.Collections.List.fold
val s : float
val ones : unit -> seq<int>
Full name: Script.ones
val twos : unit -> seq<int>
Full name: Script.twos
val onetwos : int []
Full name: Script.onetwos
val truncate : count:int -> source:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Collections.Seq.truncate
val none : int
Full name: Script.none
val ntwo : int
Full name: Script.ntwo
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Collections.Seq.filter
val x : int
val length : source:seq<'T> -> int
Full name: Microsoft.FSharp.Collections.Seq.length
More information