diff --git a/enum.ml b/enum.ml index 080b276c..10c62714 100644 --- a/enum.ml +++ b/enum.ml @@ -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 = diff --git a/enum.mli b/enum.mli index b4200f9e..4b2229c3 100644 --- a/enum.mli +++ b/enum.mli @@ -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 *)