* [Caml-list] Question on Printf wrappers @ 2016-08-12 12:44 Soegtrop, Michael 2016-08-12 12:51 ` Kakadu ` (2 more replies) 0 siblings, 3 replies; 6+ messages in thread From: Soegtrop, Michael @ 2016-08-12 12:44 UTC (permalink / raw) To: caml-list [-- Attachment #1: Type: text/plain, Size: 1429 bytes --] Dear Ocaml Users, it is quite common to have wrappers for printf to e.g. print errors or warnings with some context. Prefixing is easy to do in OCaml (honestly it took me a short while to figure it out): let prefixprintf (oc : out_channel) (fmt : ('a, out_channel, unit) format) : 'a = Printf.fprintf oc "Prefix: "; Printf.fprintf oc fmt ;; I wonder how I would postfix something. Even converting the formatted output first to a string and then output it together with the postfix doesn't seem to be easy, because the formatting function must be the last call in the function, since it has to produce the returned function. Is there some way to make a variadic lambda expression or some other magic to handle such cases? A similar question would be how I can print the same formatted string twice. I don't have an application for this, I ask this out of pure OCaml curiosity. So I am not so much interested in workarounds, more in an answer to the question how I can return a function of type 'a without more or less directly calling some variant of printf. 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: 3742 bytes --] ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Question on Printf wrappers 2016-08-12 12:44 [Caml-list] Question on Printf wrappers Soegtrop, Michael @ 2016-08-12 12:51 ` Kakadu 2016-08-12 12:55 ` Edouard Evangelisti 2016-08-12 12:53 ` Gabriel Scherer 2016-08-14 9:17 ` Richard W.M. Jones 2 siblings, 1 reply; 6+ messages in thread From: Kakadu @ 2016-08-12 12:51 UTC (permalink / raw) To: Soegtrop, Michael; +Cc: caml-list Micheal, Something lke this? let printfn fmt = kprintf (printf "%s\n") fmt Happy hacking, Kakadu On Fri, Aug 12, 2016 at 3:44 PM, Soegtrop, Michael <michael.soegtrop@intel.com> wrote: > Dear Ocaml Users, > > > > it is quite common to have wrappers for printf to e.g. print errors or > warnings with some context. Prefixing is easy to do in OCaml (honestly it > took me a short while to figure it out): > > > > let prefixprintf (oc : out_channel) (fmt : ('a, out_channel, unit) format) : > 'a = > > Printf.fprintf oc "Prefix: "; > > Printf.fprintf oc fmt > > ;; > > > > I wonder how I would postfix something. Even converting the formatted output > first to a string and then output it together with the postfix doesn’t seem > to be easy, because the formatting function must be the last call in the > function, since it has to produce the returned function. Is there some way > to make a variadic lambda expression or some other magic to handle such > cases? A similar question would be how I can print the same formatted string > twice. > > > > I don’t have an application for this, I ask this out of pure OCaml > curiosity. So I am not so much interested in workarounds, more in an answer > to the question how I can return a function of type ‘a without more or less > directly calling some variant of printf. > > > > 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 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Question on Printf wrappers 2016-08-12 12:51 ` Kakadu @ 2016-08-12 12:55 ` Edouard Evangelisti 2016-08-12 14:01 ` Soegtrop, Michael 0 siblings, 1 reply; 6+ messages in thread From: Edouard Evangelisti @ 2016-08-12 12:55 UTC (permalink / raw) To: Kakadu; +Cc: Soegtrop, Michael, caml-list [-- Attachment #1: Type: text/plain, Size: 2523 bytes --] You may also consider this: let prefixprint oc fmt = Printf.fprintf oc ("Prefix: " ^^ fmt) Best regards, Edouard 2016-08-12 13:51 GMT+01:00 Kakadu <kakadu.hafanana@gmail.com>: > Micheal, > > Something lke this? > > let printfn fmt = kprintf (printf "%s\n") fmt > > Happy hacking, > Kakadu > > On Fri, Aug 12, 2016 at 3:44 PM, Soegtrop, Michael > <michael.soegtrop@intel.com> wrote: > > Dear Ocaml Users, > > > > > > > > it is quite common to have wrappers for printf to e.g. print errors or > > warnings with some context. Prefixing is easy to do in OCaml (honestly it > > took me a short while to figure it out): > > > > > > > > let prefixprintf (oc : out_channel) (fmt : ('a, out_channel, unit) > format) : > > 'a = > > > > Printf.fprintf oc "Prefix: "; > > > > Printf.fprintf oc fmt > > > > ;; > > > > > > > > I wonder how I would postfix something. Even converting the formatted > output > > first to a string and then output it together with the postfix doesn’t > seem > > to be easy, because the formatting function must be the last call in the > > function, since it has to produce the returned function. Is there some > way > > to make a variadic lambda expression or some other magic to handle such > > cases? A similar question would be how I can print the same formatted > string > > twice. > > > > > > > > I don’t have an application for this, I ask this out of pure OCaml > > curiosity. So I am not so much interested in workarounds, more in an > answer > > to the question how I can return a function of type ‘a without more or > less > > directly calling some variant of printf. > > > > > > > > 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 > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs -- *Edouard Evangelisti* *Post doctoral Research Associate* *Sainsbury Laboratory, Cambridge University (SLCU)* *Bateman StreetCambridge CB2 1LR (United Kingdom)* [-- Attachment #2: Type: text/html, Size: 4816 bytes --] ^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: [Caml-list] Question on Printf wrappers 2016-08-12 12:55 ` Edouard Evangelisti @ 2016-08-12 14:01 ` Soegtrop, Michael 0 siblings, 0 replies; 6+ messages in thread From: Soegtrop, Michael @ 2016-08-12 14:01 UTC (permalink / raw) To: caml-list [-- 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 --] ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Question on Printf wrappers 2016-08-12 12:44 [Caml-list] Question on Printf wrappers Soegtrop, Michael 2016-08-12 12:51 ` Kakadu @ 2016-08-12 12:53 ` Gabriel Scherer 2016-08-14 9:17 ` Richard W.M. Jones 2 siblings, 0 replies; 6+ messages in thread From: Gabriel Scherer @ 2016-08-12 12:53 UTC (permalink / raw) To: Soegtrop, Michael; +Cc: caml-list [-- Attachment #1: Type: text/plain, Size: 2172 bytes --] You can do a lot with the format concatenation operator (^^): let prefixprintf prefix o fmt = Printf.fprintf o (prefix ^^ fmt) let suffixprintf suffix o fmt = Printf.fprintf o (fmt ^^ suffix) # prefixprintf "foo: " stdout "bar %d" 5;; foo: bar 5- : unit = () # suffixprintf ": foo" stdout "bar %d" 5;; bar 5: foo- : unit = () The type is very general, you can prefix or suffix with more than a litteral string: # suffixprintf ": foo %d" stdout "bar %d" 5 6;; bar 5: foo 6- : unit = () On Fri, Aug 12, 2016 at 2:44 PM, Soegtrop, Michael < michael.soegtrop@intel.com> wrote: > Dear Ocaml Users, > > > > it is quite common to have wrappers for printf to e.g. print errors or > warnings with some context. Prefixing is easy to do in OCaml (honestly it > took me a short while to figure it out): > > > > let prefixprintf (oc : out_channel) (fmt : ('a, out_channel, unit) format) > : 'a = > > Printf.fprintf oc "Prefix: "; > > Printf.fprintf oc fmt > > ;; > > > > I wonder how I would postfix something. Even converting the formatted > output first to a string and then output it together with the postfix > doesn’t seem to be easy, because the formatting function must be the last > call in the function, since it has to produce the returned function. Is > there some way to make a variadic lambda expression or some other magic to > handle such cases? A similar question would be how I can print the same > formatted string twice. > > > > I don’t have an application for this, I ask this out of pure OCaml > curiosity. So I am not so much interested in workarounds, more in an answer > to the question how I can return a function of type ‘a without more or less > directly calling some variant of printf. > > > > 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: 3382 bytes --] ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Question on Printf wrappers 2016-08-12 12:44 [Caml-list] Question on Printf wrappers Soegtrop, Michael 2016-08-12 12:51 ` Kakadu 2016-08-12 12:53 ` Gabriel Scherer @ 2016-08-14 9:17 ` Richard W.M. Jones 2 siblings, 0 replies; 6+ messages in thread From: Richard W.M. Jones @ 2016-08-14 9:17 UTC (permalink / raw) To: Soegtrop, Michael; +Cc: caml-list On Fri, Aug 12, 2016 at 12:44:51PM +0000, Soegtrop, Michael wrote: > Dear Ocaml Users, > > it is quite common to have wrappers for printf to e.g. print errors or warnings with some context. Prefixing is easy to do in OCaml (honestly it took me a short while to figure it out): > > let prefixprintf (oc : out_channel) (fmt : ('a, out_channel, unit) format) : 'a = > Printf.fprintf oc "Prefix: "; > Printf.fprintf oc fmt > ;; As Kakadu said, you should look into the ksprintf function. There are several real world examples of useful warning/error/debug/ message functions, if you start at this link and search down the file for 'ksprintf': https://github.com/libguestfs/libguestfs/blob/master/mllib/common_utils.ml#L355 The functions display ANSI colours and do line wrapping. The corresponding type signatures can be found in: https://github.com/libguestfs/libguestfs/blob/master/mllib/common_utils.mli Rich. ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2016-08-14 9:17 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2016-08-12 12:44 [Caml-list] Question on Printf wrappers Soegtrop, Michael 2016-08-12 12:51 ` Kakadu 2016-08-12 12:55 ` Edouard Evangelisti 2016-08-12 14:01 ` Soegtrop, Michael 2016-08-12 12:53 ` Gabriel Scherer 2016-08-14 9:17 ` Richard W.M. Jones
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox