Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion A quasiquote for Clojure?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Chouser  
View profile  
 More options Dec 23 2008, 3:20 pm
From: Chouser <chou...@gmail.com>
Date: Tue, 23 Dec 2008 15:20:43 -0500
Local: Tues, Dec 23 2008 3:20 pm
Subject: Re: A quasiquote for Clojure?

On Tue, Dec 23, 2008 at 11:08 AM, Rich Hickey <richhic...@gmail.com> wrote:

> On Wed, Dec 17, 2008 at 2:53 PM, Rich Hickey <richhic...@gmail.com> 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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.