[plt-scheme] format string with named parts
On Feb 9, Todd O'Bryan wrote:
> In Python, you can do
>
> "%s %s %s" % "a", "b", "c"
>
> and the % operator (for string interpolation) will translate that to
>
> "a b c"
(This is not really string interpolation, at least not using its usual
sense.)
> This is almost identical to the PLT Scheme
>
> (format "~a ~a ~a" "a" "b" "c")
>
> becoming
>
> "a b c"
>
> In Python, you can also do:
>
> "%<1>s %<2>s %<3>s" % {"1": "a", "2": "b", "3":"c"}
>
> where the {...} bit is a hash map of "1" to "a", "2" to "b", etc., and
> this will be evaluate to
>
> "a b c"
(This is closer.)
> as expected. The obvious advantage of this is that you can switch
> around the order of the pieces in the formatted string and since
> they're named, each piece will end up where it's supposed to be.
>
> Does PLT Scheme have anything similar, where you can name the parts of
> a formatted string and fill them in using either a #hash or the
> current environment or something? The web-server has templates that do
> this, but the templates seem to involve a lot of overhead that I don't
> think I need.
Basic output:
#lang at-exp scheme
(require scribble/text)
(define foo "FOO")
(define bar "BAR")
(output @list{@foo and @bar})
(It's easy to make it return a string instead, I just didn't provide
another function (or a keyword argument or something) because there
was no need and I didn't see any interface that was obviously better.)
This is using the same functionality that the web server templates are
using, but you don't need the extra overhead of using the web server
or separate files. Here's how you'd do it:
#lang scribble/text
@(define foo "FOO")
@(define bar "BAR")
@foo and @bar
@(define (choose x y) @list{@x before @y or @y before @|x|?})
@choose[foo bar]
See also http://docs.plt-scheme.org/scribble/preprocessor.html
--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://barzilay.org/ Maze is Life!