mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
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`
This commit is contained in:
parent
c1704d71ff
commit
0dafceb708
16 changed files with 134 additions and 0 deletions
|
|
@ -540,6 +540,18 @@ let pp_i ?(sep=", ") pp_item out a =
|
||||||
pp_item k out a.(k)
|
pp_item k out a.(k)
|
||||||
done
|
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_seq a k = iter k a
|
||||||
|
|
||||||
let to_gen a =
|
let to_gen a =
|
||||||
|
|
|
||||||
|
|
@ -244,6 +244,11 @@ val random_choose : 'a t -> 'a random_gen
|
||||||
(** [random_choose a rs] randomly chooses an element of [a].
|
(** [random_choose a rs] randomly chooses an element of [a].
|
||||||
@raise Not_found if the array/slice is empty. *)
|
@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
|
val to_seq : 'a t -> 'a sequence
|
||||||
(** [to_seq a] returns a [sequence] of the elements of an array [a].
|
(** [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
|
The input array [a] is shared with the sequence and modification of it will result
|
||||||
|
|
|
||||||
|
|
@ -245,6 +245,11 @@ val random_choose : 'a t -> 'a random_gen
|
||||||
(** [random_choose a rs] randomly chooses an element of [a].
|
(** [random_choose a rs] randomly chooses an element of [a].
|
||||||
@raise Not_found if the array/slice is empty. *)
|
@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
|
val to_seq : 'a t -> 'a sequence
|
||||||
(** [to_seq a] returns a [sequence] of the elements of an array [a].
|
(** [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
|
The input array [a] is shared with the sequence and modification of it will result
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,12 @@ let of_int_exn = Char.chr
|
||||||
let of_int c = try Some (of_int_exn c) with _ -> None
|
let of_int c = try Some (of_int_exn c) with _ -> None
|
||||||
let to_int = Char.code
|
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
|
let lowercase_ascii = function
|
||||||
| 'A'..'Z' as c -> Char.unsafe_chr (Char.code c + 32)
|
| 'A'..'Z' as c -> Char.unsafe_chr (Char.code c + 32)
|
||||||
| c -> c
|
| c -> c
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,10 @@ val to_int : t -> int
|
||||||
Return the ASCII code of the argument.
|
Return the ASCII code of the argument.
|
||||||
@since 1.0 *)
|
@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
|
val pp_buf : Buffer.t -> t -> unit
|
||||||
(** Renamed from [pp] since 2.0. *)
|
(** Renamed from [pp] since 2.0. *)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -202,6 +202,10 @@ module type S = sig
|
||||||
val to_tree : t -> elt ktree
|
val to_tree : t -> elt ktree
|
||||||
(** Return a [ktree] of the elements of the heap. *)
|
(** 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
|
val pp : ?sep:string -> elt printer -> t printer
|
||||||
(** @since 0.16
|
(** @since 0.16
|
||||||
Renamed from {!print} @since 2.0 *)
|
Renamed from {!print} @since 2.0 *)
|
||||||
|
|
@ -404,6 +408,22 @@ module Make(E : PARTIAL_ORD) : S with type elt = E.t = struct
|
||||||
| E -> `Nil
|
| E -> `Nil
|
||||||
| N (_, x, l, r) -> `Node(x, [to_tree l; to_tree r])
|
| 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 pp ?(sep=",") pp_elt out h =
|
||||||
let first=ref true in
|
let first=ref true in
|
||||||
iter
|
iter
|
||||||
|
|
|
||||||
|
|
@ -142,6 +142,10 @@ module type S = sig
|
||||||
val to_tree : t -> elt ktree
|
val to_tree : t -> elt ktree
|
||||||
(** Return a [ktree] of the elements of the heap. *)
|
(** 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
|
val pp : ?sep:string -> elt printer -> t printer
|
||||||
(** Printer.
|
(** Printer.
|
||||||
Renamed from {!print} since 2.0
|
Renamed from {!print} since 2.0
|
||||||
|
|
|
||||||
|
|
@ -1649,6 +1649,18 @@ let random_choose l = match l with
|
||||||
|
|
||||||
let random_sequence l st = map (fun g -> g st) l
|
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 to_seq l k = List.iter k l
|
||||||
let of_seq seq =
|
let of_seq seq =
|
||||||
let l = ref [] in
|
let l = ref [] in
|
||||||
|
|
|
||||||
|
|
@ -685,6 +685,12 @@ val random_choose : 'a t -> 'a random_gen
|
||||||
|
|
||||||
val random_sequence : 'a random_gen t -> 'a t 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
|
val to_seq : 'a t -> 'a sequence
|
||||||
(** Return a [sequence] of the elements of the list. *)
|
(** Return a [sequence] of the elements of the list. *)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -683,6 +683,12 @@ val random_choose : 'a t -> 'a random_gen
|
||||||
|
|
||||||
val random_sequence : 'a random_gen t -> 'a t 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
|
val to_seq : 'a t -> 'a sequence
|
||||||
(** Return a [sequence] of the elements of the list. *)
|
(** Return a [sequence] of the elements of the list. *)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,8 @@ let compare f g (x1,y1) (x2,y2) =
|
||||||
let c = f x1 x2 in
|
let c = f x1 x2 in
|
||||||
if c <> 0 then c else g y1 y2
|
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
|
type 'a printer = Format.formatter -> 'a -> unit
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 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
|
type 'a printer = Format.formatter -> 'a -> unit
|
||||||
|
|
||||||
val pp : ?sep:string -> 'a printer -> 'b printer -> ('a * 'b) printer
|
val pp : ?sep:string -> 'a printer -> 'b printer -> ('a * 'b) printer
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,13 @@ type 'a printer = Format.formatter -> 'a -> unit
|
||||||
|
|
||||||
module type OrderedType = Set.OrderedType
|
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
|
module type S = sig
|
||||||
include Set.S
|
include Set.S
|
||||||
|
|
||||||
|
|
@ -59,6 +66,12 @@ module type S = sig
|
||||||
|
|
||||||
val to_list : t -> elt list
|
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 :
|
val pp :
|
||||||
?start:string -> ?stop:string -> ?sep:string ->
|
?start:string -> ?stop:string -> ?sep:string ->
|
||||||
elt printer -> t printer
|
elt printer -> t printer
|
||||||
|
|
@ -135,6 +148,26 @@ module Make(O : Map.OrderedType) = struct
|
||||||
|
|
||||||
let to_list = elements
|
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 =
|
let pp ?(start="") ?(stop="") ?(sep=", ") pp_x fmt m =
|
||||||
Format.pp_print_string fmt start;
|
Format.pp_print_string fmt start;
|
||||||
let first = ref true in
|
let first = ref true in
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,12 @@ module type S = sig
|
||||||
val to_list : t -> elt list
|
val to_list : t -> elt list
|
||||||
(** [to_list t] converts the set [t] to a list of the elements. *)
|
(** [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 :
|
val pp :
|
||||||
?start:string -> ?stop:string -> ?sep:string ->
|
?start:string -> ?stop:string -> ?sep:string ->
|
||||||
elt printer -> t printer
|
elt printer -> t printer
|
||||||
|
|
|
||||||
|
|
@ -942,6 +942,9 @@ let to_klist v =
|
||||||
else `Cons (v.vec.(i), aux (i+1))
|
else `Cons (v.vec.(i), aux (i+1))
|
||||||
in aux 0
|
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 =
|
let pp ?(start="") ?(stop="") ?(sep=", ") pp_item fmt v =
|
||||||
Format.pp_print_string fmt start;
|
Format.pp_print_string fmt start;
|
||||||
iteri
|
iteri
|
||||||
|
|
|
||||||
|
|
@ -287,5 +287,11 @@ val to_klist : ('a,_) t -> 'a klist
|
||||||
val of_gen : ?init:('a, rw) t -> 'a gen -> ('a, rw) t
|
val of_gen : ?init:('a, rw) t -> 'a gen -> ('a, rw) t
|
||||||
val to_gen : ('a,_) t -> 'a gen
|
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 ->
|
val pp : ?start:string -> ?stop:string -> ?sep:string ->
|
||||||
'a printer -> ('a,_) t printer
|
'a printer -> ('a,_) t printer
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue