mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-05 19:00:31 -05:00
CCFormat: add append, append_l, infix ++ for sequencing
This commit is contained in:
parent
b1643cfbd5
commit
d0b05fdb76
2 changed files with 37 additions and 0 deletions
|
|
@ -107,6 +107,24 @@ let triple ?(sep=return ",@ ") ppa ppb ppc fmt (a, b, c) =
|
|||
let quad ?(sep=return ",@ ") ppa ppb ppc ppd fmt (a, b, c, d) =
|
||||
Format.fprintf fmt "%a%a%a%a%a%a%a" ppa a sep () ppb b sep () ppc c sep () ppd d
|
||||
|
||||
let append ppa ppb fmt () =
|
||||
ppa fmt ();
|
||||
ppb fmt ()
|
||||
|
||||
(*$= append & ~printer:(fun s -> CCFormat.sprintf "%S" s)
|
||||
"foobar" (to_string_test (append (return "foo") (return "bar")))
|
||||
"bar" (to_string_test (append (return "") (return "bar")))
|
||||
"foo" (to_string_test (append (return "foo") (return "")))
|
||||
*)
|
||||
|
||||
let append_l = List.fold_left append (return "")
|
||||
|
||||
(*$= append_l & ~printer:(fun s -> CCFormat.sprintf "%S" s)
|
||||
"" (to_string_test @@ append_l [])
|
||||
"foobarbaz" (to_string_test @@ append_l (List.map return ["foo"; "bar"; "baz"]))
|
||||
"3141" (to_string_test @@ append_l (List.map (const int) [3; 14; 1]))
|
||||
*)
|
||||
|
||||
let within a b p out x =
|
||||
string out a;
|
||||
p out x;
|
||||
|
|
@ -444,3 +462,9 @@ end
|
|||
"[(Ok \"a b c\");(Error \"nope\")]" \
|
||||
(to_string Dump.(list (result string)) [Ok "a b c"; Error "nope"])
|
||||
*)
|
||||
|
||||
module Infix = struct
|
||||
let (++) = append
|
||||
end
|
||||
|
||||
include Infix
|
||||
|
|
|
|||
|
|
@ -95,6 +95,12 @@ val triple : ?sep:unit printer -> 'a printer -> 'b printer -> 'c printer -> ('a
|
|||
val quad : ?sep:unit printer -> 'a printer -> 'b printer ->
|
||||
'c printer -> 'd printer -> ('a * 'b * 'c * 'd) printer
|
||||
|
||||
val append : unit printer -> unit printer -> unit printer
|
||||
(** [append ppa ppb] first prints [ppa ()], then prints [ppb ()]. *)
|
||||
|
||||
val append_l : unit printer list -> unit printer
|
||||
(** [append_l pps] runs the printers in [pps] sequentially. *)
|
||||
|
||||
val within : string -> string -> 'a printer -> 'a printer
|
||||
(** [within a b p] wraps [p] inside the strings [a] and [b]. Convenient,
|
||||
for instances, for brackets, parenthesis, quotes, etc.
|
||||
|
|
@ -349,3 +355,10 @@ module Dump : sig
|
|||
val to_string : 'a t -> 'a -> string
|
||||
(** Alias to {!CCFormat.to_string}. *)
|
||||
end
|
||||
|
||||
module Infix : sig
|
||||
val (++) : unit printer -> unit printer -> unit printer
|
||||
(** Alias to {!append}. *)
|
||||
end
|
||||
|
||||
include module type of Infix
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue