[plt-scheme] syntax and promises and possibly phases
How about
(define rx-hash (make-hash-table))
(define-macro (compile-rx expression)
`(let ([promise (delay ,expression)])
(hash-table-put! rx-hash `',expression promise)))
(define-macro (rx expression)
`(force (with-handlers ([exn? (lambda (e) (error 'rx "Expression not
compiled"))])
(hash-table-get rx-hash `',expression))))
Neil W. Van Dyke wrote:
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
>I suspect I'm blinded by the blindingly obvious this morning, but...
>
>Is there a way to do the following with define-syntax?
>
>I want to define a syntax extension "rx" that will make an expensive
>computation "compile-rx" happen only once, so that:
>
> (define foo
> (lambda (...)
> ...
> (rx-match (rx "abc") ...)
> ...))
>
>is equivalent to:
>
> (define foo
> (let ((rx-promise-1 (delay (compile-rx "abc"))))
> (lambda (...)
> ...
> (rx-match (force rx-promise-1) ...)
> ...)))
>
>If this is possible, then I can make "rx-match" syntax that
>automagically compiles regexps expressed as string literals only once
>per syntactic occurrence.
>
>This would really help code readability for the most common uses of
>regexps, especially since one often needs to refer to regexp submatches.
>
>(BTW, I'd rather not maintain a table mapping strings to compiled regexp
>form, except perhaps as a complement to this syntax extension, since I'd
>want to avoid a table lookup on each "rx-match" call anyway.)
>
>(Also BTW, I'm not concerned with concurrency issues right now; I
>suspect that's orthogonal to the syntax extension question.)
>
>
>