From 0dafceb7086d77d4b9155296f3bbbd8d60d1528a Mon Sep 17 00:00:00 2001 From: Fardale <33528128+FardaleM@users.noreply.github.com> Date: Wed, 30 Oct 2019 20:26:52 +0100 Subject: [PATCH] Adding to_string (#270) * add `CCArray.to_string` * add `CCArrayLabels.to_string` * add `CCList.to_string` * add `CCListLabels.to_string` * add `CCChar.to_string` * add `CCPair.to_string` * add `CCHeap.to_string` * add `CCSet.to_string` * add `CCVector.to_string` --- src/core/CCArray.ml | 12 ++++++++++++ src/core/CCArray.mli | 5 +++++ src/core/CCArrayLabels.mli | 5 +++++ src/core/CCChar.ml | 6 ++++++ src/core/CCChar.mli | 4 ++++ src/core/CCHeap.ml | 20 ++++++++++++++++++++ src/core/CCHeap.mli | 4 ++++ src/core/CCList.ml | 12 ++++++++++++ src/core/CCList.mli | 6 ++++++ src/core/CCListLabels.mli | 6 ++++++ src/core/CCPair.ml | 2 ++ src/core/CCPair.mli | 4 ++++ src/core/CCSet.ml | 33 +++++++++++++++++++++++++++++++++ src/core/CCSet.mli | 6 ++++++ src/core/CCVector.ml | 3 +++ src/core/CCVector.mli | 6 ++++++ 16 files changed, 134 insertions(+) diff --git a/src/core/CCArray.ml b/src/core/CCArray.ml index b424be94..37020788 100644 --- a/src/core/CCArray.ml +++ b/src/core/CCArray.ml @@ -540,6 +540,18 @@ let pp_i ?(sep=", ") pp_item out a = pp_item k out a.(k) done +let to_string ?(sep=", ") item_to_string a = + Array.to_list a + |> List.map item_to_string + |> String.concat sep + +(*$= to_string & ~printer:(fun s -> s) + (to_string string_of_int [|1;2;3;4;5;6|]) "1, 2, 3, 4, 5, 6" + (to_string string_of_int [||]) "" + (to_string ~sep:" " string_of_int [|1;2;3;4;5;6|]) "1 2 3 4 5 6" + (to_string string_of_int [|1|]) "1" +*) + let to_seq a k = iter k a let to_gen a = diff --git a/src/core/CCArray.mli b/src/core/CCArray.mli index cc6738e5..e25c68ca 100644 --- a/src/core/CCArray.mli +++ b/src/core/CCArray.mli @@ -244,6 +244,11 @@ val random_choose : 'a t -> 'a random_gen (** [random_choose a rs] randomly chooses an element of [a]. @raise Not_found if the array/slice is empty. *) +val to_string : ?sep:string -> ('a -> string) -> 'a array -> string +(** [to_string ~sep item_to_string a] print [a] to a string using [sep] as a separator + between elements of [a]. + @since NEXT_RELEASE *) + val to_seq : 'a t -> 'a sequence (** [to_seq a] returns a [sequence] of the elements of an array [a]. The input array [a] is shared with the sequence and modification of it will result diff --git a/src/core/CCArrayLabels.mli b/src/core/CCArrayLabels.mli index 126acd4a..b14e42e4 100644 --- a/src/core/CCArrayLabels.mli +++ b/src/core/CCArrayLabels.mli @@ -245,6 +245,11 @@ val random_choose : 'a t -> 'a random_gen (** [random_choose a rs] randomly chooses an element of [a]. @raise Not_found if the array/slice is empty. *) +val to_string : ?sep:string -> ('a -> string) -> 'a array -> string +(** [to_string ~sep item_to_string a] print [a] to a string using [sep] as a separator + between elements of [a]. + @since NEXT_RELEASE *) + val to_seq : 'a t -> 'a sequence (** [to_seq a] returns a [sequence] of the elements of an array [a]. The input array [a] is shared with the sequence and modification of it will result diff --git a/src/core/CCChar.ml b/src/core/CCChar.ml index 4323a764..57556956 100644 --- a/src/core/CCChar.ml +++ b/src/core/CCChar.ml @@ -16,6 +16,12 @@ let of_int_exn = Char.chr let of_int c = try Some (of_int_exn c) with _ -> None let to_int = Char.code +let to_string c = String.make 1 c + +(*$Q to_string + (Q.string_of_size (Q.Gen.return 1)) (fun s -> to_string s.[0] = s) +*) + let lowercase_ascii = function | 'A'..'Z' as c -> Char.unsafe_chr (Char.code c + 32) | c -> c diff --git a/src/core/CCChar.mli b/src/core/CCChar.mli index 974e9096..3e9f3fa3 100644 --- a/src/core/CCChar.mli +++ b/src/core/CCChar.mli @@ -40,6 +40,10 @@ val to_int : t -> int Return the ASCII code of the argument. @since 1.0 *) +val to_string : t -> string +(** [to_string c] return a string containing [c] + @since NEXT_RELEASE *) + val pp_buf : Buffer.t -> t -> unit (** Renamed from [pp] since 2.0. *) diff --git a/src/core/CCHeap.ml b/src/core/CCHeap.ml index 5afc9d2d..42d2ba82 100644 --- a/src/core/CCHeap.ml +++ b/src/core/CCHeap.ml @@ -202,6 +202,10 @@ module type S = sig val to_tree : t -> elt ktree (** Return a [ktree] of the elements of the heap. *) + val to_string : ?sep:string -> (elt -> string) -> t -> string + (** Print the heap in a string + @since NEXT_RELEASE *) + val pp : ?sep:string -> elt printer -> t printer (** @since 0.16 Renamed from {!print} @since 2.0 *) @@ -404,6 +408,22 @@ module Make(E : PARTIAL_ORD) : S with type elt = E.t = struct | E -> `Nil | N (_, x, l, r) -> `Node(x, [to_tree l; to_tree r]) + let to_string ?(sep=",") elt_to_string h = + to_list_sorted h + |> List.map elt_to_string + |> String.concat sep + + (*$Q + Q.(list int) (fun l -> \ + let h = H.of_list l in \ + (H.to_string string_of_int h) \ + = (List.sort Stdlib.compare l |> List.map string_of_int |> String.concat ",")) + Q.(list int) (fun l -> \ + let h = H.of_list l in \ + (H.to_string ~sep:" " string_of_int h) \ + = (List.sort Stdlib.compare l |> List.map string_of_int |> String.concat " ")) + *) + let pp ?(sep=",") pp_elt out h = let first=ref true in iter diff --git a/src/core/CCHeap.mli b/src/core/CCHeap.mli index 5cd427d9..52c4077c 100644 --- a/src/core/CCHeap.mli +++ b/src/core/CCHeap.mli @@ -142,6 +142,10 @@ module type S = sig val to_tree : t -> elt ktree (** Return a [ktree] of the elements of the heap. *) + val to_string : ?sep:string -> (elt -> string) -> t -> string + (** Print the heap in a string + @since NEXT_RELEASE *) + val pp : ?sep:string -> elt printer -> t printer (** Printer. Renamed from {!print} since 2.0 diff --git a/src/core/CCList.ml b/src/core/CCList.ml index fb483d73..96548f8e 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -1649,6 +1649,18 @@ let random_choose l = match l with let random_sequence l st = map (fun g -> g st) l +let to_string ?(start="") ?(stop="") ?(sep=", ") item_to_string l = + let l = List.map item_to_string l in + start ^ (String.concat sep l) ^ stop + +(*$= to_string & ~printer:(fun s -> s) + (to_string string_of_int []) "" + (to_string ~start:"[" ~stop:"]" string_of_int []) "[]" + (to_string ~start:"[" ~stop:"]" string_of_int [1]) "[1]" + (to_string ~start:"[" ~stop:"]" string_of_int [1;2;3;4]) "[1, 2, 3, 4]" + (to_string ~sep:" " string_of_int [1;2;3;4]) "1 2 3 4" +*) + let to_seq l k = List.iter k l let of_seq seq = let l = ref [] in diff --git a/src/core/CCList.mli b/src/core/CCList.mli index 9d331dbe..6391e0b1 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -685,6 +685,12 @@ val random_choose : 'a t -> 'a random_gen val random_sequence : 'a random_gen t -> 'a t random_gen +val to_string : ?start:string -> ?stop:string -> ?sep:string -> + ('a -> string) -> 'a t -> string +(** [to_string ~start ~stop ~sep item_to_string l] print [l] to a string using + [sep] as a separator between elements of [l]. + @since NEXT_RELEASE *) + val to_seq : 'a t -> 'a sequence (** Return a [sequence] of the elements of the list. *) diff --git a/src/core/CCListLabels.mli b/src/core/CCListLabels.mli index fbfe7bd7..d32f582a 100644 --- a/src/core/CCListLabels.mli +++ b/src/core/CCListLabels.mli @@ -683,6 +683,12 @@ val random_choose : 'a t -> 'a random_gen val random_sequence : 'a random_gen t -> 'a t random_gen +val to_string : ?start:string -> ?stop:string -> ?sep:string -> + ('a -> string) -> 'a t -> string +(** [to_string ~start ~stop ~sep item_to_string l] print [l] to a string using + [sep] as a separator between elements of [l]. + @since NEXT_RELEASE *) + val to_seq : 'a t -> 'a sequence (** Return a [sequence] of the elements of the list. *) diff --git a/src/core/CCPair.ml b/src/core/CCPair.ml index 80884e20..ea1a9a2c 100644 --- a/src/core/CCPair.ml +++ b/src/core/CCPair.ml @@ -42,6 +42,8 @@ let compare f g (x1,y1) (x2,y2) = let c = f x1 x2 in if c <> 0 then c else g y1 y2 +let to_string ?(sep=", ") a_to_string b_to_string (x,y) = + Printf.sprintf "%s%s%s" (a_to_string x) sep (b_to_string y) type 'a printer = Format.formatter -> 'a -> unit diff --git a/src/core/CCPair.mli b/src/core/CCPair.mli index 28c61577..bd8cedc8 100644 --- a/src/core/CCPair.mli +++ b/src/core/CCPair.mli @@ -67,6 +67,10 @@ val equal : ('a -> 'a -> bool) -> ('b -> 'b -> bool) -> ('a * 'b) -> ('a * 'b) - val compare : ('a -> 'a -> int) -> ('b -> 'b -> int) -> ('a * 'b) -> ('a * 'b) -> int +val to_string : ?sep:string -> ('a -> string) -> ('b -> string) -> ('a * 'b) -> string +(** Print tuple in a string + @since NEXT_RELEASE *) + type 'a printer = Format.formatter -> 'a -> unit val pp : ?sep:string -> 'a printer -> 'b printer -> ('a * 'b) printer 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 diff --git a/src/core/CCVector.ml b/src/core/CCVector.ml index 3bde49e1..92175e39 100644 --- a/src/core/CCVector.ml +++ b/src/core/CCVector.ml @@ -942,6 +942,9 @@ let to_klist v = else `Cons (v.vec.(i), aux (i+1)) in aux 0 +let to_string ?(start="") ?(stop="") ?(sep=", ") item_to_string v = + to_list v |> CCList.to_string ~start ~stop ~sep item_to_string + let pp ?(start="") ?(stop="") ?(sep=", ") pp_item fmt v = Format.pp_print_string fmt start; iteri diff --git a/src/core/CCVector.mli b/src/core/CCVector.mli index 42f4bd4e..de5b0e6c 100644 --- a/src/core/CCVector.mli +++ b/src/core/CCVector.mli @@ -287,5 +287,11 @@ val to_klist : ('a,_) t -> 'a klist val of_gen : ?init:('a, rw) t -> 'a gen -> ('a, rw) t val to_gen : ('a,_) t -> 'a gen +val to_string : + ?start:string -> ?stop:string -> ?sep:string -> + ('a -> string) -> ('a,_) t -> string +(** Print the vector in a string + @since NEXT_RELEASE *) + val pp : ?start:string -> ?stop:string -> ?sep:string -> 'a printer -> ('a,_) t printer