generic sequence pretty-printing function

This commit is contained in:
Simon Cruanes 2013-01-29 11:13:46 +01:00
parent fbc4946a99
commit ee4ce9c9ba
3 changed files with 22 additions and 5 deletions

View file

@ -201,3 +201,16 @@ module Set =
let module S = (val m : Set.S with type elt = s and type t = t) in
fold (fun set x -> S.add x set) S.empty seq
end
(** {2 Pretty printing of sequences} *)
(** Pretty print a sequence of ['a], using the given pretty printer
to print each elements. An optional separator string can be provided. *)
let pp_seq ?(sep=", ") pp_elt formatter seq =
let first = ref true in
iter
(fun x ->
(if !first then first := false else Format.pp_print_string formatter sep);
pp_elt formatter x;
Format.pp_print_cut formatter ())
seq

View file

@ -152,3 +152,10 @@ module Set :
val to_seq : (module Set.S with type elt = 'a and type t = 'b) -> 'b -> 'a t
val of_seq : (module Set.S with type elt = 'a and type t = 'b) -> 'a t -> 'b
end
(** {2 Pretty printing of sequences} *)
val pp_seq : ?sep:string -> (Format.formatter -> 'a -> unit) ->
Format.formatter -> 'a t -> unit
(** Pretty print a sequence of ['a], using the given pretty printer
to print each elements. An optional separator string can be provided. *)

View file

@ -2,11 +2,8 @@
(** {2 Test sequences} *)
(** print a list of items using the printing function *)
let rec pp_list ?(sep=", ") pp_item formatter = function
| x::y::xs -> Format.fprintf formatter "%a%s@,%a"
pp_item x sep (pp_list ~sep:sep pp_item) (y::xs)
| x::[] -> pp_item formatter x
| [] -> ()
let pp_list ?(sep=", ") pp_item formatter l =
Sequence.pp_seq ~sep pp_item formatter (Sequence.List.to_seq l)
(** Set of integers *)
module ISet = Set.Make(struct type t = int let compare = compare end)