From: "Soegtrop, Michael" <michael.soegtrop@intel.com>
To: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: RE: [Caml-list] Question on Printf wrappers
Date: Fri, 12 Aug 2016 14:01:45 +0000 [thread overview]
Message-ID: <0F7D3B1B3C4B894D824F5B822E3E5A172CF39E99@IRSMSX102.ger.corp.intel.com> (raw)
In-Reply-To: <CAMCFErTu+Y4bE8+bRzrObddq0aMqS=TEdcahA4m_+FJtQY7kAQ@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 2747 bytes --]
Dear Kakadu, Gabriel, Edouard,
thanks a lot for the valuable hints! It is not so obvious from the manual. For those curious (like me) how the types of such functions might look like, here are a few examples:
(* 'a is a function from the arguments of the first format (prefix) to 'c.
'c is a function from the arguments of the second format (fmt) to 'd.
'd is a function from the arguments of the third format (postfix) to unit.
So 'a is a function from arguments required by all formats to unit.
And 'c is a function from arguments required by the last 2 formats and to unit. *)
let bracketprintf
(prefix : ('a, out_channel, unit, unit, 'b, 'c) format6)
(suffix : ('d, out_channel, unit, 'e, unit, unit) format6)
(oc : out_channel)
(fmt : ('c, out_channel, unit, 'b, 'e, 'd) format6)
: 'a
= Printf.fprintf oc (prefix ^^ fmt ^^ suffix);;
(* 'a is a function from the arguments of <fmt> followed by two int arguments for the supplied suffix to unit.
The two string arguments for the internal prefix are supplied by the string -> string -> 'a *)
let myprintf1 (oc : out_channel) (fmt : ('a, out_channel, unit, unit, unit, int -> int-> unit) format6) : string -> string -> 'a =
bracketprintf "Prefix1 %s %s\n" "Suffix1 %d %d\n" oc fmt
;;
(* As above, but with the ^^ directly in the function *)
let myprintf2 (oc : out_channel) (fmt : ('a, out_channel, unit, unit, unit, int -> int-> unit) format6) : string -> string -> 'a =
Printf.fprintf oc ("Prefix2 %s %s\n" ^^ fmt ^^ "Suffix2 %d %d\n")
;;
(* As above, but the two string parameters for the prefix are supplied internally *)
let myprintf3 (oc : out_channel) (fmt : ('a, out_channel, unit, unit, unit, int -> int-> unit) format6) : 'a =
Printf.fprintf oc ("Prefix3 %s %s\n" ^^ fmt ^^ "Suffix3 %d %d\n") "abc" "def"
;;
(* If parameters for the suffix shall be applied internally, kfprintf needs to be used instead of format concatenation *)
let myprintf4 (oc : out_channel) (fmt : ('a, out_channel, unit, unit, unit, unit) format6) : 'a =
Printf.kfprintf (fun oc -> Printf.fprintf oc "Suffix4 %d %d\n" 5 6) oc ("Prefix4 %s %s\n" ^^ fmt) "abc" "def"
;;
let () =
let test = open_out "Test.txt" in
myprintf1 test "%d %d\n" "abc" "def" 3 4 5 6;
myprintf2 test "%d %d\n" "abc" "def" 3 4 5 6;
myprintf3 test "%d %d\n" 3 4 5 6;
myprintf4 test "%d %d\n" 3 4;
Best regards,
Michael
Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Christian Lamprechter
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928
[-- Attachment #2: Type: text/html, Size: 11139 bytes --]
next prev parent reply other threads:[~2016-08-12 14:02 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-08-12 12:44 Soegtrop, Michael
2016-08-12 12:51 ` Kakadu
2016-08-12 12:55 ` Edouard Evangelisti
2016-08-12 14:01 ` Soegtrop, Michael [this message]
2016-08-12 12:53 ` Gabriel Scherer
2016-08-14 9:17 ` Richard W.M. Jones
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0F7D3B1B3C4B894D824F5B822E3E5A172CF39E99@IRSMSX102.ger.corp.intel.com \
--to=michael.soegtrop@intel.com \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).