diff --git a/sequence.ml b/sequence.ml index f33ac57..18b94e9 100644 --- a/sequence.ml +++ b/sequence.ml @@ -713,3 +713,16 @@ let pp_seq ?(sep=", ") pp_elt formatter seq = end); pp_elt formatter x) seq + +let pp_buf ?(sep=", ") pp_elt buf seq = + let first = ref true in + iter + (fun x -> + if !first then first := false else Buffer.add_string buf sep; + pp_elt buf x) + seq + +let to_string ?sep pp_elt seq = + let buf = Buffer.create 25 in + pp_buf ?sep (fun buf x -> Buffer.add_string buf (pp_elt x)) buf seq; + Buffer.contents buf diff --git a/sequence.mli b/sequence.mli index 97aa638..b09a0de 100644 --- a/sequence.mli +++ b/sequence.mli @@ -442,3 +442,10 @@ 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. *) + +val pp_buf : ?sep:string -> (Buffer.t -> 'a -> unit) -> + Buffer.t -> 'a t -> unit + (** Print into a buffer *) + +val to_string : ?sep:string -> ('a -> string) -> 'a t -> string + (** Print into a string *)