add CCArray.to_string

This commit is contained in:
Fardale 2019-10-28 00:15:15 +01:00
parent 509dacb96f
commit 55d72263d1
2 changed files with 17 additions and 0 deletions

View file

@ -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 =

View file

@ -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