mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-09 12:45:34 -05:00
some small improvements to CCRandom
This commit is contained in:
parent
c14a0a4bbb
commit
c33576a7e1
2 changed files with 25 additions and 0 deletions
|
|
@ -41,6 +41,8 @@ let map f g st = f (g st)
|
||||||
|
|
||||||
let (>|=) g f st = map f g st
|
let (>|=) g f st = map f g st
|
||||||
|
|
||||||
|
let delay f st = f () st
|
||||||
|
|
||||||
let _choose_array a st =
|
let _choose_array a st =
|
||||||
if Array.length a = 0 then invalid_arg "CCRandom.choose_array";
|
if Array.length a = 0 then invalid_arg "CCRandom.choose_array";
|
||||||
a.(Random.State.int st (Array.length a))
|
a.(Random.State.int st (Array.length a))
|
||||||
|
|
@ -69,6 +71,8 @@ let replicate n g st =
|
||||||
if n = 0 then acc else aux (g st :: acc) (n-1)
|
if n = 0 then acc else aux (g st :: acc) (n-1)
|
||||||
in aux [] n
|
in aux [] n
|
||||||
|
|
||||||
|
let list_seq l st = List.map (fun f -> f st) l
|
||||||
|
|
||||||
exception SplitFail
|
exception SplitFail
|
||||||
|
|
||||||
let _split i st =
|
let _split i st =
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,21 @@ val map : ('a -> 'b) -> 'a t -> 'b t
|
||||||
|
|
||||||
val (>|=) : 'a t -> ('a -> 'b) -> 'b t
|
val (>|=) : 'a t -> ('a -> 'b) -> 'b t
|
||||||
|
|
||||||
|
val delay : (unit -> 'a t) -> 'a t
|
||||||
|
(** Delay evaluation. Useful for side-effectful generators that
|
||||||
|
need some code to run for every call.
|
||||||
|
Example:
|
||||||
|
{[
|
||||||
|
let gensym = let r = ref 0 in fun () -> incr r; !r ;;
|
||||||
|
|
||||||
|
delay (fun () ->
|
||||||
|
let name = gensym() in
|
||||||
|
small_int >>= fun i -> return (name,i)
|
||||||
|
)
|
||||||
|
]}
|
||||||
|
@since NEXT_RELEASE
|
||||||
|
*)
|
||||||
|
|
||||||
val choose : 'a t list -> 'a option t
|
val choose : 'a t list -> 'a option t
|
||||||
(** Choose a generator within the list. *)
|
(** Choose a generator within the list. *)
|
||||||
|
|
||||||
|
|
@ -59,6 +74,12 @@ val choose_return : 'a list -> 'a t
|
||||||
@raise Invalid_argument if the list is empty *)
|
@raise Invalid_argument if the list is empty *)
|
||||||
|
|
||||||
val replicate : int -> 'a t -> 'a list t
|
val replicate : int -> 'a t -> 'a list t
|
||||||
|
(** [replace n g] makes a list of [n] elements which are all generated
|
||||||
|
randomly using [g] *)
|
||||||
|
|
||||||
|
val list_seq : 'a t list -> 'a list t
|
||||||
|
(** Build random lists from lists of random generators
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
val small_int : int t
|
val small_int : int t
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue