mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2026-01-23 17:46:40 -05:00
stub for monadic IO in CCPrint
This commit is contained in:
parent
3381fbcf38
commit
39c33046ce
2 changed files with 53 additions and 0 deletions
|
|
@ -144,3 +144,34 @@ let _with_file_out filename f =
|
|||
|
||||
let to_file filename format =
|
||||
_with_file_out filename (fun oc -> fprintf oc format)
|
||||
|
||||
(** {2 Monadic IO} *)
|
||||
|
||||
module type MONAD_IO = sig
|
||||
type 'a t (** the IO monad *)
|
||||
type output (** Output channels *)
|
||||
|
||||
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
|
||||
|
||||
val write : output -> string -> unit t
|
||||
end
|
||||
|
||||
module MakeIO(M : MONAD_IO) = struct
|
||||
let output out pp x =
|
||||
let buf = Buffer.create 128 in
|
||||
pp buf x;
|
||||
M.write out (Buffer.contents buf)
|
||||
|
||||
let printl out pp x =
|
||||
let buf = Buffer.create 128 in
|
||||
pp buf x;
|
||||
Buffer.add_char buf '\n';
|
||||
M.write out (Buffer.contents buf)
|
||||
|
||||
let fprintf out format =
|
||||
let buf = Buffer.create 128 in
|
||||
Printf.kbprintf
|
||||
(fun buf -> M.write out (Buffer.contents buf))
|
||||
buf
|
||||
format
|
||||
end
|
||||
|
|
|
|||
|
|
@ -76,3 +76,25 @@ val to_file : string -> ('a, Buffer.t, unit, unit) format4 -> 'a
|
|||
|
||||
val printf : ('a, Buffer.t, unit, unit) format4 -> 'a
|
||||
val eprintf : ('a, Buffer.t, unit, unit) format4 -> 'a
|
||||
|
||||
(** {2 Monadic IO} *)
|
||||
|
||||
module type MONAD_IO = sig
|
||||
type 'a t (** the IO monad *)
|
||||
type output (** Output channels *)
|
||||
|
||||
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
|
||||
|
||||
val write : output -> string -> unit t
|
||||
end
|
||||
|
||||
module MakeIO(M : MONAD_IO) : sig
|
||||
val output : M.output -> 'a t -> 'a -> unit M.t
|
||||
(** Output a single value *)
|
||||
|
||||
val printl : M.output -> 'a t -> 'a -> unit M.t
|
||||
(** Output a value and add a newline "\n" after. *)
|
||||
|
||||
val fprintf : M.output -> ('a, Buffer.t, unit, unit M.t) format4 -> 'a
|
||||
(** Fprintf on a monadic output *)
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue