mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2026-01-28 20:04:51 -05:00
basic functions in Enum
This commit is contained in:
parent
3da9444cd8
commit
5d289098b0
2 changed files with 23 additions and 0 deletions
14
enum.ml
14
enum.ml
|
|
@ -34,12 +34,26 @@ and 'a generator = unit -> 'a
|
||||||
(** A generator may be called several times, yielding the next value
|
(** A generator may be called several times, yielding the next value
|
||||||
each time. It raises EOG when it reaches the end. *)
|
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 start enum = enum ()
|
||||||
|
|
||||||
let next gen = gen ()
|
let next gen = gen ()
|
||||||
|
|
||||||
let junk gen = ignore (gen ())
|
let junk gen = ignore (gen ())
|
||||||
|
|
||||||
|
let is_empty enum =
|
||||||
|
try ignore ((enum ()) ()); false
|
||||||
|
with EOG -> true
|
||||||
|
|
||||||
let fold f acc enum =
|
let fold f acc enum =
|
||||||
let rec fold acc gen =
|
let rec fold acc gen =
|
||||||
let acc', stop =
|
let acc', stop =
|
||||||
|
|
|
||||||
9
enum.mli
9
enum.mli
|
|
@ -37,6 +37,12 @@ and 'a generator = unit -> 'a
|
||||||
(** A generator may be called several times, yielding the next value
|
(** A generator may be called several times, yielding the next value
|
||||||
each time. It raises EOG when it reaches the end. *)
|
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
|
val start : 'a t -> 'a generator
|
||||||
(** Create a new generator *)
|
(** Create a new generator *)
|
||||||
|
|
||||||
|
|
@ -46,6 +52,9 @@ val next : 'a generator -> 'a
|
||||||
val junk : 'a generator -> unit
|
val junk : 'a generator -> unit
|
||||||
(** Drop element *)
|
(** Drop element *)
|
||||||
|
|
||||||
|
val is_empty : _ t -> bool
|
||||||
|
(** Check whether the enum is empty *)
|
||||||
|
|
||||||
val fold : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b
|
val fold : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b
|
||||||
(** Fold on the generator *)
|
(** Fold on the generator *)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue