From: Alessandro Baretta <a.baretta@barettadeit.com>
To: Ocaml <caml-list@inria.fr>
Subject: Request for comments: Printf list conversion
Date: Thu, 12 Jan 2006 10:19:27 +0100 [thread overview]
Message-ID: <43C61F1F.4030101@barettadeit.com> (raw)
I would like to transform the following rather vague idea into a formal feature
request. Before doing this, I would like to know what others think about it.
I continually come across the need to pretty-print via Printf a list of
values--for example, let's say an int list. Printf has conversion specifiers for
int, but int list cannot be handled easily. What I usually end up doing is
converting values in the list to strings, concatenate them, and output the
result with the %s conversion specifier.
Printf.bprintf buf "A beatiful list of ints: [ %s ]\n" (String.concat " ;"
(List.map string_of_int [ 1;2;3;4;5 ] )
Obviously, this is radically ineffiecient. A better solution might be the following.
Printf.bprintf buf "A beatiful list of ints: [ %t ]\n" (fun buf ->
List.iter (Printf.bprintf buf " %d;") [ 1;2;3;4;5 ]
)
The above is adequately fast, but terribly ugly.
I believe that it should be possible to add a conversion-flag--let me use '&' as
an example--telling the type-system and the Printf module that the corresponding
parameter is a list of values whose type is determined by the
conversion-specifier proper. Let me show what I would like to be able to write:
Printf.bprintf buf "A beatiful list of ints: [ %&d ]\n" [ 1;2;3;4;5 ]
Here, the &-flag would tell the type-system that the first parameter is an 'a
list. The fact that 'a = int is to be inferred from the actual conversion
specifier ('d'). Upon recognizing a %& conversion, Printf would have to iterate
over the corresponding parameter with the appropriate conversion
(format_int_with_conv, in this case).
Now, of course, the '&' conversion-flag does not settle the issue, as a
parameter like the first one of String.concat would also be needed. Another
issue which is brought up by the idea I have jotted down is how to handle
conversions like '%&a' and '%&t'.
What do you guys think about all this?
Alex
next reply other threads:[~2006-01-12 9:19 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-01-12 9:19 Alessandro Baretta [this message]
2006-01-12 9:31 ` [Caml-list] " Jean-Marc EBER
2006-01-12 9:55 ` Jean-Christophe Filliatre
2006-01-12 12:55 ` Damien Doligez
2006-01-12 13:27 ` Virgile Prevosto
2006-01-13 8:47 ` Jean-Christophe Filliatre
2006-01-12 15:50 ` Bill Wood
2006-01-12 17:39 ` Richard Jones
2006-01-12 19:29 ` Bill Wood
2006-01-13 9:31 ` Hendrik Tews
2006-01-13 16:28 ` Bill Wood
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=43C61F1F.4030101@barettadeit.com \
--to=a.baretta@barettadeit.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