2011/6/16 Alex Baranosky <alexander.barano...@gmail.com>:
> 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)))
> Thanks for the help!
> Alex

When you generate code programmatically (e.g. in a macro), you
generate the data structures as objects rather than their string form.
Consider this macro*:

(defmacro unless [cond-expr false-expr true-expr]
  (liist 'if cond-expr true-expr false-expr))

To make the (if ...) list you don't dive into the string
representation of this data structure -- i.e. you don't try to do
something like (str "(if " cond-expr " " ... ")"). You should think
about what kind of objects the syntax represent and try to generate
those instead. Code is data. The string representation is only for
storing code in files and interacting with a human. Clojure itself
works with the data as objects.

To construct a regex pattern object re-pattern is the function to use,
as you already know. There is no reason to dive into string syntax. If
you happen to want to serialize this object later (e.g. print it at
the repl or dump it to a text file), this regex object will print as
an ordinary regex literal.

* Using syntax-quote is probably more ideomatic here.

// Rasmus Svensson (raek)

-- 
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