diff --git a/enum.ml b/enum.ml index 1637d4bb..4156e626 100644 --- a/enum.ml +++ b/enum.ml @@ -492,6 +492,25 @@ let int_range i j = x end +let pp ?(start="") ?(stop="") ?(sep=",") ?(horizontal=false) pp_elem formatter enum = + (if horizontal + then Format.fprintf formatter "@[%s" start + else Format.fprintf formatter "@[%s" start); + let gen = enum () in + let rec next is_first = + let continue_ = + try + let x = gen () in + (if not is_first + then Format.fprintf formatter "%s@,%a" sep pp_elem x + else pp_elem formatter x); + true + with EOG -> false in + if continue_ then next false + in + next true; + Format.fprintf formatter "%s@]" stop + module Infix = struct let (@@) = append diff --git a/enum.mli b/enum.mli index b089a0d4..789fbc49 100644 --- a/enum.mli +++ b/enum.mli @@ -182,6 +182,10 @@ val to_rev_list : 'a t -> 'a list val int_range : int -> int -> int t +val pp : ?start:string -> ?stop:string -> ?sep:string -> ?horizontal:bool -> + (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unit + (** Pretty print an enum *) + module Infix : sig val (@@) : 'a t -> 'a t -> 'a t val (>>=) : 'a t -> ('a -> 'b t) -> 'b t