diff --git a/src/core/CCSet.ml b/src/core/CCSet.ml index 32ae1a31..6f91db6b 100644 --- a/src/core/CCSet.ml +++ b/src/core/CCSet.ml @@ -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 diff --git a/src/core/CCSet.mli b/src/core/CCSet.mli index 7424c0e7..d26f054b 100644 --- a/src/core/CCSet.mli +++ b/src/core/CCSet.mli @@ -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