[plt-scheme] format string with named parts
Just a quick try not using @-exps. Does something like this look good?
#lang scheme
(define (py-format str couples)
(regexp-replace*
"~<([^>]*)>([aAsSvVeEcCbBoOxX])"
str
(lambda(all key str-format)
(let ([key-val (assoc key couples)])
(if key-val
(format (string-append "~" str-format) (second key-val))
(error "Key not found in py-format: " key))))))
(py-format "~<x>a ~<y>a ~<z>s"
'(("x" "ola")
("y" (hey "hey"))
("z" "salute")))
; "ola (hey hey) \"salute\""
On Wed, Feb 10, 2010 at 17:10, Todd O'Bryan <toddobryan at gmail.com> wrote:
> On Tue, Feb 9, 2010 at 10:40 PM, Eli Barzilay <eli at barzilay.org> wrote:
> >
> > 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]
> >
>
> The nice thing about the Python version is that you can just have a
> string with placeholders for values you're going to include later. Am
> I correct that I really need to write a function (like choose above)
> that expects the values I want to go into the string so that the
> @-exprs don't get evaluated before they have values?
>
> Obviously, that's not much more difficult, but my brain just didn't go
> there automatically. Clearly, I need to spend more time programming in
> Scheme. :-)
>
> Todd
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20100210/b93457f0/attachment.html>