On Tue, Dec 23, 2008 at 11:08 AM, Rich Hickey <[email protected]> wrote:
>
> On Wed, Dec 17, 2008 at 2:53 PM, Rich Hickey <[email protected]> wrote:
>>
>> Moving syntax-quote out of the reader might be a big deal. But I think
>> scenarios like this could be covered if:
>>
>> ~x not in a syntax-quote yielded the form (unquote x) from the reader
>>
>> unquote would not be defined by Clojure, so still an error if allowed
>> to get evaluated.
>>
>> Things like your sql would be macros that handled (unquote x)
>> internally.
>>
>
> SVN 1184 implements this.
>
> Feedback welcome on its utility for macro writers.
Here's an example of one way to use it:
user=> (where (and (> i (- 3 1)) (< i ~(+ 3 1))))
"where i > 3 - 1 and i < 4"
user=> (let [my-name "'chouser'"] (where (and (> id 0) (= name ~my-name))))
"where id > 0 and name = 'chouser'"
This 'where' macro produces something that looks vaguely like a SQL
"where" clause. It accepts s-expressions to translate, but also
allows you to mark sub-expressions with ~ to indicate that they should
be evaluated as regular clojure expresions, and not translated
literally to SQL. If it were more than a toy example, it would use
prepareStatement or similar to get appropriate quoting of clojure
objects, rather than just stuffing them directly in the resulting
string.
Here's the code:
(defmacro where [e]
(let [f (fn f [e]
(if-not (list? e)
[(str e)]
(let [[p & r] e]
(if (= p `unquote)
r
(apply concat (interpose [(str " " p " ")]
(map f r)))))))]
(list* `str "where " (f e))))
--Chouser
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---