basic functions in Enum

This commit is contained in:
Simon Cruanes 2013-03-18 23:23:13 +01:00
parent 3da9444cd8
commit 5d289098b0
2 changed files with 23 additions and 0 deletions

14
enum.ml
View file

@ -34,12 +34,26 @@ and 'a generator = unit -> 'a
(** A generator may be called several times, yielding the next value
each time. It raises EOG when it reaches the end. *)
let empty () = fun () -> raise EOG
let singleton x =
fun () ->
let stop = ref false in
fun () ->
if !stop
then raise EOG
else begin stop := true; x end
let start enum = enum ()
let next gen = gen ()
let junk gen = ignore (gen ())
let is_empty enum =
try ignore ((enum ()) ()); false
with EOG -> true
let fold f acc enum =
let rec fold acc gen =
let acc', stop =

View file

@ -37,6 +37,12 @@ and 'a generator = unit -> 'a
(** A generator may be called several times, yielding the next value
each time. It raises EOG when it reaches the end. *)
val empty : 'a t
(** Enmpty enum *)
val singleton : 'a -> 'a t
(** One-element enum *)
val start : 'a t -> 'a generator
(** Create a new generator *)
@ -46,6 +52,9 @@ val next : 'a generator -> 'a
val junk : 'a generator -> unit
(** Drop element *)
val is_empty : _ t -> bool
(** Check whether the enum is empty *)
val fold : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b
(** Fold on the generator *)