[plt-scheme] format string with named parts

From: YC (yinso.chen at gmail.com)
Date: Wed Feb 10 14:56:48 EST 2010

On Wed, Feb 10, 2010 at 11:17 AM, Todd O'Bryan <toddobryan at gmail.com> wrote:

> I wasn't clear. I need to make the format strings before I'm ready to
> plug in the named values. In Python, they're just strings, so there's
> no problem creating them before you have the values ready to insert,
> but since @-exprs are really code, you have to hold off evaluating
> them until you have the values ready to plug in.
> Either using a lambda-expression or figuring something out like
> Laurent suggested is not hard, once you think about doing it. My
> biggest problem seems to be not being aware exactly which tools I have
> in my toolbox and, even when I know there's a tool in there, not
> realizing it's good for the task at hand.

Some of the tools live on planet ;)  For example - bzlib/template provides
such capability.  There might be others as well.

> (require (planet bzlib/template))
> ;; pass in pairs of key & values to iqs - note order does not matter
> (iqs "Hello, $name, $question?" `(question . "how are you") `(name .
"Hello, Todd, how are you?"
> ;; you can change the escape char too via iqs-symbol-start - let's change
it to % instead of $
> (parameterize ((iqs-symbol-start #\%))
    (iqs "Hello, %name, %question?" `(question . "how are you") `(name .
"Hello, Todd, how are you?"
> ;; if you do not pass in the value it prints out the original placeholder
> (iqs "Hello, $name, $question?" `(question . "how are you"))
"Hello, $name, how are you?"

On Wed, Feb 10, 2010 at 11:29 AM, Eli Barzilay <eli at barzilay.org> wrote:

> I consider the python thing a hack, which is really compensating for
> *not* having a proper string interpolation thing.  Perhaps a more
> explicit way to see the hack is that the hash table is essentially a
> kind of a namespace thing, which is orthogonal to the namespace your
> code is using.  I guess that you could claim that it's less of a hack
> in python since you can reify the current namespace as a hash table
> anyway (and perhaps that's a common idiom?).

IMHO the pattern comes up more often when you need `format` but with named
instead of ordinal arguments, and when you might need to custom handle the
non-existence of named argument (i.e. a default value).

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20100210/b197aabb/attachment.html>

Posted on the users mailing list.