add CCSet.to_string

This commit is contained in:
Fardale 2019-10-29 20:42:51 +01:00
parent 1e0669e1b0
commit 087612823e
2 changed files with 39 additions and 0 deletions

View file

@ -8,6 +8,13 @@ type 'a printer = Format.formatter -> 'a -> unit
module type OrderedType = Set.OrderedType
(*$inject
module S = CCSet.Make(struct
type t = int
let compare x y = Stdlib.compare x y
end)
*)
module type S = sig
include Set.S
@ -59,6 +66,12 @@ module type S = sig
val to_list : t -> elt list
val to_string :
?start:string -> ?stop:string -> ?sep:string ->
(elt -> string) -> t -> string
(** Print the set in a string
@since NEXT_RELEASE *)
val pp :
?start:string -> ?stop:string -> ?sep:string ->
elt printer -> t printer
@ -135,6 +148,26 @@ module Make(O : Map.OrderedType) = struct
let to_list = elements
let to_string ?(start="") ?(stop="") ?(sep=",") elt_to_string h =
to_list h
|> CCList.to_string ~start ~stop ~sep elt_to_string
(*$= & ~printer:(fun s -> s)
(S.to_string string_of_int (S.of_list [4; 3])) "3,4"
*)
(*$Q
Q.(list int) (fun l -> \
let s = S.of_list l in \
(S.to_string string_of_int s) \
= (CCList.sort_uniq ~cmp:CCInt.compare l \
|> List.map string_of_int |> String.concat ","))
Q.(list int) (fun l -> \
let s = S.of_list l in \
(S.to_string ~sep:" " string_of_int s) \
= (CCList.sort_uniq ~cmp:CCInt.compare l \
|> List.map string_of_int |> String.concat " "))
*)
let pp ?(start="") ?(stop="") ?(sep=", ") pp_x fmt m =
Format.pp_print_string fmt start;
let first = ref true in

View file

@ -65,6 +65,12 @@ module type S = sig
val to_list : t -> elt list
(** [to_list t] converts the set [t] to a list of the elements. *)
val to_string :
?start:string -> ?stop:string -> ?sep:string ->
(elt -> string) -> t -> string
(** Print the set in a string
@since NEXT_RELEASE *)
val pp :
?start:string -> ?stop:string -> ?sep:string ->
elt printer -> t printer