From 9c375ed93e6f89141ad26536a3192603e16aa250 Mon Sep 17 00:00:00 2001 From: Fardale Date: Mon, 28 Oct 2019 13:03:22 +0100 Subject: [PATCH] add `CCList.to_string` --- src/core/CCList.ml | 12 ++++++++++++ src/core/CCList.mli | 6 ++++++ 2 files changed, 18 insertions(+) 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. *)