pretty printing of an Enum

This commit is contained in:
Simon Cruanes 2013-03-20 01:10:36 +01:00
parent bd99d044f7
commit 64a50fbedf
2 changed files with 23 additions and 0 deletions

19
enum.ml
View file

@ -492,6 +492,25 @@ let int_range i j =
x x
end end
let pp ?(start="") ?(stop="") ?(sep=",") ?(horizontal=false) pp_elem formatter enum =
(if horizontal
then Format.fprintf formatter "@[<h>%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 module Infix = struct
let (@@) = append let (@@) = append

View file

@ -182,6 +182,10 @@ val to_rev_list : 'a t -> 'a list
val int_range : int -> int -> int t 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 module Infix : sig
val (@@) : 'a t -> 'a t -> 'a t val (@@) : 'a t -> 'a t -> 'a t
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t val (>>=) : 'a t -> ('a -> 'b t) -> 'b t