On Jun 16, 4:47 am, Alex Baranosky <alexander.barano...@gmail.com>
wrote:
> IS it possible to use the regex reader macro #"" with generated code?  What
> I mean is do something like:
>
> #"${(join "|" (range 1 10000))}"
>
> I'm using ${...} to mean string interpolation, though I know Clojure doesn't
> have that syntax.  Is there a way to get this effect or must I use
> (re-pattern (join "|" (range 1 10000)))

In your specific example, as all the components used to build your
regex are static, you could use re-pattern as a macro:

(defmacro re-ra­nge []
  (re-p­attern (cloj­ure.string­/join "|" (rang­e 1 1000)­)

However, as soon as you want to parameterise the regex, things get
hairy. You *could* have your macro take arguments like this:

; bad idea
(defmacro re-ra­nge [start end]
  (re-p­attern (cloj­ure.string­/join "|" (rang­e start end­)

This would work as long as the macro is passed static values, like 1
and 1000. This, however, *won't* work:

(let [x 1000]
  (re-range 1 x)

In this case #'re-range macro would receive the symbol 'x as its end
argument, because macros are executed at compile time (or macro
expansion time, to be more precise) and take code as their data.

Hope that helps,
--
Daniel

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to