From: Matej Kosik <5764c029b688c1c0d24a2e97cd764f@gmail.com>
To: oleg@okmij.org
Cc: caml-list@inria.fr, gabriel.scherer@gmail.com
Subject: Re: [Caml-list] [batteries] ... how to create (format) directives
Date: Fri, 29 Nov 2013 09:32:04 +0000 [thread overview]
Message-ID: <52985F14.4090508@gmail.com> (raw)
In-Reply-To: <20131128063044.55659.qmail@www1.g3.pair.com>
On 28/11/13 06:30, oleg@okmij.org wrote:
>
> Enclosed is one example of a combinator library for formatting, in
> plain OCaml (even Caml-lite, probably), with no extensions, GADTs,
> type classes, etc. Here is a simple demo
This is interesting. Thank you.
>
> let ex1 = let open PrintComp in
> pr s"1" s"2" printf
>
> (* prints: 12 *)
>
> (* Look! No string concatenation operations! We separate operations
> with mere white space (and often even it is not needed
> *)
>
> let ex2 = let open PrintComp in
> pr s"1" s"2" b"3" i 4
> sprintf
> (*
> val ex2 : string = "12<bold>3</bold>4"
> *)
>
>
> (* The format is really typed *)
> let ex3 = let open PrintComp in
> pr s"1" s"2" i "x" b"3" i 4
> sprintf
> (*
> Characters 50-53:
> pr s"1" s"2" i "x" b"3" i 4
> ^^^
> Error: This expression has type string but an expression was expected of type
> int
> *)
>
>
> (* It is possible to avoid s" " below, so to insert interworld space
> automatically
> *)
I do not understand what do you have in mind (above).
(Sometimes spaces are desired, sometimes they are undesired.
I do not see a general rule that could be embedded to `b' so that it could reliably decide in either way.)
>
> let ex4 = let open PrintComp in pr
> b"mdx" s" " it "command" br
> s"Perform a given " it "command" br
> s"Section COMMANDS describes all the supported commands."
> sprintf
>
> (*
> val ex4 : string =
> "<bold>mdx</bold> <i>command</i>\n\nPerform a given <i>command</i>\n\nSection COMMANDS describes all the supported commands."
> *)
>
> (* The formatting sequence can be interrupted, e.g.,
> to bind some common subexpressions or to perform some computations
> *)
>
> let ra = fun x f -> f x
>
> let ex5 = let open PrintComp in pr
> b"mdx" s" "
> (* interrupt the flow *)
> begin let cmd st = ra st it "command" in
> fun st -> ra st (* continue with the flow *)
> cmd br
> s"Perform a given " cmd br
> s"Section COMMANDS describes all the supported commands." end
> sprintf
>
> (*
> val ex5 : string =
> "<bold>mdx</bold> <i>command</i>\n\nPerform a given <i>command</i>\n\nSection COMMANDS describes all the supported commands."
> *)
>
>
> Here is the implementation, of a FORTH like language for formatting.
> Polyvariadic functions are possible even in plain OCaml.
>
> module PrintComp = struct
> (* Put this at the beginning *)
> let pr k = k []
>
> (* to format a string *)
> let s = fun st (str:string) k -> k (str :: st)
>
> (* to format a string in bold *)
> let b = fun st (str:string) k -> k ("</bold>" :: str :: "<bold>" :: st)
>
> (* to format a string in italics *)
> let it = fun st (str:string) k -> k ("</i>" :: str :: "<i>" :: st)
>
> (* to format an integer *)
> let i = fun st n k -> k (string_of_int n :: st)
>
> (* generate a line break *)
> let br = fun st k -> k ("\n\n" :: st)
>
> (* To finally print as a string *)
> let sprintf st = String.concat "" (List.rev st)
>
> (* To finally print on stdout *)
> let printf st = List.iter print_string (List.rev st)
>
> (* To finally print on channel *)
> (* similarly *)
> end;;
>
>
next prev parent reply other threads:[~2013-11-29 9:32 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-11-25 15:46 [Caml-list] [batteries] ... how to create (format) directives that do not take any arguments? Matej Kosik
2013-11-25 16:27 ` Gabriel Scherer
2013-11-27 11:37 ` Matej Kosik
2013-11-28 6:30 ` [Caml-list] [batteries] ... how to create (format) directives oleg
2013-11-29 9:32 ` Matej Kosik [this message]
2013-11-30 3:15 ` oleg
2013-11-27 11:54 ` [Caml-list] [batteries] ... how to create (format) directives that do not take any arguments? Jeremie Dimino
2013-11-27 12:52 ` Matej Kosik
2013-11-27 13:00 ` Jeremie Dimino
2013-11-29 9:32 ` Matej Kosik
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=52985F14.4090508@gmail.com \
--to=5764c029b688c1c0d24a2e97cd764f@gmail.com \
--cc=caml-list@inria.fr \
--cc=gabriel.scherer@gmail.com \
--cc=oleg@okmij.org \
/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