From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id A59377F72A for ; Fri, 12 Aug 2016 14:54:27 +0200 (CEST) IronPort-PHdr: 9a23:BxnYlx0lOBx+JMGxsmDT+DRfVm0co7zxezQtwd8ZsekXL/ad9pjvdHbS+e9qxAeQG96KsrQd06GL7+igATVGusfZ9ihaMdRlbFwssY0uhQsuAcqIWwXQDcXBSGgEJvlET0Jv5HqhMEJYS47UblzWpWCuv3ZJQk2sfTR8Kum9IIPOlcP/j7n0oMyKJVwQz2fhKfMqdVPt/F2X7pFXyaJZaY8JgiPTpXVJf+kEjUhJHnm02yjG28Gr4ZR4+D5Rsf9yv+RJUKH9YrhqBecAVGduGykP6cbqrRjOSxeUrjtZCz1O00kAPw+Qpir9U5jtqCzi8qJY2SKaNMDyB/hgXDWp765mTFnzjyoIKyQ+6EnWjNB9iORQpxf39DJlxIuBT4ifLvtzeuvmdtMXX2dbFpJeXiZbA464KZAED+cbMPxwoIz0pl9Iphy7U1r/TNjzwyNF0yellZYx1P4sRESfhFQt Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=gabriel.scherer@gmail.com; spf=Pass smtp.mailfrom=gabriel.scherer@gmail.com; spf=None smtp.helo=postmaster@mail-it0-f46.google.com Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of gabriel.scherer@gmail.com) identity=pra; client-ip=209.85.214.46; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="gabriel.scherer@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of gabriel.scherer@gmail.com designates 209.85.214.46 as permitted sender) identity=mailfrom; client-ip=209.85.214.46; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="gabriel.scherer@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-it0-f46.google.com) identity=helo; client-ip=209.85.214.46; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="postmaster@mail-it0-f46.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0BYAACRxa1Xhi7WVdFegneBJHwHrV2GQIUHgX0chgECgUAHOBQBAQEBAQEBAREBAQEICwsJGS+CMgQBEwGCFAEEARIRHQEIEx0BAwELBgULNwICIgERAQUBHAYTCBqHdAEDDwihHoEyPjKLPIFqglsFhk0KGScNVINsAQEBAQEBBAEBAQEBAQEYAgYQhhqETYdBgloBBJk8gWKNM49DjDWCOBMegQ8eglmBWToyhysBAQE X-IPAS-Result: A0BYAACRxa1Xhi7WVdFegneBJHwHrV2GQIUHgX0chgECgUAHOBQBAQEBAQEBAREBAQEICwsJGS+CMgQBEwGCFAEEARIRHQEIEx0BAwELBgULNwICIgERAQUBHAYTCBqHdAEDDwihHoEyPjKLPIFqglsFhk0KGScNVINsAQEBAQEBBAEBAQEBAQEYAgYQhhqETYdBgloBBJk8gWKNM49DjDWCOBMegQ8eglmBWToyhysBAQE X-IronPort-AV: E=Sophos;i="5.28,510,1464645600"; d="scan'208,217";a="229720970" Received: from mail-it0-f46.google.com ([209.85.214.46]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 12 Aug 2016 14:54:26 +0200 Received: by mail-it0-f46.google.com with SMTP id x130so10890491ite.1 for ; Fri, 12 Aug 2016 05:54:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=8SpBJtaKtM1KdBDrfZdWlRJ8ozfu6wluqE7umuSQToM=; b=O7yIq1UTP+chE47tyWnfJEHwea+E6xLTVOpdo7pzyiNSBT3slXBY3UGg1ticAiQ/xd 8Bs9oW+7CBlRi/sxsT0Sp8VKVlDf9paDh8rZqMSA4RdBH+tBR8siAt81QJf+XMGqxcR9 qBHl+sjuJLprReZ9Me9Z9mdl9VZFD1RqRsFjx065OD/FhuWKHAkZJWBxRktVn+1+mlGq eHPo0i0hSxVmcPzqf3B4KIDTYn85qWOTmKf6NKPxwIfUTqvohg3/QKY7LnZ+Es/hxLls o4COT1i91q9c02twx7Ko0yjtRvtsjzKP6d+eqm/zG8KbNhfWvn1JWcI//ib+UKyZotNd Sd5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=8SpBJtaKtM1KdBDrfZdWlRJ8ozfu6wluqE7umuSQToM=; b=cBLPTYMGCtxvdlYT712ovQboN9/ua2JK7QYPYZJ7dIOQjN7siTYQO13FLksBJVih0y 1vcMjunzW3KZUfNryTJwmRydUobLJNNrYYH4Rb10sbnXXjpP1CYNGs9d43B/xCooZbIw 91vEFpPygfoWvCH3nJuOk9nYWu6AChLUvqLBj0QOQbTlPC1OJO3JByDeqZBZYHngv0FU vvtrrNderWUcxyxBwgu5nsFneocJO6CFcdC5p7Doa7SjypJBJDFOgTWxRIO4bD1lmBZ0 rV4exSAtGgZfO+JgSMWHIPsb4HB+2PAc3M7C8oT3uTxinyZCdd8InpZRj7+fVMel8Gvy XK4g== X-Gm-Message-State: AEkoouuWbDBJ8gf+wLqNyj57wL6Ku8e1zLKWh5OYXtmuSDLYRHGCKcVLv6chUMK+8orzsz86SDXQf1ND7ik3IQ== X-Received: by 10.36.55.133 with SMTP id r127mr2957095itr.15.1471006464959; Fri, 12 Aug 2016 05:54:24 -0700 (PDT) MIME-Version: 1.0 Received: by 10.79.20.67 with HTTP; Fri, 12 Aug 2016 05:53:44 -0700 (PDT) In-Reply-To: <0F7D3B1B3C4B894D824F5B822E3E5A172CF39E46@IRSMSX102.ger.corp.intel.com> References: <0F7D3B1B3C4B894D824F5B822E3E5A172CF39E46@IRSMSX102.ger.corp.intel.com> From: Gabriel Scherer Date: Fri, 12 Aug 2016 14:53:44 +0200 Message-ID: To: "Soegtrop, Michael" Cc: "caml-list@inria.fr" Content-Type: multipart/alternative; boundary=001a114066e6ce70990539df6113 Subject: Re: [Caml-list] Question on Printf wrappers --001a114066e6ce70990539df6113 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable You can do a lot with the format concatenation operator (^^): let prefixprintf prefix o fmt =3D Printf.fprintf o (prefix ^^ fmt) let suffixprintf suffix o fmt =3D Printf.fprintf o (fmt ^^ suffix) # prefixprintf "foo: " stdout "bar %d" 5;; foo: bar 5- : unit =3D () # suffixprintf ": foo" stdout "bar %d" 5;; bar 5: foo- : unit =3D () 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 =3D () 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 =3D > > 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=E2=80=99t 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=E2=80=99t 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 answ= er > to the question how I can return a function of type =E2=80=98a without mo= re 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 > --001a114066e6ce70990539df6113 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
You can do a lot with the format concatenat= ion operator (^^):

