more combinators (iterate and forever);

some sequences of random elements
This commit is contained in:
Simon Cruanes 2013-02-15 17:35:00 +01:00
parent 45e331861d
commit 55ff51f2cb
2 changed files with 49 additions and 0 deletions

View file

@ -36,6 +36,16 @@ let singleton x = fun k -> k x
(** Infinite sequence of the same element *) (** Infinite sequence of the same element *)
let repeat x = fun k -> while true do k x done let repeat x = fun k -> while true do k x done
(** [iterate f x] is the infinite sequence (x, f(x), f(f(x)), ...) *)
let iterate f x =
let rec iterate k x = k x; iterate k (f x) in
from_iter (fun k -> iterate k x)
(** Sequence that calls the given function to produce elements *)
let forever f =
let rec forever k = k (f ()); forever k in
from_iter forever
(** Cycle forever through the given sequence. O(n). *) (** Cycle forever through the given sequence. O(n). *)
let cycle s = fun k -> while true do s k; done let cycle s = fun k -> while true do s k; done
@ -320,6 +330,26 @@ module Map = struct
end end
end end
(** {2 Infinite sequences of random values} *)
let random_int bound = forever (fun () -> Random.int bound)
let random_bool = forever Random.bool
let random_float bound = forever (fun () -> Random.float bound)
(** Sequence of choices of an element in the array *)
let random_array a =
assert (Array.length a > 0);
let seq k =
while true do
let i = Random.int (Array.length a) in
k a.(i);
done in
from_iter seq
let random_list l = random_array (Array.of_list l)
(** {2 Pretty printing of sequences} *) (** {2 Pretty printing of sequences} *)
(** Pretty print a sequence of ['a], using the given pretty printer (** Pretty print a sequence of ['a], using the given pretty printer

View file

@ -42,6 +42,12 @@ val singleton : 'a -> 'a t
val repeat : 'a -> 'a t val repeat : 'a -> 'a t
(** Infinite sequence of the same element *) (** Infinite sequence of the same element *)
val iterate : ('a -> 'a) -> 'a -> 'a t
(** [iterate f x] is the infinite sequence (x, f(x), f(f(x)), ...) *)
val forever : (unit -> 'b) -> 'b t
(** Sequence that calls the given function to produce elements *)
val cycle : 'a t -> 'a t val cycle : 'a t -> 'a t
(** Cycle forever through the given sequence. Assume the (** Cycle forever through the given sequence. Assume the
given sequence can be traversed any amount of times (not transient). *) given sequence can be traversed any amount of times (not transient). *)
@ -204,6 +210,19 @@ module Map : sig
module Make(V : Map.OrderedType) : S with type key = V.t module Make(V : Map.OrderedType) : S with type key = V.t
end end
(** {2 Infinite sequences of random values} *)
val random_int : int -> int t
val random_bool : bool t
val random_float : float -> float t
val random_array : 'a array -> 'a t
(** Sequence of choices of an element in the array *)
val random_list : 'a list -> 'a t
(** {2 Pretty printing of sequences} *) (** {2 Pretty printing of sequences} *)
val pp_seq : ?sep:string -> (Format.formatter -> 'a -> unit) -> val pp_seq : ?sep:string -> (Format.formatter -> 'a -> unit) ->