=C2=A0 let prefixprintf prefix o fmt =3D Pr= intf.fprintf o (prefix ^^ fmt)
=C2=A0 let suffixprintf suffix o fm= t =3D Printf.fprintf o (fmt ^^ suffix)

# prefixprintf "foo: &qu= ot; stdout "bar %d" 5;;
foo: bar 5- : unit =3D ()
# suffixp= rintf ": foo" stdout "bar %d" 5;;
bar 5: foo- : unit= =3D ()

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 =3D ()

<= br>

On= Fri, Aug 12, 2016 at 2:44 PM, Soegtrop, Michael <michael.soegtro= p@intel.com> wrote:

Dear Ocaml Users,

=C2=A0

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 O= Caml (honestly it took me a short while to figure it out):

=C2=A0

let prefixprintf (oc : out_channel) (fmt : ('a, = out_channel, unit) format) : 'a =3D

=C2=A0 Printf.fprintf oc "Prefix: ";

=C2=A0 Printf.fprintf oc fmt

;;

=C2=A0

I wonder how I would postfix something. Even convert= ing the formatted output first to a string and then output it together with= the postfix doesn=E2=80=99t seem to be easy, because the formatting functi= on 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 sim= ilar question would be how I can print the same formatted string twice.<= /u>

=C2=A0

I don=E2=80=99t have an application for this, I ask = this out of pure OCaml curiosity. So I am not so much interested in workaro= unds, more in an answer to the question how I can return a function of type= =E2=80=98a without more or less directly calling some variant of printf.

=C2=A0

Best regards,

=C2=A0

Michael

=C2=A0

Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, ww= w.intel.de
Managing Directors: Christin Eisenschmid, Christian Lamprechter
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928


--001a114066e6ce70990539df6113--